Australia
Offline
Newbie
Karma: 0
Posts: 22
:-]
|
 |
« on: May 13, 2011, 09:27:23 pm » |
Dear all, I am trying to send a command from a bash shell to my Arduino Uno. I have the following code running on the Arduino: int LedPin = 13; int val = 0; void setup() { Serial.begin(9600); pinMode(LedPin, OUTPUT); }
// Read Value from serial and set a led high or low void loop () { val = Serial.read(); if (val == '0') { digitalWrite(LedPin,LOW); } if (val == '1') { digitalWrite(LedPin,HIGH); } } From bash, I can set the led high or low using screen and then press 0 or 1: virtualmix@bluedragon:~$ screen /dev/ttyACM0 I would like to set the led high or low using echo: echo 1 > /dev/ttyACM0 But it doesn't work. Am I doing it wrong? Is there any known issues using the echo command, or anything I have missed out? Any help appreciated :-) Thank you!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35502
Seattle, WA USA
|
 |
« Reply #1 on: May 14, 2011, 07:53:10 am » |
I would guess that screen opens the serial port once, and keeps it open, and that echo opens the port, sends the data, and closes the port.
Opening and closing the serial port causes the Arduino to reset. The echo command sends the data before the Arduino is ready to read it, so it gets lost. The immediate reset causes the action, even if the data was received to not be executed.
There are ways to modify the Arduino to prevent a reset on the opening of the serial port.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: May 14, 2011, 11:53:39 am » |
Your arduino code does not check if there is a character on the serial port, check - http://www.arduino.cc/en/Serial/Available#DEFINE LEDPIN 13 char val;
void setup() { Serial.begin(9600); pinMode(LEDPIN , OUTPUT); }
void loop () { if (Serial.available() > 0) { val = Serial.read(); if (val == '0') { digitalWrite(LEDPIN ,LOW); } if (val == '1') { digitalWrite(LEDPIN ,HIGH); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Portugal
Offline
God Member
Karma: 9
Posts: 752
Tomorrow I will know a BIT more than yesterday
|
 |
« Reply #3 on: May 14, 2011, 04:14:48 pm » |
Hello since you are using linux why not try gtkterm if you have graphic envirement ofcorse to test it. If not you should this example to configure the port stty -F /dev/ttyS0 ispeed 9600 ospeed 9600 -ignpar cs8 -cstopb -echo
In my case 9600 baud, no parity, one stop bit.
Then wrote the data you need to send into a file (using hexedit) and send the file's contents:
cat commando0.dat > /dev/ttyS0 Also check if you port is ttyS0!!
|
|
|
|
|
Logged
|
Debian,Mint,Ubuntu Arduino Mega 2560 Arduino Nano Arduino Duemilanove MAC OS Montain Lion Raspberry PI Model B
|
|
|
|
Australia
Offline
Newbie
Karma: 0
Posts: 22
:-]
|
 |
« Reply #4 on: May 20, 2011, 08:54:49 am » |
Thank you so much to all of you for your reply. Hugo007 was right, the problem is due to the serial port being closed after each connexion and the Arduino restarting every time because of its auto reset feature. Anyway, I have tried a few different solutions and investigate this issue a bit further but it does not seem to have an elegant solution. So far, the best solution I have found, but not tested, is to plug a 120 Ohm resistor between 5V and reset pin. I couldn't find any "software solution" that successfully disabled the auto reset feature. Also, I played a bit with gtkterm and it seemed similar to "screen" in some ways but does not satisfy my need to send command directly from a bash script. Thanks again for your support and if there is anything new, I am still open to discuss this issue. -Virtualmix-
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #5 on: May 20, 2011, 08:59:48 am » |
There is a hardware solution - turn hardware flow control off with stty, and it won't toggle the DTR pin, thus avoiding reset.
|
|
|
|
|
Logged
|
|
|
|
|
Australia
Offline
Newbie
Karma: 0
Posts: 22
:-]
|
 |
« Reply #6 on: May 21, 2011, 12:48:21 pm » |
@Aeturnalus: Thank you for your reply. Do you know the exact command?
So far I tried several stty command without success... I have spend about 4 hours to try do this and I am now about to give up my project...
|
|
|
|
« Last Edit: May 21, 2011, 12:52:31 pm by virtualmix »
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #7 on: May 21, 2011, 02:46:13 pm » |
Try this: stty -F /dev/ttyUSB0 -hupcl
Seems to work on my computer.
|
|
|
|
|
Logged
|
|
|
|
|
Australia
Offline
Newbie
Karma: 0
Posts: 22
:-]
|
 |
« Reply #8 on: May 21, 2011, 09:22:38 pm » |
Thanks Aeturnalus. I tried this command in many different ways but it doesn't disable auto reset feature on my Arduino. virtualmix@computer:~$ stty -F /dev/ttyACM0 -hupcl virtualmix@computer:~$ echo 1 >/dev/ttyACM0
The board keep reseting :-/
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #9 on: May 21, 2011, 10:06:12 pm » |
Strange - it seemed to work for me. Have you tried running it as root?
|
|
|
|
|
Logged
|
|
|
|
|
Australia
Offline
Newbie
Karma: 0
Posts: 22
:-]
|
 |
« Reply #10 on: May 21, 2011, 10:12:09 pm » |
virtualmix@computer:~$ sudo su [sudo] password for virtualmix: root@computer:/home/virtualmix# stty -F /dev/ttyACM0 -hupcl root@computer:/home/virtualmix# echo 1 >/dev/ttyACM0 The board still reset... So I tried to change the permissions: root@computer:/home/virtualmix# chmod 777 /dev/ttyACM0 root@computer:/home/virtualmix# stty -F /dev/ttyACM0 -hupcl root@computer:/home/virtualmix# echo 1 >/dev/ttyACM0 But it did not work, my Arduino still reset.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 0
Posts: 360
Arduino rocks
|
 |
« Reply #11 on: May 21, 2011, 10:15:39 pm » |
The stty command disables hangup - that is, it keeps the serial connection open. Have you tried seeing what happens if you do this: ~$ sudo stty -F /dev/ttyACM0 -hupcl ~$ echo 1 > /dev/ttyACM0 ~$ echo 1 > /dev/ttyACM0
Does it reset twice, or just once? On my board, the first echo command resets, but the second one doesn't.
|
|
|
|
|
Logged
|
|
|
|
|
Australia
Offline
Newbie
Karma: 0
Posts: 22
:-]
|
 |
« Reply #12 on: May 21, 2011, 10:49:44 pm » |
Thanks for for help. I did what you suggested but the board still reset.
|
|
|
|
|
Logged
|
|
|
|
|
Australia
Offline
Newbie
Karma: 0
Posts: 22
:-]
|
 |
« Reply #13 on: May 22, 2011, 03:55:26 am » |
Sorry I didn't read your previous post properly: The board reset twice.
|
|
|
|
|
Logged
|
|
|
|
|
|