phone_138
2022-05-21 10:09
采纳率: 0%
浏览 18

C#串口通信SerialPort

问题遇到的现象和发生背景

读取到串口数据(str1)为01 02 01 01 60 48,我想让str1的数据等于str2的数据,但str2返回的数据却不是01 02 01 01 60 48

问题相关代码,请勿粘贴截图

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;

namespace SerialPortReadData
{
internal class Program
{

    public static string[] IpAddress = { "127.0.0.1", "192.168.1.11", "192.168.1.12", "192.168.1.13", "192.168.1.14", "192.168.1.15", "192.168.1.100" };//主机IP地址
    public static string[] YiPinSendToData = { "B003D100110E", "B003D100211E", "B003D100312E", "B003D100110E", "B003D100211E", "B003D100312E", "B004D100010E", "B004D200011E", "B004D300012E", "B004D610016E", "B004D600015E" };//易拼打开场景1-3 发送十进制字符串
    public static string[] ReceiveData = { "01 02 01 01 60 48", "01 02 01 00 A1 88" };

    public static void UdpStringToHexString(string datapccket)//UDP协议,发送十进制字符串
    {
        byte[] buffer = new byte[1024];
        IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IpAddress[0]), 41235);//易拼端口
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        buffer = Encoding.ASCII.GetBytes(datapccket);
        client.SendTo(buffer, buffer.Length, SocketFlags.None, ep);
        client.Close();
    }
    public static void SerialReceive()
    {
        SerialPort mySerialPort = new SerialPort("com8");
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
        mySerialPort.Open();
        Console.WriteLine("监听串口数据:");
        
        Console.ReadKey();
        mySerialPort.Close();
    }
   
    //字节数组转16进制字符串
    public static string BytesToHexStr(byte[] bytes)
    {
        string returnStr = "";
        if (bytes != null)
        {
            for (int i = 0; i < bytes.Length; i++)
            {
                returnStr += bytes[i].ToString("X2") + " ";
            }
        }
        return returnStr;
    }


    public static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        int _bytestoread = sp.BytesToRead;
        byte[] recvData = new byte[_bytestoread];
        sp.Read(recvData, 0, _bytestoread);
        string str1 = BytesToHexStr(recvData);
        string str2 = ReceiveData[0];
        Console.Write(str1);
        Console.Write(str2);
        if (str1 ==str2)
        {
            UdpStringToHexString(YiPinSendToData[1]);
        }

    }


    public static void Main(string[] args)
    {
        SerialReceive();
    }
}

}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

我想让接收到数据str1等于str1的数据,然后执行UDP控制

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题