Search code examples
pythoncavrpyserialusart

Weird b'\x' data while communication between avr and python serially


I'm trying to send and receive data between python and atmega128 avr c script. I am getting weird type of byte I don't understand.

I've tried to read data in python code but the results look kind of like b'\x00' b'\x06' b'\x9e' b'f' b'\x06'. What is wrong in my code?

Here is my main thread of atmega

unsigned char Message[]="Initialization Complete!"; 
unsigned char buff = 0;

MCU_init(); 
UART_init_with_INT();

uart_send_string(Message,25);
uart_send_byte('\n');
uart_send_byte('\r');

return 0;

This is my python script reading data

import serial

ser = serial.Serial('COM4', 115200)

while(True):
    print(ser.read())

#ser.write(b'hello test')
ser.close()

This is my actual weird result

b'\x86'
b'\x98'
b'\xf8'
b'\x9e'
b'\x86'
b'\x9e'
b'`'
b'f'
b'\x9e'
b'\x06'
b'\x06'
b'\x9e'
b'\x86'
b'\x9e'
b'\x98'
b'f'
b'\x06'
b'~'
b'\x86'
b'\x9e'
b'\xfe'
b'\x9e'
b'\xf8'
b'\x9e'
b'\x00'
b'\x98'
b'\x80'
b'\xe6'
b'\x9e'
b'\xe6'
b'\x9e'
b'\x00'
b'\x06'
b'\x9e'
b'f'
b'\x06'
b'~'
b'f'
b'f'
b'\x18'
b'\x06'
b'\xe6'
b'\x80'

However what I expect the output to be is "Initialization Complete!"

P.S. This is UART implementation

void uart_send_byte(unsigned char byte)
{
        while(!(UCSR1A&(1<<UDRE1)));
        UDR1 = byte;
}

void uart_send_string(unsigned char *str, unsigned char len)
{
        int i;
        for(i=0;i<len;i++) {
                if(!(*(str+i)))
                        break;
                uart_send_byte(*(str+i));
        }
}

Solution

  • Thanks everyone, I solved.

    I changed my python code as below and done! I had to setup port.

    import serial
    
    ser = serial.Serial(
        port='/COM4',
        baudrate=57600,
        parity=serial.PARITY_ODD,
        stopbits=serial.STOPBITS_TWO,
        bytesize=serial.SEVENBITS
    )
    
    while(True):
        print(ser.readline())
    
    ser.close()