Go Down

Topic: Serial TX and RX loops in unix (Read 503 times) previous topic - next topic

goodkiller

Nov 23, 2012, 07:07 pm Last Edit: Nov 23, 2012, 07:11 pm by goodkiller Reason: 1
Hi,

Unfortunately i'm currently stucked one of my project and some myserious way arduino code does not work in unix or windows platform. Idea is that arduino send string "READ" every afer 5 seconds using SimpleTimer library and at the same time arduino listen serial communication - reads data from serial and prints out after newline char recieved. But somehow arduino code makes instant loop in unix or windows platform but in arduino serial monitor it works like a charm. Does anyone know where the problem may caused or why this kind loop occurred.

Thanks for your time and help  :.

Here is my Arduino code:
Code: [Select]

#include <SimpleTimer.h>

SimpleTimer t;

String inputString = "";
boolean stringComplete = false;

void setup()
{
 Serial.begin(9600);
 
 inputString.reserve(200);
 
 t.setInterval(5000, getData);
}

void loop()
{
 if(stringComplete)
 {
   Serial.println(inputString);
   
   inputString = "";
   stringComplete = false;
 }
 
 t.run();
}

void getData()
{
 Serial.println("READ");
}

void serialEvent()
{
 while (Serial.available())
 {
   char inChar = (char)Serial.read();
   
   inputString += inChar;

   if (inChar == '\n') {
     stringComplete = true;
   }
 }
}


Arduino serial monitor output:
Code: [Select]

READ
READ
READ
this is testing message from arduino serial monitor

READ


Unix output:
Code: [Select]

pi@raspberrypi /opt/local/server $ cat /dev/ttyACM0

EAD

READ

READ







READ

...

PaulS

First things, first. Ditch the String class. After you've done that, let us know if you still have a problem.

goodkiller

So if you suggest to not use String class then why Arduino tutorials also teaches incorrectly - http://www.arduino.cc/en/Tutorial/SerialEvent
I copied exact code and i tried into unix, and again it generates loop. If even tutorial lies then how someone else should learn Arduino programming :)
I also tried modified kind version of Arduino tutorial:
Code: [Select]

/*
  Serial Event example

When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.

A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.

Created 9 May 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/SerialEvent

*/

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
hardware serial RX.  This routine is run between each
time loop() runs, so using delay inside loop can delay
response.  Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
   
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == ';') {
      stringComplete = true;
    }
    else
    {
      // add it to the inputString:
      inputString += inChar;
    }
  }
}


Unix commands:
Code: [Select]

pi@raspberrypi ~ $ echo '12345;' > /dev/ttyACM0
pi@raspberrypi ~ $ echo '98765;' > /dev/ttyACM0
pi@raspberrypi ~ $ echo 'abcdef;' > /dev/ttyACM0


Results from unix:
Code: [Select]

pi@raspberrypi ~ $ cat /dev/ttyACM0
12345



12345



98765







12345







98765



abcdef





Seems like serial/com port remembers last output and prints it out with new string and enter also many newline characters. How this happens only in unix on windows plaform? When i'm testing with Arduino serial monitor then it works like a charm... Does unix serial configuration allows to remember last output etc. ?

PaulS

You seem to be assuming that the problem is with the Arduino. That the output appears correct when a PC receives the data from the serial port that the Arduino is connected to refutes that.

I think that the problem is with the RaspberryPi. I suggest that you ask what the Pi is doing wrong, on the RaspberryPi forum.

goodkiller

Thanks for your reply PaulS. I'm already getting closer for my problem. Hardware does not play a game here (Raspberry PI), problem is with unix, namely with how to read port. I'm currently using command "stty" after installind "minicom" or "ser2net" then it works like a charm. I think there must be some special flag to use stty command to get normal output. I'll keep digging and let all you know how to do it, or someone is already solved this problem?

goodkiller

So, i found now a solution for my problem. If anyone trying to communicate between arduino and some unix platform software then i suggest to use this command:

Code: [Select]

stty -F /dev/ttyACM0 -parenb cs8 cstopb cread ixon isig ispeed 9600; cat /dev/ttyACM0

Go Up