简体   繁体   English

串口通讯,数据接收

[英]Serial Port Communication, Data Receive

I'm trying to communicate with with external machine using serial port and what i did, Sending a command getting Positive Response then Sending ENQ and Here in output should show me the Final Response. 我正在尝试使用串行端口与外部计算机进行通信,我做了什么,发送命令获取正响应,然后发送ENQ,然后在输出中应向我显示最终响应。

First Problem is that the Output Result Repeating 3 Times and it's weird because im Not using any loop in program, 第一个问题是输出结果重复3次,这很奇怪,因为im在程序中未使用任何循环,

The Second Problem Is i want to Extract the Result to Calculating the Response BCC Im not sure how i can read from buffer and Extract from Buffer! 第二个问题是我想提取结果以计算响应BCC我不确定如何从缓冲区读取和从缓冲区提取!

When working with a serial communication port or with some other type of slow data transfer mechanism you must take into account that the CPU is probably much much faster than the data transfer through the much slower communications port. 当使用串行通信端口或某些其他类型的慢速数据传输机制时,必须考虑到CPU可能比通过慢速通信端口进行数据传输快得多。

I suggest that you take a look at this stack overflow parsing/formatting data from serial port - C# . 我建议您看看此堆栈溢出从串行端口C#解析/格式化数据

The second problem that you will have is to place onto a stream of bytes a structure that separates out the various messages that are in the byte stream. 您将要解决的第二个问题是在字节流上放置一个结构,该结构将字节流中的各种消息分开。 Normally the approach is to have a series of levels of software, a kind of protocol stack similar to the OSI Model so that different areas of the software deal with different aspects or parts of the communication problem. 通常,该方法是拥有一系列软件级别, 类似于OSI模型的一种协议栈,以便软件的不同区域处理通信问题的不同方面或部分。

With serial communication ports there is usually some kind of a protocol that specifies the starting indicator byte, the series of message bytes, and an ending indicator. 对于串行通信端口,通常存在某种协议,用于指定起始指示符字节,消息字节序列和结束指示符。

This protocol specifies the actual messages that are transferred so that the sender and the receiver can take the stream of bytes and chop it up into individual messages. 该协议指定了实际传输的消息,以便发送方和接收方可以获取字节流并将其切成单独的消息。 These individual messages are then provided to some other functions to actually parse through and perform some action. 然后将这些单独的消息提供给其他一些功能,以实际解析并执行某些操作。

Reading your source it appears that you need to rethink your approach to a more modular and more layered approach. 阅读您的资料后,您似乎需要重新考虑采用模块化和分层方法的方法。

For instance your port interface layer should be processing individual bytes in order to assemble the bytes into a message. 例如,您的端口接口层应处理单个字节,以便将字节组合成一条消息。 The approach that I would take for this would be to use a finite state machine approach (also see state machines - basic of computer science ). 为此,我将采用有限状态机方法(另请参阅状态机-计算机科学基础 )。 And I would use an observer pattern so that as messages are assembled, any object that wants the assembled messages would register as a listener and then be provided the assembled message. 而且我将使用观察者模式,以便在组装消息时,想要组装消息的任何对象都将注册为侦听器,然后提供组装消息。

So I would have an object that handles the communication port events. 因此,我将拥有一个处理通信端口事件的对象。 This object would read the stream of bytes from the communication port and then assemble byte arrays of individual messages from the stream. 该对象将从通信端口读取字节流,然后从该流中组装各个消息的字节数组。 I expect that this would be a singleton object to ensure that only one object is interacting with the communications port so this object would also have methods to write a message to the communications port. 我希望这将是一个单例对象,以确保只有一个对象与通信端口进行交互,因此该对象还将具有将消息写入通信端口的方法。

This object would also implement the observer pattern so that other objects could register for communication port messages. 该对象还将实现观察者模式,以便其他对象可以注册通信端口消息。 Once a complete message is received, the communication port object would then provide it to all of the registered listeners. 一旦收到完整的消息,通信端口对象便会将其提供给所有已注册的侦听器。

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

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