Go Down

Topic: Problem with serial output from arduino. Processing can't read it (Read 2036 times) previous topic - next topic

fcady

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

PaulS

I suggest that you head over to http://snippets-r-us.com to get help with your snippets. Or, post ALL of your code (using the # icon above).

fcady

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!

// Arduino
void setup() {}
void loop() {
  if (Serial.available() ) {
    char mybuffer[4];
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);
    Serial.println(1000);
  }
}

// Processing
import processing.serial.*;
Serial port;
void setup() {
  port = new Serial(this, Serial.list()[0], 9600);
}
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);
}

PaulS

Code: [Select]
// Arduino
void setup() {}
void loop() {
  if (Serial.available() ) {
    char mybuffer[4];
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);
    Serial.println(1000);
  }
}

Serail.available(), Serial.read(), and Serial.print() without Serial.begin() won't work. No wonder nothing gets to/from Processing.

fcady

I'm afraid that doesn't help.  Here is the new code.  Note that it matches the baud rate in Processing.

// Arduino code
void setup() {
  Serial.begin(9600);
}
void loop() {
  if (Serial.available() ) {
    char mybuffer[4];
    int throw_away1 = Serial.readBytesUntil('\n', mybuffer, 4);
    Serial.println(1000);
  }
}

PaulS

Quote
I'm afraid that doesn't help.

Then, you have problems on the Processing side, too.

Code: [Select]
  port = new Serial(this, Serial.list()[0], 9600);
Are you certain that the Arduino is on the first port in the list?

fcady

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.

zoomkat

Quote
But for some reason the Arduino monitor is working fine.


It is probably better written code.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

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

Go Up