3

I have arduino uno. I am trying to send and receive serial data from arduino to python. I have usb to uart converter. I have connected its tx to rx of Arduino and rx to tx of Arduino and gnd is connected to gnd. I have connected this usb to my laptop where I have written below python script:

import serial

ser = serial.Serial(port='COM6')

res = ser.read(10)
print(bytes.hex(res))
data = b"\x7E\x7E\xAA\x03\xAB\x77"
ser.write(data)

ser.close()

In above code, I am first reading the serial data, once the data is received, I sent out some hex packets. Below is the Arduino code:

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  const byte message[] = {0xAA, 0xBB, 0x06, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03 };

  Serial.write(message, sizeof message);
  Serial.println("data sent");
  delay(1000);

  char buffer[6];
  if (Serial.available() > 0)
  {
    int size = Serial.readBytesUntil('\x77', buffer, 6);
    for (int i = 0 ; i < size ; i++) 
    {
      Serial.println(buffer[i], HEX);
    }
  }
}

In above code, I am first sending out the hex packets (which is received fine on python side) and then it waits for serial data. Once serial data is available to read, it reads it unitll \x77 and prints out all data. This is received on serial console of Arduino:

enter image description here

You will notice that it has received the data but there are some extra FF being received. Why have we recived these FF. Also in the python code, we know we are sending data of length 6 so accordingly we have defined char buffer[6] in arduino but what if we have to receive data which we do not what length it will be. Please help. Thanks

6
  • 2
    How did you configure the serial port on the Python side? It is not done in the Python code itself. Jun 28, 2023 at 12:29
  • @EdgarBonet Sorry I didn't get you by configuring the serial port. On the Python side, I have just mentioned the port number and thats it. About the hardware, its the same as mentioned in the question.
    – S Andrew
    Jun 28, 2023 at 17:27
  • 2
    You cannot connect another USB-to-Serial converter on the same rx and tx pins. Serial ports cannot work in "parallel". Define a software serial using any other digital pins and connect your converter on those pins. The hardware rx and tx pins are already connected to the on-board "serial-to-USB" converter.
    – Fahad
    Jun 28, 2023 at 18:27
  • 1
    @Fahad I know and this is why when uploading the code, I need to remove the jumper wires rfom tx and rx pins. Once code is uploaded, I just press reset to start it again. Is that okay?
    – S Andrew
    Jun 29, 2023 at 14:50
  • @SAndrew But you are still using the same pins for two serials! One is the onboard serial that you are watching on the Arduino IDE serial window (COM7) and one is in the python (COM6). That is a problem. Serials are not meant to be used as parallel buses.
    – Fahad
    Jun 30, 2023 at 1:32

1 Answer 1

4

The effect that you see is type promotion, in the C++ standard called "integral promotion", which happens implicitly. Values of type char are cast to int. Commonly, and in your case too, the char is by default signed.

The values 0xAA and 0xAB have a set most significant bit, which commonly designates a negative value in two's complement. This holds true for your case. And so these negative values are cast to the same negative values, but in full width of an int. In your case, an int has 32 bits.

Hex 0xAA as (by default signed) char is -86 decimal. And -86 decimal as (by definition signed) int is 0xFFFFFFAA hex.

Printed as hex, all bits are shown. So you see all these FF.

The solution is to mask only the interesting lower 8 bits:

    {
      Serial.println(buffer[i] & 0xFF, HEX);
    }
4
  • Good catch! Well, it's actually a bit more complicated: the char gets promoted to a 16-bit int by the overload resolution of Serial.println(), then explicitly casted to long by Print::print(int, int), then converted to unsigned long by the call to printNumber(). Jun 29, 2023 at 7:29
  • 1
    @EdgarBonet This issue happens a lot with beginners, and I had it many times already. :-D -- Thanks for going deep into the details! I feel the official Arduino documentation in the web as unsatisfactory, because I always miss the exact and complete prototypes. We should not have to look into the library source... Jun 29, 2023 at 9:15
  • Thanks for your answer. Any idea why some of the garbage and random values are being received? Thansk
    – S Andrew
    Jun 29, 2023 at 15:09
  • @SAndrew Which garbage and random values do you mean? I don't see any. Please post a new question if this is a separate issue, else edit your question. Jun 30, 2023 at 6:06

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.