maxime33
maxime33

Reputation: 13

Reading a null byte with PySerial

I send adc values from a PIC (microcontroller) to my PC from a com port. I use PySerial to read datas. But I have a problem when I try to read a null byte, it doesn't work.

if self.serial_com.inWaiting():
    val = self.serial_com.read()
else:
    print "no data"

When I send a null byte (0x00), he always displays "no data" so I can't read a null byte, why? I guess a null byte is not a data?

I'm using Python 2.7.9 and PySerial 2.7 on Windows

Upvotes: 1

Views: 1996

Answers (2)

maxime33
maxime33

Reputation: 13

Thanks jcoppens for you answer, To send values from the microcontroller (in C) : firstly I split my adc value into 2 bytes (because it's 16 bits value), for example :

short int Value = 512;
char value_tab[2];
char* ConvertValue = value_tab;
*ConvertValue++ = Value & 0x00ff; // lower byte
*ConvertValue++ = ((Value & 0xff00) >> 8); // upper byte

And I send the value_tab with functions provide by Microchip ( putUSBUSART(char *data, BYTE length) and CDCTxService() ). You can see these functions here :description code

I tried to send a null byte with a rs232 terminal and it displays something, so I don't know why pyserial can't read it.

Upvotes: 0

jcoppens
jcoppens

Reputation: 5440

From the PySerial documentation:

The port is set up for binary transmission. No NULL byte stripping, CR-LF translation etc. (which are many times enabled for POSIX.) This makes this module universally useful.

I would check the transmission routines at the other end. Are you using a string send routine? (in that case, the routine stops before the null. You have to use a character by character send routine which is transparent for special values. Such as write if you are working in C)

Upvotes: 1

Related Questions