使用C#接收串行端口数据时出现问题 [英] Problem with serial port data receive in C#

查看:78
本文介绍了使用C#接收串行端口数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#程序有疑问.通过串行端口,我收到一个大约110个字符的大字符串.这部分工作正常,但是当我添加代码以分割字符串时,在某些行之后出现错误.这是我得到的错误:

I have a problem with a C# program. Through the Serial port i Receive a large string about 110 characters. This part works ok, but when i add code to split the string up i receive an error after some rows. Here is the error I get:

** mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的未处理异常

**An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

其他信息:索引和长度必须引用字符串中的位置.**

Additional information: Index and length must refer to a location within the string.**

这是代码:

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
      if (!comport.IsOpen) return;

      if (CurrentDataMode == DataMode.Text)
      {
        // Read all the data waiting in the buffer
        string data = comport.ReadExisting();

        Log(LogMsgType.Incoming, data);
        string ziua = data.Substring(0, 8);
        string ora = data.Substring(8, 8);
        string linie = data.Substring(18, 1);
        string interior = data.Substring(22, 3);
        string durata1 = data.Substring(26, 4);
        string durata2 = data.Substring(30, 8);
        string nrtel = data.Substring(38, 10);
        string tipapel = data.Substring(75, 1);
        string acct = data.Substring(76, 5);


      }
      else
      {
        int bytes = comport.BytesToRead;

        byte[] buffer = new byte[bytes];

        comport.Read(buffer, 0, bytes);

        Log(LogMsgType.Incoming, ByteArrayToHexString(buffer));
      }
    }

我已经测试了每个子串,并且都可以.字符串长度为112.不能短.

i've tested every substring and all of them are ok. the string lenght is 112. it can't be to short.

此错误出现在几行112 ...大约一半之后

this error appears after a few lines of 112... about one and a half

推荐答案

这是串行端口的典型行为.他们很慢.当DataReceived事件触发时,您通常只会得到一个或两个字符.值得注意的是,它在调试时效果很好,因为单步执行代码会使串行端口有很多时间来接收其他字符.但是,如果字符串没有足够长的时间,它将在没有调试器的情况下立即运行Kaboom.

This is typical behavior for a serial port. They are very slow. When the DataReceived event fires, you'd typically only get one or two characters. Notably is that it works well when you debug because single-stepping through the code gives the serial port lots of time to receive additional characters. But it will go Kaboom as soon as you run without a debugger because the string isn't long enough.

您需要通过将收到的字符串附加到类作用域的字符串变量中来修改代码.仅在收到所需的所有字符后才解析字符串.您需要某种方式来知道您已收到完整的回复.最典型的串行设备将以特殊字符终止字符串.经常换行.

You'll need to modify the code by appending the string you receive to a string variable at class scope. Only parse the string after you've received all the characters you expected. You'll need some way to know that you've received the full response. Most typically serial devices will terminate the string with a special character. Often a line-feed.

如果是这种情况,则可以通过将SerialPort.NewLine属性设置为该终止符并调用ReadLine()而不是ReadExisting()来简化操作.

If that's the case then you can make it easy by setting the SerialPort.NewLine property to that terminator and calling ReadLine() instead of ReadExisting().

这篇关于使用C#接收串行端口数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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