I am having a bizarre issue with the serial output from my arduino board. I have a simple program (code below) that reads a dummy serial message from the computer and then sends a reply. It works fine with the Arduino monitor. But when I try to read it in with Processing it just read a null string. And when I try to do it with Python it will read the correct output twice. Also, when I try uploading new code onto the device there is often a timeout error, which can be fixed by unplugging and board and then re-plugging it. This was all working fine a while ago, and the board hasn't undergone any trauma so I don't think there is a physical problem. Any ideas? I'm really stumped here. Thanks for your help!
// Arduino code
// When I upload this and use the Serial monitor, putting in "000" and pressing enter
// makes "1000" print to the screen
void loop() {
if (Serial.available() ) {
char mybuffer[4];
int throw_away1 = Serial.readBytesUntil('\n', mybuffer, NUM_COMM_DIGITS);
Serial.println(1000);
}
}
// Processing code
// Prints out a constant string of "999", rather than "1000"
void draw() {
delay(400);
port.write("000\n");
int res;
String inString = port.readStringUntil('\n');
if (inString != null) res = int(trim(inString));
else res = 999;
println(res);
}
Python code
from serial import Serial
ser = Serial("/dev/tty.usbserial-A900H0WW")
ser.write("000\n")
ser.read() # prints '1000\r\n'
ser.read() # prints '1000\r\n' again
ser.read() # this time it hangs
Here are my entire Arduino and Processing programs. One obvious thing to tweak with is the baud rate (something I admittedly don't know a lot about), but it was working fine a couple weeks ago. Thanks!
Yes, it definitely is. Thank you by the way for your time and help! I'm inclined to suspect it's a problem with the serial port in general, since Processing and Python mess up in completely different ways on it. But for some reason the Arduino monitor is working fine.
Hello
I am very new to arduino...
I am trying to make processing and arduino to communicate through serial port and i tried to test your code! The problem is that i get other data numbers at processing output and other at arduino serial monitor. Take a look at the attachment. Any help apreciated