lars2000
Posts: 7
Joined: Mon Apr 04, 2016 1:44 am

Reading Serial Data from Rx pin

Tue Aug 02, 2016 5:28 am

Hello, I am reading sensor data transmitted to the Rx pin via zigbee adapter. When using the code below I get alot of white space between actual data. My question is how do I view more controlled data (e.g. without white space)? I ultimately plan on processing the data, I'm really confused about how data should be transmitted over serial and processed. If I am transmitting say, one string every two seconds, should I only read data every two seconds at the receiver? There are too many variables for me, how is this type of thing normally handled?

Code: Select all

import serial

serialport = serial.Serial("/dev/serial0", 9600, timeout=0.5)

while True:
    try:
        command = serialport.read()
        print str(command)
    except:
        pass

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reading Serial Data from Rx pin

Tue Aug 02, 2016 7:42 am

You define the data format. There in no right or wrong way.

Personally I'd transmit data as plain text with new line delimiters. The plain text makes it easy to debug and you need a delimiter to allow you to detect message end and to recover in the case of errors. If your data is important you might want to consider adding a checksum to allow for the detection of corrupt messages.

Have a look at the NMEA sentences used by GPS to get an idea of a popular serial data format.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Reading Serial Data from Rx pin

Tue Aug 02, 2016 5:44 pm

Which sensor ?

Dave.
Apple say... Monkey do !!

lars2000
Posts: 7
Joined: Mon Apr 04, 2016 1:44 am

Re: Reading Serial Data from Rx pin

Tue Aug 02, 2016 9:19 pm

davef21370 wrote:Which sensor ?

Dave.
Well there will be a multi sensors, including ground moister, temperature, photo, etc.

Return to “Python”