长电缆超时的串行通信超时 [英] Serial communication timeout on long cable time out

查看:52
本文介绍了长电缆超时的串行通信超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 rs232 读取不同硬件的应用程序.它已经过测试并且运行良好.对于最终应用,我需要引入数百米长的电缆,这意味着我有 rs485 转换器.

I have an application which reads different hardware via rs232. It has been tested and it was working perfectly. For the final application I needed to introduce a few hunder m long cable which means I have rs485 converters.

当我运行应用程序读取硬件时,出现 System.IO.Ports.SerialStream.Read 超时错误.不幸的是,我已将超时增加到 20 秒,但它没有解决问题

When I run my application to read the hardware I get a time out error for System.IO.Ports.SerialStream.Read. I have increased the timeout to 20sec unfortunately it did not resolve the problem

我尝试了不同的应用程序来读取硬件,即使读取频率为 1 秒,它们也能正常工作.

I have tried different applications to read the hardware and they worked even with 1sec reading frequency.

通信使用 modbus 协议,我认为在当前阶段是无关紧要的,因为我没有到达阶段接收任何东西.

The communication is using modbus protocol, which is in the current stage I assume is irrelevant as I do not get to the stage to receive anything.

我的代码是这样的:首先是串口打开和初始化:

My code looks like that: First the serial port opening and initialization:

//get the right modbus data structure element
ModBus MB = (ModBus)s[0].sensorData;

//set up the serial port regarding the data structure's data
SerialPort sp = new SerialPort();
sp.PortName = MB.portName;
sp.BaudRate = Convert.ToInt32(MB.baudRate);
sp.DataBits = MB.dataBits;
sp.Parity = MB.parity;
sp.StopBits = MB.stopBits;
//Set time outs 20 sec for now
sp.ReadTimeout = 20000;
sp.WriteTimeout = 20000;

//将端口添加到读者可以访问的List中portList.Add(sp);sp.Open();

//add the port to a List which can be accessed by the reader portList.Add(sp); sp.Open();

读取硬件:

//get the right port for com
SerialPort sp = getRightPort();
ModBus MB = getRightModBusStructureelement();
try
   {
     //Clear in/out buffers:
     sp.DiscardOutBuffer();
     sp.DiscardInBuffer();

     //create modbus read message
     byte[] message = createReadModBusMessage();

     try
        {
         sp.Write(message, 0, message.Length);

         // FM.writeErrorLog output included for easier debug
         FM.writeErrorLog(DateTime.Now + ": ModBus Message Sent");
         FM.writeErrorLog(DateTime.Now + ": Read TimeOut = " + sp.ReadTimeout + " Write TimeOut = " + sp.WriteTimeout);

         int offset = 0, bytesRead;
         int bytesExpected = response.Length;

         FM.writeErrorLog(DateTime.Now + ": start read");

         while (bytesExpected > 0 && (bytesRead = sp.Read(response, offset, bytesExpected)) > 0)
            {
              FM.writeErrorLog(DateTime.Now + ": read - " + offset);
              offset += bytesRead;
              bytesExpected -= bytesRead;
            }
        }
        catch (Exception err)
        {
           Console.WriteLine("ERROR Modbus Message to serial port ModBus: " + err);
           FM.writeErrorLog(DateTime.Now + " - " + "ERROR Modbus Message to serial port ModBus: " + err);
         }

  }

在尝试应用程序后,我从 ErroLog.txt 得到以下输出:

After trying the application I got the following output from the ErroLog.txt:

14/01/2016 17:18:17: ModBus Message Sent
14/01/2016 17:18:17: Read TimeOut = 20000 Write TimeOut = 20000
14/01/2016 17:18:18: start read
14/01/2016 17:18:38 - ERROR Modbus Message to serial port ModBus: System.TimeoutException: The operation has timed out.
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Ports.SerialPort.Read(Byte[] buffer, Int32 offset, Int32 count)
   at ProbReader.SensorReader.modbusReading(List`1 mm, Int32 spCounter)
14/01/2016 17:18:38: 0
14/01/2016 17:18:38: 0

为了以防万一但同样的错误,我已将超时增加到 60 秒:

I have increased the timeout to 60sec just in case but same error:

15/01/2016 11:11:51: ModBus Message Sent
15/01/2016 11:11:51: Read TimeOut = 60000 Write TimeOut = 60000
15/01/2016 11:11:51: start read
15/01/2016 11:12:51 - ERROR Modbus Message to serial port ModBus: System.TimeoutException: The operation has timed out.
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count, Int32 timeout)
   at System.IO.Ports.SerialStream.Read(Byte[] array, Int32 offset, Int32 count)
   at System.IO.Ports.SerialPort.Read(Byte[] buffer, Int32 offset, Int32 count)
   at ProbReader.SensorReader.modbusReading(List`1 mm, Int32 spCounter)
15/01/2016 11:12:51: 0
15/01/2016 11:12:51: 0

我尝试了几种不同的读取串口的方法,我认为当前的方法看起来最好,就是我阅读代码中的while循环.

I have tried a few different ways of reading the serial port, I think the current method looks the best which is the while loop in my reading code.

我没有包含我的其余代码,因为它之前超时了,我认为它无关紧要.

I did not include the rest of my code as it times out before and I think it is irrelevant.

推荐答案

正如我在我的问题中提到的,我能够使用其他软件读取硬件,因此它需要是软件错误.在调查了所有可能的变量后,我可以在串行端口设置中操作,我想出了转动握手并让它始终被接受的想法.

As I have mentioned in my question I was able to read the hardware with other software so it needed to be a software error. After investigation all the possible variables I could manipulate at the serial port setting I have come up with the idea of turning of the handshake and letting it to be always accepted.

稍微挖掘一下,我的以下代码就会增加写入和读取时间.它解决了我的问题:

A little bit of digging around give my the following code with increasing the write and read time out. It solved my issue:

                            sp.ReadTimeout = 60000;
                            sp.WriteTimeout = 60000;

                            sp.DtrEnable = true;
                            sp.RtsEnable = true;
                            sp.Handshake = Handshake.None;

希望以后能帮到其他人,感谢大家的帮助和努力.

I hope it will helps for others in the future, and thanks for everyone help and effort.

这篇关于长电缆超时的串行通信超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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