串口读取问题 [英] SerialPort reading problem

查看:56
本文介绍了串口读取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须创建一个应用程序,每200ms从PLC读取一次寄存器.为此,我正在使用类似的方法:

I have to create an aplication which reads the registers from a PLC at each 200ms. To do that I am using something like:

SerialPort port=new SerialPort();
   bool dataRecieved=false;

   private void Form1_Load(object sender, EventArgs e)
   {
      port.RecievedBytesThrescold=21; //always the incoming message has to have 21 bytes

      timer1.Interval=200;
      timer1.Start();
   }

   private void ProcesTimier_Tick(object sender, EventArgs e)
   {
      if(dataRecieved)
      {
           dataRecieved=false;
           port.Read(buffer_rx,0,21);
           TranslateValues();
      }

      //Do something else based on the translated values

      if(port.IsOpen && connectected)
      {
         port.Write(buffer_tx,0,lengh);   //sends the command for a new reading
      }
   }

   private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
   {
           Invoke(new EventHandler(DoUpdate));
   }
   void DoUpdate(object sender, EventArgs e)
   {
           dataRecieved = true;
   }



问题在于,我的应用程序有时会不时出现冻结现象,并且当我进行调试以查看应用程序冻结的执行行时,它会将我指向port.Read(),但事实并非如此.''恢复任何异常.我尝试使用try-catch块,但没有捕获任何东西.我设置了time1.Interval = 2000,但是它也不起作用.
我的问题是,如果您知道为什么我的应用程序会冻结并将我指向port.Read(),但是它没有捕获任何异常,我该如何解决?



The problem is that from time to time, at random moments, my application freezies and when I go with the debug to see the execution line at which the application freezes, it points me to the port.Read(), but it doesn''t retun any exeption. I have tried to use a try-catch block, but it didn''t catch anything. I made time1.Interval=2000, but it didn''t work either.
My question is if you know why my application freezies and points me to the port.Read(), but it doesn''t catch any exception and how could I get over it?

推荐答案

您的计时器应该不是必需的,并让port.RecievedBytesThrescold = 1(默认值).如果将其设置为21,唯一可以确定的是它将至少为21长.我的建议是缓冲自己:
Your timer should not be necessary, and let port.RecievedBytesThrescold=1 (default). The only thing you can be sure of if you set it to 21, is that it will be at least 21 long. My suggestion is to do the buffering your self:
byte[] buffer = new byte[50];
int endOfBuffer = 0;

void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
  int dataLength = _serialPort.BytesToRead;
  byte[] data = new byte[dataLength];
  int nbrDataRead = _serialPort.Read(data, 0, dataLength);
  if (nbrDataRead == 0)
    return;
      
  // Do some buffering
  Buffer.BlockCopy(data, 0, buffer, endOfBuffer, nbrDataRead);
  endOfBuffer += nbrDataRead;
  
  // Check if you got 21 bytes
  if(endOfBuffer >= 21)
  {
    byte[] plcDataArray = new byte[21];
    Buffer.BlockCopy(buffer, 0, plcDataArray, 0, 21);
    endOfBuffer -= 21;
    if(endOfBuffer != 0)
      Buffer.BlockCopy(buffer, 0, buffer, 21, endOfBuffer);
    
    // Signal your code that you got a complete data package from the plc
    if(SignalCompletePackageEvent != null)
      SignalCompletePackageEvent(this, EventArgsTakingYourByteArray(plcDataArray));   
  }
  
}


我共享了一个非常简单的串行端口应用程序,您可以随意查看它基本的串行端口侦听应用程序.
祝你好运.注意:如果您对解决方案进行投票,总是很高兴;)


I have shared a very simple serial port app, and you''re free to look at it Basic serial port listening application.
Good luck. NB: Always happy if you vote on the solution ;)


这篇关于串口读取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆