2

I'm attempting to write data over a serial line to the arduino, however I don't want to use the arduino IDE but rather use the command line.

In order to do this I have the following commands:

$stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
$echo s123 > /dev/ttyACM0

The code on the receiving end checks whatever or not the input is s123 and if it is starts blinking. I have tested this using the arduino IDE and it worked, however this did not work.

The sketch looks like this:

    void loop(){
     if(Serial.available()==4){
       byte b1,b2,b3,b4; 
      b1=Serial.read();
      b2=Serial.read();
      b3=Serial.read();
      b4=Serial.read();     
        if(b1=='s'){
               digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
               delay(1000);               // wait for a second
               digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
               delay(1000);               // wait for a second
               digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
               Serial.println("the cow has landed");
          }
     }


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

the output of

stty -F /dev/ttyACM0 -a

was :

speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 0; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts
ignbrk -brkint -ignpar -parmrk inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl noflsh -xcase -tostop -echoprt -echoctl -echoke

Thank you

Thijser
  • 173
  • 10
  • can you post the sketch? – nkint Jan 05 '15 at 16:31
  • I have added the sketch – Thijser Jan 05 '15 at 16:39
  • Could you post the output of `stty -F /dev/ttyACM0 -a` after your own stty command? That could help see some potential config issues. – jfpoilpret Jan 05 '15 at 16:43
  • I have added the output – Thijser Jan 05 '15 at 16:55
  • 1
    Your code doesn't seem to have any way of getting back in sync if that is ever lost. And it may be that you inadvertently reset the arduino when you configure/open the serial port. If you start talking too soon thereafter, (as echo would) the first character(s) might be lost while in reset, leaving your sketch and host program out of sync. Try putting a "Hello World" in your setup() – Chris Stratton Jan 05 '15 at 17:34
  • If you made a sketch to endlessly print something, could you see it when you read from `/dev/ttyACM0`? – BrettAM Jan 05 '15 at 18:07
  • 1
    `if(Serial.available()==4)` is bad code. Unless there are exactly 4 characters it will do nothing. At the least it should be `>=4` but you really need to work out what you want it to do. Even if it worked it would print 1 line then do nothing. – Milliways Jan 05 '15 at 22:45

1 Answers1

0

This was fixed this morning after a reboot and upgrade command, still don't know which one of these fixed it but it seems to work now.

Ah figured it out we were using the 0 and 1 rx gates to communicate over serial rather then usb and as the used power supply turned out to be only 5V rather then 9-12V it didn't quite work as intended.

Thijser
  • 173
  • 10
  • 1
    This is not a solution, as your program remains faulty. At best you are momentarily lucking out - which is actually bad luck, because when a system is faulty, it's more helpful when it will cooperate by failing while you are paying attention to fixing it, and not some other time. – Chris Stratton Jan 06 '15 at 16:08