3

I am writing a byte to serial port using Python.

import serial

ser = serial.Serial ("/dev/ttyACM0")    
ser.baudrate = 115200                   
ser.write('\x57')
ser.close()  

When I connect TX to RX I have no problem to read that byte (sent from Python code), using GtkTerm. But when I am trying to read this data on micro controller using C, I always read 240. But when I use GtkTerm to send hexadecimal data directly (View -> Send Hexadecimal data), I read (on microcontroller) appropriate value. What could be wrong?

C code:

char byte = getc_();
printf_("1 byte received: i: %i \n",byte);

get_c() function:

char getc_()
{
    #ifdef LIB_MUTEX
    mutex_lock(&mutex_getc_);
    #endif

    char res = uart_read();

    #ifdef LIB_MUTEX
    mutex_unlock(&mutex_getc_);
    #endif

    return res;
}
3
  • 2
    Parity, baudrate, startbits, stopbits, flow control. Are you reading the byte using the same UART when reading it with GtkTerm, btw? It would explain why it works, because the UART should always be configured correctly to decode what it encoded. Commented Dec 1, 2015 at 17:43
  • 1
    Check the error flags for the microcontroller's UART. Hopefully there's one for overflow and one for framing. If the framing one is set, you either have a bad baudrate or a signal problem. If the overflow is set, you're sending too much data. If you have access to an oscilloscope, probe the TX line from the PC and see if its any different in the two cases - python vs gtkterm. Commented Dec 1, 2015 at 18:51
  • 1
    You may see some junk from the UART when you first start using it. Protocols that work over serial ports need to put some effort into synchronizing the two ends and can't just assume that the first byte in on one side will be the first byte out on the other. Commented Dec 1, 2015 at 19:45

2 Answers 2

1

Likely wrong communication rate. Change receiver/transmitting baud to maybe 1/4 or 1/6 -- Suggest 19200. (or speed up transmitting/reciver)

240 is 0xF0. With RS- 232, data is sent

Start bit - LS bit - next bit - ... -next bit - MS bit - Stop bit
// or 
0 - LS bit - next bit - ... -next bit - MS bit - 1
// or 0xF0
0 - 0 - 0 - 0 - 0 - 1 - 1 - 1 - 1 - 1

If the receiving end is seeing too many 0 bits (look at least significant bit first), and data received is in bit groups of 0's and 1's, it usually means data is sent at a slower baud than what the receiver is using.

Another clue to deciphering mis-match baud is what is the ratio of the count of bytes sent versus received. Given various bit patterns this is not a hard rule, but the side with more data is likely the one at the higher baud.

Sign up to request clarification or add additional context in comments.

Comments

0

After some time I find this solution. First byte I always receive 240, but with additional small sleeps I get the correct value which I sent.

import serial
import time
ser = serial.Serial (port = "/dev/ttyACM0", bytesize = 8, stopbits = 1)
ser.baudrate = 115200 
sleep_time = 0.05  
ser.write('\x41') #240
time.sleep(sleep_time)
ser.write('\x41') #right value A
time.sleep(sleep_time)
ser.write('\x41') #right value A
...
ser.close()  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.