简体   繁体   English

从串行端口读取所有数据,但传入流没有一致的流尾

[英]Reading all data from serial port, but incoming stream doesn't have a consistent end of stream

I'm trying to reverse engineer an old DOS CNC program and I've had limited success. 我正在尝试对旧的DOS CNC程序进行逆向工程,但我取得的成功有限。 I sniffed the Rx/Tx stream from the DOS program and saw this... 我从DOS程序中嗅探Rx / Tx流,并看到了...

Tx from DOS: 从DOS发送:

"OUTPUT TEST" 0D 

Rx from machine: 来自机器的接收:

"OUTPUT TEST" 0D 0D 0A 
0D 0A 
"TEST" 0D 0A 
"0001>  +09000  +09000  +00000  +00000  +00000  +00032  +00000  D  O  " 0D 0A 
"0002>  +00000  +03000  +00000  +00000  +00000  +00032  +00000  B  N  " 0D 0A 
"0003>  +00000  +03000  +00000  +00000  +00000  +00032  +00000  B  N  " 0D 0A 
"0004>  +00000  +03000  +00000  +00000  +00000  +00032  +00000  B  N  " 0D 0A 
"0005>  +00000  +03000  +00000  +00000  +00000  +00033  +00000  B  N  " 0D 0A 
"0006>  +00000  +03000  +00000  +00000  +00000  +00033  +00000  B  N  " 0D 0A 
"0007>  +00000  +03000  +00000  +00000  +00000  +00033  +00000  B  N  " 0D 0A 
"0008>  +00000  +03000  +00000  +00000  +00000  +00033  +00000  B  N  " 0D 0A 
"0009>  +00000  +03000  +00000  +00000  +00000  +00033  +00000  B  N  " 0D 0A 
"0010>  +09000  +17000  +00000  +00000  +00000  +00064  +00000  B  X  .  " 0D 0A 
14 0D 0A 
"PIN>  " 

Of course, I had to reformat the stream so it's readable. 当然,我必须重新格式化流以使其可读。

As you can see, the last line does not have a line feed. 正如你所看到的,最后一行没有换行符。 This makes ReadLine useless, as the last line will only show up on the next request. 这使ReadLine无效,因为最后一行仅在下一个请求中显示。 I need to be able to send a command and read to the end. 我需要能够发送命令并阅读到最后。 The only way I've been able to achieve this is by sleeping long enough for the transmission to complete on the machine then process the data on the PC. 我能够做到这一点的唯一方法是,使计算机休眠足够长的时间,以便在计算机上完成传输,然后在PC上处理数据。 On some machines, the delay has to be set as high as 5 seconds. 在某些计算机上,延迟必须设置为5秒。 I'd hate to do it this way, but it works. 我不愿意这样做,但它确实有效。

While spUISC.BytesToRead > 0
    buffer &= spUISC.ReadExisting
    Threading.Thread.Sleep(100)
End While

Summary 摘要

How can I tell if an incoming stream is done if it doesn't have a terminating char? 如果没有终止字符,如何判断输入流是否完成?

I personally would modify the loop to do the following: 我个人将修改循环以执行以下操作:

  1. Read available bytes into the buffer 将可用字节读入缓冲区
  2. Attempt to validate that the buffer has a valid response 尝试验证缓冲区具有有效响应
  3. If yes on #2, consume the buffer and exit the loop. 如果在#2上为是,则消耗缓冲区并退出循环。 Else keep waiting for more data. 否则继续等待更多数据。

If you can perform a blocking read, there is no need to sleep at all. 如果您可以执行阻止读取,则根本不需要睡眠。 You'll just keep looping until you get a valid frame. 您将一直循环播放,直到获得有效的帧为止。 Obviously, other error handling may be required. 显然,可能需要其他错误处理。

Do a blocking read but you'll need to process by character, not line. 进行阻塞阅读,但您需要按字符而不是行进行处理。

"0D 0A PIN> " appears to be a prompt (Process INput?). 出现提示“ 0D 0A PIN>”(进程输入?)。 Don't read until end-of-line, read until you see that prompt (recognize it with a regular expression) and process the bytes preceding the prompt. 直到行尾才读,直到看到提示(用正则表达式识别)并处理提示之前的字节,然后阅读。 It appears to be echoing the command characters you send it so after checking discard those characters, leaving a clean response. 它似乎在回显您发送的命令字符,因此在选中后丢弃这些字符,从而留下清晰的响应。

To reverse engineer hook up the test box to a terminal program and experiment by hand with commands to that prompt. 要进行反向工程,请将测试框连接到终端程序,然后手动尝试该提示符下的命令。 Commands might be terminated with 0D, 0A or both. 命令可能以0D,0A或两者都终止。 Eg A blank command, "HELP", "?" 例如,空命令“ HELP”,“?” or an invalid command might give useful information. 否则无效的命令可能会提供有用的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM