c# 串口 转发到 TCP 客户端

前言

对于数据流Stream的转发.在.net 3.5之后的版本只需要 stream.CopyTo(stream).

目前只是为了方便调用测试,花了一点点时间做了一个简单的调用demo

完整代码

using System.IO.Ports;
using System.Net.Sockets;
using System.Text;

namespace SerialPortTransferTcp;

public class Program
{
    private static SerialPort _serialPort;

    //TCP服务器端口
    private const int port = 8000;
    //TCP服务器IP
    private const string server = "127.0.0.1";
    static TcpClient client = null;

    static void Main(string[] args)
    {
        //开始链接串口
        _serialPort = new SerialPort("COM5", 115200, Parity.None, 8, StopBits.One);
        _serialPort.Open();//开始链接串口
        Console.WriteLine("串口链接成功!");

        //开始链接TCP 
        startConnectTcp();
        Console.ReadLine();
    }

    public static void startConnectTcp()
    {
        try
        {
            client = new TcpClient();
            client.Connect(server, port);

            NetworkStream stream = client.GetStream();
            Console.WriteLine("TCP Connected:" + client.Connected);
          
            _serialPort.BaseStream.CopyToAsync(stream);//将串口流拷贝到TCP流
            stream.CopyToAsync(_serialPort.BaseStream);//将TCP流拷贝到串口流
         
            Console.WriteLine("按任意键结束!");
            Console.Read();

            stream.Close();
            client.Close();
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception: {0}", e.Message);
        }

    }
}

运行效果

posted @ 2022-06-09 22:20  鹄鹄  阅读(923)  评论(0编辑  收藏  举报