TCP和串口间的互相通信(透传)

TCP和串口间的互相通信(透传)

Tcp作为服务端,接收消息,通过串口发送

private void TcpDataReceive(object clientmsg)
        {
            string tcpReceiveFormat = "Hex";
            Dispatcher.Invoke(new Action(() =>
            {
                if (TcpSendFormat.IsChecked == true)
                {
                    tcpReceiveFormat = "ASCII";
                }
            }));
            
            Byte[] bytes = new Byte[1024];
            int i;
            NetworkStream stream = clientmsg as NetworkStream;
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                string msg = "";
                byte[] buffer = new byte[i];
                buffer = bytes.Take(i).ToArray();
                msg += $"[{DateTime.Now.ToString("HH:mm:ss.fff")} ###Rece]  ";
                if (tcpReceiveFormat.Equals("Hex"))
                {
                    string tempss = "";
                    foreach (byte b in buffer)
                    {
                        msg += $"{b.ToString("X2")} ";
                        tempss += b.ToString("X2");
                    }
                    messageModel.TcpReceNewestMsg = tempss;
                }
                else if (tcpReceiveFormat.Equals("ASCII"))
                {
                    msg += $"{Encoding.ASCII.GetString(buffer)} ";
                    messageModel.TcpReceNewestMsg = Encoding.ASCII.GetString(buffer);
                }
                msg += "\r\n";
                messageModel.TcpReceiveMessage += msg;
            }
        }
private void SerialDataReceive(object sender, SerialDataReceivedEventArgs e)
        {
            string receiveFormat = "Hex"; 
            Dispatcher.Invoke(new Action(() =>
            {
                if (SerialSendFormat.IsChecked == true)
                {
                    receiveFormat = "ASCII";
                }
            }));
            int num = serialPort.BytesToRead;
            byte[] buffer = new byte[num];
            receiveCount += num;
            serialPort.Read(buffer, 0, num);

            string msg = "";
            msg += $"[{DateTime.Now.ToString("HH:mm:ss.fff")} ###Rece]  ";

            if (receiveFormat.Equals("Hex"))
            {
                string temps = "";
                foreach (byte b in buffer)
                {
                    msg += $"{b.ToString("X2")} ";
                    temps += b.ToString("X2");
                }
                messageModel.SerialReceNewestMsg = temps;
            }
            else if (receiveFormat.Equals("ASCII"))
            {
                msg += $"{Encoding.ASCII.GetString(buffer)} ";
                messageModel.SerialReceNewestMsg = Encoding.ASCII.GetString(buffer);
            }
            msg += "\r\n";
            messageModel.SerialReceiveMessage += msg;

        }

在这里插入图片描述
源码资源:https://download.csdn.net/download/qq_40397362/85088218``

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值