Rs-232通信(软件)/RS-232通信(软件)现在我们已经了解了硬件部分,让我们直接进入软件部分。我们将通过定义一些常用术语来看看这个难题的每个部分。想知道像9600-8-N-1这样的短语是什么意思吗?
在正式聚会上,你是用软件握手还是硬件握手来打招呼?如果你不确定,请继续阅读!
现在我们知道了硬件部分,让我们直接进入软件部分。我们将通过定义一些常用术语来理解这个难题的每一部分。有没有想过像9600-8-N-1这样的字符串是什么意思?
在正式聚会上,你用软件握手还是硬件握手来问候你?如果你不确定,请继续读下去!
ASCII是一个人可读到计算机可读的翻译代码。(即每个字母/数字被翻译成1和0)它是一个7位(一位是1或0)代码,所以我们可以翻译128个字符。(2^7是128)使用第8位的字符集确实存在,但它们不是真正的ASCII。下面是一个ASCII图表,显示其“人类可读”的表示。我们通常用十六进制术语来指代这些字符。' 0 '是30h,' 5 '是35h,' E '是45h,以此类推。(“h”简单的意思是十六进制)ASCII是一种将人类可读转换为计算机可读的翻译代码(例如,每个字母/数字被翻译为1和0)。它是一个7位代码(一位是1或0),所以我们可以翻译128个字符(2 ^ 7=128)。有使用8位字符集的情况,但它们不是真正的ASCII。下面是一个ASCII表,显示了它的“人类可读”表示。我们通常用十六进制的术语来表示字符,比如“0”是30h,“5”是35h,“e”是45h,以此类推(“h”的简单含义就是十六进制)。
start bit -在RS-232中,我们发送的第一个东西叫做起始位。这个起始位(由Kleinschmidt在WW1发明)是一个同步位,添加在我们发送的每个字符之前。这被认为是一个空白或负电压或0。起始位-in RS-232,我们发送的第一个东西叫做起始位。这个开始位(由Kleinschmidt在第一次世界大战期间发明)是在我们发送的每个字符之前添加的同步位。这被认为是一个空间或负电压或0。stop bit -我们发送的最后一个东西叫做停止位。这个停止位告诉我们最后一个字符刚刚被发送。把它想象成一个字符结束位。这被认为是一个标记或正电压或1。开始位和停止位通常称为成帧位,因为它们包围着要重新发送的字符。停止位-The:我们发送的最后一个东西叫做停止位。这个停止位告诉我们最后一个字符刚刚被发送。把它当成一个字符的结束位。这被认为是一个标记或正电压或1。开始位和停止位通常被称为帧位,因为它们包围着我们正在发送的字符。
rong>parity bit - Since most PLCs/external equipment are byte-oriented (8 bits=1byte) it seems natural to handle data as a byte. Although ASCII is a 7-bit code it is rarely transmitted that way. Typically, the 8th bit is used as a parity bit for error checking. This method of error checking gets its name from the math idea of parity. (remember the odd-even property of integers? I didn't think so.) In simple terms, parity means that all characters will either have an odd number of 1's or an even number of 1's. Common forms of parity are None, Even, and Odd. (Mark and Space aren't very common so I won't discuss them). Consider these examples:下面的图片显示了字符“E”(45h 100 0101b)这个数据和偶数校验位是如何离开串口的:
Another important thing that is sometimes used is called software handshaking (flow control). Like the hardware handshaking we saw in the previous chapter, software handshaking is used to make sure both devices are ready to send/receive data. The most popular "character flow control" is called XON/XOFF. It's very simple to understand. Simply put, the receiver sends the XOFF character when it wants the transmitter to pause sending data. When it's ready to receive data again, it sends the transmitter the XON character. XOFF is sometimes referred to as the holdoff character and XON as the release character.
另一个重要的东西,有时被用来称为软件握手(流控制)。就像我们在前一章看到的硬件握手,软件握手是用来确保两个设备都准备好发送或者接收数据。最流行的“字符流控制”称为XON / XOFF,这很容易理解,简单地说,当接收方希望发送方暂停发送数据时,接收方发出XOFF字符,当它准备再次接收数据时,它向发送方发送XON字符。XOFF有时被称为拖延字符,XON被称为释放字符
The last thing we should know about is delimiters. A delimiter is simply added to the end of a message to tell the receiver to process the data it has received. The most common is the CR or the CR and LF pair. The CR (carriage return) is like the old typewriters. (remember them??) When you reached the end of a line while typing, a bell would sound. You would then grab the handle and move the carriage back to the start. In other words, you returned the carriage to the beginning. (This is the same as what a CR delimiter will do if you view it on a computer screen.) The plc/external device receives this and knows to take the data from its buffer. (where the data is stored temporarily before being processed) An LF (line feed) is also sometimes sent with the CR character. If viewed on a computer screen this would look like what happens on the typewriter when the carriage is returned and the page moves down a line so you don't type over what you just typed.
最后我们应该知道的是分隔符。只需要在消息的末尾添加一个分隔符,就可以告诉接收方可以处理它接收到的数据了,最常见的是CR或CR+LF。CR(回车)就像老式的打字机(记得吗?),当你打字打到一行的末尾时,铃声会响起,然后,你可以抓住把手,把打字头移回起点。换句话说,你让马车回到了起点(这与在计算机屏幕上查看CR分隔符的作用相同)。PLC或外部设备接收这个分隔符,并知道从它的缓冲区取数据(数据在处理前暂时存储)。LF(换行)有时也与CR字符一起发送。如果在电脑屏幕上看,这看起来就像在打字机上发生的情况,当回车时,页面向下移动一行,这样您就不会在刚刚键入的内容上重叠打字了。
Sometimes an STX and ETX pair is used for transmission/reception as well. STX is "start of text" and ETX is "end of text". The STX is sent before the data and tells the external device that data is coming. After all the data has been sent, an ETX character is sent.
有时,STX和ETX也用于传输或接收。STX是“文本的开始”,ETX是“文本的结束”。STX在数据之前发送,并告诉外部设备数据即将到来。所有数据发送完毕后,将发送一个ETX字符。
Finally, we might also come across an ACK/NAK pair. This is rarely used but it should be noted as well. Essentially, the transmitter sends its data. If the receiver gets it without error, it sends back an ACK character. If there was an error, the receiver sends back a NAK character and the transmitter resends the data.
最后,我们可能还会遇到ACK / NAK。这很少使用,但也应该注意。本质上,发送器发送数据,如果接收方接收时没有发生错误,它将返回一个ACK字符;如果有一个错误,接收方发送回一个NAK字符并且发送方会重新发送数据。
RS-232 has a lot of information to absorb, so feel free to reread it. It's not so difficult if you take your time and think about each step. Other communication methods get easier!
关于RS-232有很多知识需要吸收,所以我们可以随时重读这些内容。如果你花时间思考每一步,这并不难,也可以更容易地理解其他的通讯方式!