且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

快速读取和解析连续数据的方法

更新时间:2023-02-10 10:04:36

一种非常简单的成功方法是停止尝试使其更快.毫无疑问,串行端口数据速率非常非常低,而现代计算机却非常非常快.您的 Read()调用仅返回一个字节,很少返回2.

A very simple way to get ahead is to stop trying to make it faster. There is no point, serial port data rates are very, very low and modern computers are very, very fast. Your Read() call only ever returns a single byte, rarely 2.

请注意,这很难看到,当您调试并单步执行代码时,您会人为地减慢程序速度.允许接收更多字节,并因此通过 Read()调用返回更多字节.但是,当程序以正常速度运行时,不会发生这种情况.

Note that this is hard to see, when you debug and single-step through the code then you'll artificially slow down your program a great deal. Allowing more bytes to be received and thus more of them getting returned by the Read() call. But this doesn't happen when the program runs at normal speed.

因此,请改用 SerialPort.BaseStream.ReadByte().使代码非常简单.

So use SerialPort.BaseStream.ReadByte() instead. Makes the code very simple.