简单的串行点对点通信协议 [英] Simple serial point-to-point communication protocol

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

问题描述

我需要一个简单的通信协议在两个设备(一台PC和微控制器)之间。 PC必须向micro发送一些命令和参数。微数据必须传输字节数组(来自传感器的数据)。



数据必须是 noise protected (除了奇偶校验,我想我需要一些其他数据校正方法)。



有没有任何标准的解决方案去做这个? (我只需要一个想法,而不是完整的解决方案)。



任何建议是赞赏。



编辑1。我没有决定是否有任何语法错误将 主/从 协议或双方可以启动通信。微电脑必须知道微处理器是否完成了任务并可以发送数据。如果数据准备就绪,或者当工作完成时,微型数据可以发送数据,它可以连续轮询微数据。我不知道哪个更好,更简单。



编辑2。 硬件和物理层协议。 由于 RS-232 C串行标准用于PC,我将使用异步通信。我将只使用RxD,TxD和GND信号。我不能使用额外的电线,因为微控制器AFAIK不支持它们。 BTW我正在使用 AVR ATmega128芯片。



所以我将使用固定波特率,8位数据,2位停止位,不进行奇偶校验(或用?)。



数据链路协议 。这是我最关心的问题。感谢您提出 HDLC PPP Modbus 协议。我会研究它。

解决方案

我会使用 HDLC 。过去我已经祝你好运了。我想点一下连接只是使用异步框架,并忘记所有其他控件的东西,因为它可能是过度的。



除了使用HDLC进行数据包的成帧。我格式化我的包,如下所示。这是如何使用802.11

  U8 cmd; 
U8 len;
u8 payload [len];

每个命令数据包的总大小为len +2



然后定义如

  #define TRIGGER_SENSOR 0x01 
#define SENSOR_RESPONSE 0x02
另外一个优点就是你可以添加新的命令,如果你正确设计你的解析器来忽略未定义的命令,那么你将有一些向后兼容性。



所以把它放在一起,数据包看起来如下。

  //总包长度减去标志len + 4 
U8 sflag; // 0x7e从HDLC
U8 cmd开始分组报文结束//告诉对方该怎么办
U8 len; // payload length
U8 payload [len]; //可以是零len
U16 crc;
U8 eflag; //框架结束标志

然后系统将监视串行流的标志0x7e,当它在那里你检查长度,看看它是否是pklen> = 4和pklen = len + 4,并且crc是有效的。注意不要仅仅依赖crc的小包,你会得到很多假阳性也检查长度。如果长度或crc不匹配,则仅重置长度和crc,并从解码新帧开始。如果是匹配,则将数据包复制到新的缓冲区,并传递给您的命令处理功能。当收到标志时,始终重设长度和crc。



对于您的命令处理函数,请抓住cmd和len,然后使用开关来处理每种类型的命令。我还要求某些事件发送一个响应,所以系统的行为就像一个事件驱动的远程过程调用。



所以例如传感器设备可以有一个定时器或回应命令阅读。然后将格式化数据包并将其发送到PC,并且PC将响应它接收到数据包。如果没有,那么传感器设备可以重新发送超时。



当您进行网络传输时,应将其设计为网络堆栈,如 OSI模块,如 Foredecker 点不要忘记物理层的东西。我的HDLC文章是数据链接层,而 RPC和命令处理是应用程序层


I need a simple communication protocol between two devices (a PC and a microcontroller). The PC must send some commands and parameters to the micro. The micro must transmit an array of bytes (data from sensor).

The data must be noise protected (besides parity checking, I think I need some other data correction method).

Is there any standard solution to do this? (I need only an idea, not the complete solution).

P.S. Any advice is appreciated. P.P.S Sorry for any grammar mistakes, I hope you understand.

Edit 1. I have not decided whether it will be master/slave protocol or both sides can initiate communication. The PC must know when micro have done a job and can send data. It can continuously poll the micro if data is ready, or the micro can send data, when a job is done. I don't know which is better and simpler.

Edit 2. Hardware and physical layer protocol. Since RS-232C serial standard used in the PC, I will use asynchronous communication. I will use only RxD, TxD and GND signals. I can't use additional wires because the microcontroller AFAIK doesn't support them. BTW I'm using the AVR ATmega128 chip.

So I will use fixed baud rate, 8 bits of data, 2 stop bits without parity checking (or with?).

Data link protocol. That's what my question primarily concerned about. Thanks for suggesting HDLC, PPP and Modbus protocols. I will research on it.

解决方案

I would use HDLC. I have had good luck with it in the past. I would for a point to point serial just use the Asynchronous framing and forget about all of the other control stuff as it would probably be overkill.

In addition to using HDLC for the framing of the packet. I format my packet like the following. This is how options are passed using 802.11

U8 cmd;
U8 len;
u8 payload[len];

The total size of each command packet is len +2

You then define commands like

#define TRIGGER_SENSOR 0x01
#define SENSOR_RESPONSE 0x02

The other advantage is that you can add new commands and if you design your parser correctly to ignore undefined commands then you will have some backwards compatibility.

So putting it all together the packet would look like the following.

 // total packet length minus flags len+4
 U8 sflag;   //0x7e start of packet end of packet flag from HDLC
 U8 cmd;     //tells the other side what to do.
 U8 len;     // payload length
 U8 payload[len];  // could be zero len
 U16 crc;
 U8 eflag;   //end of frame flag

The system will then monitor the serial stream for the flag 0x7e and when it is there you check the length to see if it is pklen >= 4 and pklen=len+4 and that the crc is valid. Note do not rely on just crc for small packets you will get a lot of false positives also check length. If the length or crc does not match just reset the length and crc and start with decoding the new frame. If it is a match then copy the packet to a new buffer and pass it to your command processing function. Always reset length and crc when a flag is received.

For your command processing function grab the cmd and len and then use a switch to handle each type of command. I also require that a certain events send a response so the system behaves like a remote procedure call that is event driven.

So for example the sensor device can have a timer or respond to a command to take a reading. It then would format a packet and send it to the PC and the PC would respond that it received the packet. If not then the sensor device could resend on a timeout.

Also when you are doing a network transfer you should design it as a network stack like the OSI modle as Foredecker points don't forget about the physical layer stuff. My post with the HDLC is the data link layer and the RPC and command handling is the Application Layer.

这篇关于简单的串行点对点通信协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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