C# 使用串口。收到部分回复

问题描述 投票:0回答:1

我正在用c#、windowform制作一个串口程序。

当我向我的设备发送消息时,我的程序仅收到部分响应。 当我尝试使用 ComPortMaster 时,我得到了我所期望的结果(c0 06 02 43 6d 00 2a c1 c0 08 02 6d 00 01 20 00 46 c1)。 但在我的程序中,它只显示“c0 06 02 43 6d 00 2a”,响应的第一部分。

我设置波特率、奇偶校验、数据位等。 ComPortMaster 设置。波特率 2400,数据位 8,停止位 1,奇偶校验 在我的代码中,

openSerial(cbxComport.SelectedItem.ToString(), 2400, 8, StopBits.One, Parity.Odd);

我还设置了缓冲区大小和超时。

serialPort1.ReadBufferSize = 8192;
serialPort1.WriteBufferSize = 8192;
serialPort1.ReadTimeout = 5000;
serialPort1.WriteTimeout = 1000;

为了获取数据,我使用了这个。

serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    this.Invoke(new EventHandler(MySerialReceived1));
}



void MySerialReceived1(object s, EventArgs e)
{
    int RecvSize = serialPort1.BytesToRead;
    byte[] data = new byte[RecvSize];
    serialPort1.Read(data, 0, RecvSize);

 
    AppendLog(DateTime.Now.ToString("HH:m:ss:fff") + " raw[" + RecvSize + "] : " + Util.ByteToString(data) + Environment.NewLine);
            
    
    //I sent data to another method, so that I can get a full packet.
    //I tried without this part, still message stopped
  
  SerialControl.recieveData(data);
    if (SerialControl.finishedFlag)
    {
        byte[] recieved = SerialControl.GetRecieved();
        string msg = Util.ByteToString(recieved);
        txtRecieved.Text = msg;
        tbLog.AppendText("Received : " + msg + Environment.NewLine);
        classifyData(recieved);
    }
}

但我得到的是 发送:c0 6 2 43 6d 0 2a c1 15:25:05:809 原始[5]:c0 6 2 43 6d 15:25:05:856 原始[2]:0 2a

它总是停在c1。

这是我的 SerialControl 类。 响应将同时得到回显和响应。所以它认为第一个是回声,第二个是响应。

 public static class SerialControl
 {
     public static SerialPort serialPort = new SerialPort();

     public static bool recieveFlag = false, finishedFlag = false, echoFlag = false;
     public static byte startByte = 0x7E, endByte = 0xE7;
     public static List<byte> echoArr = new List<byte>();
     public static List<byte> dataArr = new List<byte>();
     public static List<byte> recieveArr = new List<byte>();

     public static void sendData(byte[] data)
     {
         serialPort.Write(data, 0, data.Length);
     }

     public static void Clear()
     {
         finishedFlag = false;
         recieveFlag = false;
         recieveArr.Clear();
         dataArr.Clear();
         echoArr.Clear();
         return;
     }

     public static void recieveData(byte[] data)
     {
         if (finishedFlag) return;
         int startIndex = Array.IndexOf(data, startByte);
         int endIndex = Array.LastIndexOf(data, endByte);
         
         if (recieveFlag) // receiving
         {
             //save all of data
             recieveArr.AddRange(data);
             finishedFlag = false;

             recieveArr.AddRange(Util.sliceArray(data,0,endIndex));
            
         }
         else {//did not recieve yet
             if (startIndex == -1)
             {
                 return;
             }
             else // stx exist
             {
                 //save all of data
                 recieveArr.AddRange(data);
                 recieveFlag = true;
                 finishedFlag = false;
             }
         }

         //check received data
         endIndex = recieveArr.IndexOf(endByte);
         if (!echoFlag && endIndex > -1) //did not get echo yet
         {
             Console.WriteLine("ehco check");
             echoArr = recieveArr.GetRange(0, endIndex + 1) ;
             echoFlag = true ;
             recieveArr.RemoveRange(0, endIndex + 1);
         }
         //already have echo
         endIndex = recieveArr.IndexOf(endByte);
         Console.WriteLine("data endIndex:"+endIndex);
         if (echoFlag) {
             if (endIndex > -1) {
                 Console.WriteLine("datacheck");

                 dataArr = recieveArr.GetRange(0, endIndex + 1) ;
                 finishedFlag = true ;
                 recieveFlag = false ;
             }
         }
     }

     public static byte[] GetRecieved()
     {
         if (finishedFlag)
         {
             byte[] result = dataArr.ToArray();
             finishedFlag = false;
             recieveFlag = false;
             recieveArr.Clear();
             dataArr.Clear();
             echoArr.Clear();
             return result;
         }
         else return null;
     }

     public static byte[] GetEcho() {
         if (echoFlag)
         {
             byte[] result = echoArr.ToArray();
             return result;
         }
         else return null;
     }
 }
c# serial-port
1个回答
0
投票

我有一个类似的问题 - 我必须在接收数据时放置一个循环

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    Console.WriteLine("Delegate Fired Reading Port Now");
    do
    {
      int result = 0;
      try
      {
         result = SerialPort.ReadByte();
      }
      catch (Exception ex)
      {
      }

      if (result != 0)
      {
        Console.WriteLine($"Byte Read {result}");
        BytesReceivedCount++;
        VersionString = String.Concat(VersionString, result);
      }
      else
      {
        ReadSerialPort = false;
      }
   }
   while (ReadSerialPort == true);
}
© www.soinside.com 2019 - 2024. All rights reserved.