Pages: [1]   Go Down
Author Topic: Problem with serial output from arduino. Processing can't read it  (Read 1604 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// 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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm afraid that doesn't help.
Then, you have problems on the Processing side, too.

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It is probably better written code.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* Screen Shot 2014-06-07 at 10.22.26 AM (2).png (567.5 KB, 1280x1024 - viewed 11 times.)
Logged

Pages: [1]   Go Up
Jump to: