1

I am attempting to use pyserial to read the data from an arduino on Windows.

import serial

device      = 'COM3'
baud        = 9600

with serial.Serial(device,baud, timeout = 0) as serialPort:
    while True:
        line = serialPort.readline()
        line = line.decode("utf-8")
        if line:
            print(line)
void setup() {
  Serial.begin(9600);
}

void loop() {

  int x = 12;
  int y = 34;
  int z = 56;
  Serial.print(x);
  Serial.print(',');
  Serial.print(y);
  Serial.print(',');
  Serial.println(z);  

}

The arduino Serial monitor is outputting exactly what I expect.

12,34,56
12,34,56
12,34,56

The python script on the other hand is outputting:

1
2,34
,56



12,
34,5
6


1
2,34
,56



12,
34,5
6

I have tried delaying the output from the Arduino, I have tried making a buffer in the arduino code and only output the data when the buffer was full, thinking maybe python would have time to read it correctly.

I have see numerous people on this site and others make similar code and suggest it works fine, I however cannot get coherent data from python. Anyone know my issue?

1 Answer 1

1

Try to do like this

Python

import serial

device = 'COM3'
baud = 9600

with serial.Serial(device, baud) as port:
    while True:
        print(port.readline().decode("utf-8"))

Arduino

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

void loop() {
  int x = 12;
  int y = 34;
  int z = 56;
  Serial.println(x + ',' + y + ',' + z);  
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fixed! Thank you! The python code and arduino code work together from this comment, however, my original python script works as well as an FYI.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.