ravisurdhar
Posts: 7
Joined: Sat Mar 22, 2014 1:32 am

How to implement binary serial communication?

Sun Mar 23, 2014 1:44 am

I'm familiar with serial communication from my Arduino experience, but every sensor I've ever used before just dumped all its data in comma-separated ASCII over the serial port. However, I'd like to use a sensor like this: http://www.chrobotics.com/docs/GP9Datasheet.pdf

From the data sheet:
The GP9 UART operates at a 3.3V logic level with 8 data bits, 1 stop bit, and no parity. While the logic level is 3.3V, the pins are 5V tolerant to simplify integration with 5V systems.
Excellent! So I can just wire the TX and RX pins directly to my Pi, right? Only problem is, now the data sheet delves into all these "registers", and I have no idea how to get data out of them.
Data transmitted and received by the GP9 is formatted into packets containing:
1. The three character start sequence 's', 'n', 'p' to indicate the start of a new packet (i.e. start new p acket)
2. A "packet type" (PT) byte describing the function and length of the packet
3. An address byte indicating the address of the register or command
4. A sequence of data bytes, the length of which is specified in the PT byte
5. A two-byte checksum for error-detection
How do I actually get data from this thing? Some example code would be SUPER helpful. Thank you!

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: How to implement binary serial communication?

Sun Mar 23, 2014 8:56 am

Just before what you have quoted it says -
By default, the serial baud rate
of the
GP9
is set at 115200, but the baud rate can be changed by the end
user if desired
And just after what you have quoted there are 3 tables which show as a picture what the description says.

If you just set up a terminal to read data from the serial port my guess is that you will get a stream of characters in which the character sequence 's' 'n' 'p' appears at regular intervals. Once you are confident the data is arriving you can parse or decode it using a python script that reads from the serial port. (There is a library for accessing serial port data.)
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

ravisurdhar
Posts: 7
Joined: Sat Mar 22, 2014 1:32 am

Re: How to implement binary serial communication?

Sun Mar 23, 2014 3:55 pm

I guess the part that's confusing me is the 50 pages of register address that come after that. If all the data from all of the registers is just being transmitted sequentially over the serial port, why list all those register addresses?

User avatar
Burngate
Posts: 6313
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: How to implement binary serial communication?

Sun Mar 23, 2014 5:53 pm

ravisurdhar wrote:I guess the part that's confusing me is the 50 pages of register address that come after that. If all the data from all of the registers is just being transmitted sequentially over the serial port, why list all those register addresses?
Because It's a super-complicated device?

I've not read all the way through the pdf datasheet, but what seems to happen is
- you tell it what data you want
- it replies with the data

What you transmit to it is
- 0x73
- 0x6E
- 0x70
- ?? (packet tyoe)
- - - - - and so-on

Just 'cos the sheet says 's', 'n', 'p', doesn't mean the device understands ASCII
It really should say 0x73, 0x6E, 0x70 - which was chosen as a start-sequence because it happens to be 'snp' in ASCII
The next byte in all probability won't translate into anything meaningful, because it's just a set of bits, and the same goes for the addess and data bytes - they're just numbers, so why translate the first three bytes into ASCII?

ravisurdhar
Posts: 7
Joined: Sat Mar 22, 2014 1:32 am

Re: How to implement binary serial communication?

Sun Mar 23, 2014 6:00 pm

Right, that's more along the lines of what I expected, but DeeJay was talking about the device just sending a stream of data over the serial port, as opposed to having to send it read commands for each register you want to read, and then it sends you the requested data back. My question is, how do I actually send it those hex values over the serial port, and read the subsequent data? I'm more familiar with the "constant stream of ASCII data" type devices.

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: How to implement binary serial communication?

Sun Mar 23, 2014 6:11 pm

I don't think any of us knows more than is in the documentation. I assumed you were getting some data but
didn't know how to interpret it. It looks like a Command and Response protocol, not a steady stream of data.
To initiate a serial read of one or more registers aboard the sensor, a packet should be sent to the
GP9 with the "Has Data" bit cleared. This tells the device that this will be a read operation from the address
specified in the packet's "Address" byte. If the "Is Batch" bit is set, then the packet will trigger a batch
read in which the "Address" byte specifies the address of the first register to be read.
In response to a read packet, the GP9 will send a packet in which the "Has Data" bit is set, and the "Is
Batch" and "Batch Length" bits are equivalent to those of the packet that triggered the read operation.
The register data will be contained in the "Data Bytes" section of the packet.
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

ravisurdhar
Posts: 7
Joined: Sat Mar 22, 2014 1:32 am

Re: How to implement binary serial communication?

Sun Mar 23, 2014 6:31 pm

No, I haven't bought the device yet...since it's $400, I'm determining if I can work with it before I buy it!

Return to “Python”