Now that we understand the hardware part of the picture, let's dive right into the software part. We'll take a look at each part of the puzzle by defining a few of the common terms. Ever wondered what phrases like 9600-8-N-1 meant?
Do you use software-handshaking or hardware-handshaking at formal parties for a greeting? If you're not sure, read on!
现在我们已经了解了硬件部分,接下来让我们直接进入软件部分。我们将通过定义一些常见术语来了解这个迷题的每个部分。有没有想过像9600-8-N-1这样的一串字符是什么意思?
在正式的聚会上,你用软件握手还是硬件握手来表示问候?如果你不确定,请继续往下读!
- ASCII is a human-readable to computer-readable translation code. (i.e. each letter/number is translated to 1's and 0's) It's a 7-bit (a bit is a 1 or a 0) code, so we can translate 128 characters. (2^7 is 128) Character sets that use the 8th bit do exist but they are not true ASCII. Below is an ASCII chart showing its "human-readable" representation. We typically refer to the characters by using hexadecimal terminology. "0" is 30h, "5" is 35h, "E" is 45h, etc. (the "h" simple means hexadecimal)
- ASCII是一种把人类可读转换到计算机可读的翻译代码(例如,每个字母/数字被翻译成1和0),这是一个7位(1个位是一个1或者一个0)的代码,所以我们可以翻译128个字符(2^7 = 128)。使用8位的字符集的情况也存在,但它们不是真正的ASCII。下面是一个ASCII表,显示了它的“人类可读”的表示形式。我们通常使用十六进制术语来表示字符,如“0”是30h、“5”是35h、“E”是45h、等等(“h”的简单含义是十六进制)。
- start bit - In RS-232 the first thing we send is called a start bit. This start bit ("invented" during WW1 by Kleinschmidt) is a synchronizing bit added just before each character we are sending. This is considered a SPACE or negative voltage or a 0.
- 起始位 - 在RS-232中,我们发送的第一件东西叫做起始位。这个起始位(是Kleinschmidt在一战期间发明的)是在我们发送的每个字符之前添加的一个同步位。这被认为是一个空格或负电压或0。
- stop bit - The last thing we send is called a stop bit. This stop bit tells us that the last character was just sent. Think of it as an end-of -character bit. This is considered a MARK or positive voltage or a 1. The start and stop bits are commonly called framing bits because they surround the character we are sending.
- 停止位 - 我们发送的最后一个东西叫做停止位。这个停止位告诉我们最后一个字符刚刚被发送。可以把它看作是字符结束位。这被认为是一个标记或正电压或1。起始位和停止位通常被称为帧位,因为它们环绕着我们正在发送的字符。
- 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:send "E" (45h or 1000101b(inary)) In parity of None, the parity bit is always 0 so we send 10001010.In parity of Even we must have an Even number of 1's in our total character so the original character currently has 3 1's (1000101) therefore our parity bit we will add must be a 1. (10001011) Now we have an even number of 1's. In Odd parity we need an odd number of 1's. Since our original character already has an odd number of 1's (3 is an odd number, right?) our parity bit will be a 0. (10001010)During transmission, the sender calculates the parity bit and sends it. The receiver calculates parity for the 7-bit character and compares the result to the parity bit received. If the calculated and real parity bits don't match, an error occurred an we act appropriately. It's strange that this parity method is so popular. The reason is because it's only effective half the time. That is, parity checking can only find errors that effect an odd number of bits. If the error affected 2 or 4 or 6 bits the method is useless. Typically, errors are caused by noise which comes in bursts and rarely effects 1 bit. Block redundancy checks are used in other communication methods to prevent this.
- 奇偶校验位 - 因为大多数PLC和外部设备是面向字节的(8位=1字节),它似乎自然地按一个字节来处理数据。虽然ASCII是一个7位码,但很少以这种方式传输。通常,第8位被作为奇偶校验位进行错误检查。这种错误检查方法得名于奇偶校验的数学思想。(还记得整数的奇偶性质吗?)简而言之,奇偶性意味着所有的字符要么是奇数的1,要么是偶数的1。
- 奇偶性的常见类型有None、Even、Odd。(Mark和Space不是很常见,所以我就不讨论了)。参考下面的例子:
- 发送字符“E”(45h或1000101b(二进制))
- 当奇偶校验类型为None时,奇偶校验位总是0,所以我们发送10001010。
- 当奇偶校验类型为Even时,我们的总字符中必须有偶数个1,由于原始字符目前有3个1(1000101),因此我们要添加的奇偶校验位必须是1。(10001011)现在有偶数个1了。
- 当奇偶校验类型为Odd时,我们需要奇数个1。由于我们的原始字符中已经有一个奇数的1(3是一个奇数,对吧?)我们要添加的奇偶校验位将是一个0。(10001010)
- 在传输期间,发送方计算奇偶校验位并发送它,接收方为7位字符计算奇偶校验,并将结果与接收到的奇偶校验位进行比较。如果计算的奇偶校验位和实际的奇偶校验位不匹配,一个错误将发生,我们需要采取适当的行动。
- 奇怪的是,这种奇偶校验法竟如此流行,原因是它只有一半时间是有效,也就是说,奇偶校验只能发现影响奇数位的错误。如果错误影响2位或4位或6位,该方法是无用的。通常,错误是由突发的噪声引起的,很少影响1位。块冗余检查用于其他通信方法来防止这种情况。
- Baud rate - I'll perpetuate the incorrect meaning since it's most commonly used incorrectly. Think of baud rate as referring to the number of bits per second that are being transmitted. So 1200 means 1200 bits per second are being sent and 9600 means 9600 bits are being transmitted every second. Common values (speeds) are 1200, 2400, 4800, 9600, 19200, and 38400.
- 波特率 - 我将继续使用这个错误的含义,因为它是最常见的错误使用。把波特率想象为为每秒正在传输的位数,比如1200表示每秒发送1200个位,9600表示每秒传输9600个位。通用值(速度)有1200、2400、4800、9600、19200和38400。
- RS232 data format - (baud rate-data bits-parity-stop bits) This is the way the data format is typically specified. For example, 9600-8-N-1 means a baud rate of 9600, 8 data bits, parity of None, and 1 stop bit.
- RS232数据格式 - (波特率 - 数据位 – 奇偶校验位 - 停止位)这是数据格式通常指定的方式。例如,9600 - 8 - n - 1表示波特率为9600,数据位为8,奇偶校验位为None,停止位为1。
The picture below shows how data leaves the serial port for the character "E" (45h 100 0101b) and Even parity.
下面的图片显示了字符“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有很多知识需要吸收,所以我们可以随时重读这些内容。如果你花时间思考每一步,这并不难,也可以更容易地理解其他的通讯方式!
,