I have some devices which read an RFID tage and pass the serial number of the tag over the serial port.
It seems to me that it is "better" to use two bytes for each d开发者_StackOverflow中文版igit of the serial number, especially since some devices are sending a terminating 0x0D 0xA (CR/LF).
Now I find one device which is using one byte per digit, so to send "12" it does not send 0x31, 0x32, but rather 0x12. Which means that I can't distinguish between CR/LF and a real 0xA and 0xD. I ask and get some waffle about it not mattering as the strings are fixed length - so why bother with CR LF? And, just to be (in)consistent, they use two bytes each for CR/LF.
As you may have gathered, I am fairly new to this project and somewhat confused by the inconsistencies between different manufacturers' devices.
The manufacturer of this one is happy to change their firmware to accommodate me. Should I ask them to use two bytes for each digit?
Serial is a thing that never seemed to become standard. The way it transmits is a standard but what is sent is always up to the developers of the hardware/software. Most devices have some sort of packet of information. Usually goes something like this STARTBYTE, DATA, CHECKSUM and sumtimes ~CHECKSUM (the inverse of the checksum) and maybe a STOPBYTE. A checksum helps you ensure your data is valid, but with only a couple of bytes being sent its hard to do a checksum.
To answer your question. It is a fantastic idea to ensure the most accurate data possible. This means you don't want to even be able to confuse CR/LF with your tag id. You can either make sure its not both CR and LF programmatically - then its a valid id, or request they change the firmware. It seems that most companies like to send serial data as plain text. I do not prefer this as it is bulky and frankly a waste of performance (if you need speed in your process). For me it is easier to read each byte of the packet and interpret accordingly. You might even ask them to do something like this. 0xFF 0x00 DATA0 DATA1 0xFF . this uses 2 start bytes and a stop byte. It sends a little more data, but helps to ensure you are getting valid data. In your code you could check for 0xFF 0x00 and 0xFF at the end. If you do not get that, you are not on a valid packet.
If you don't want to use packets, you could always just check for the CR/LF together, I am hoping the companies that build the RFID tags would not use CR/LF as their id on one.
If you feel you want more clarity and don't want to change anything, I recommend talking to the manufacturer and asking for some example interface code, or advice on the best way to ensure accurate data. They should accommodate as you are the customer.
- Forming Data Packets Information
- Serial Communication Concepts
- Example of Serial Communication with a Device
- Very Good website on General Serial and Packet Interpretation with Code examples
If you need any further assistance please let me know.
Always include a byte that indicates how long the message is - either the entire message, or just the data area. It only costs one byte (assuming messages < 256 bytes long), and makes parsing the message much easier.
精彩评论