I wrote Arduino sketch and it works. It waits for a command and answers text on response.
Simultaneously, I can't get any response with command line tools.
For example, I have the following code to get temperature:
#!/bin/bash
tty=/dev/ttyUSB0
exec 4<$tty 5>$tty
stty -F $tty cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
#stty -F $tty 9600 -echo
#printf "temp\n" >&5
echo temp >&5
read reply <&4
echo "$reply"
#tail -n 1 - <&4
It returns empty or hang under different circumstances.
Also, if I run built-in Serial Monitor
, I can see responses on command lines like:
echo temp > /dev/ttyUSB0
But I see nothing with commands like tail -f /dev/ttyUSB0
What I am doing wrong?
-
I really want to say using Linux is where you going wrong, but that would be childish. :) I'm not sure if I read it right, but are you trying to connect two apps to the same console port? I wouldn't expect that to work, but I only use Windows, does that work on Linux?Code Gorilla– Code Gorilla2015年10月12日 19:41:52 +00:00Commented Oct 12, 2015 at 19:41
1 Answer 1
Add the flag -hupcl
to your stty
command. That will disable the assertion of DTR which is resetting the board.
hupcl (-hupcl)
Stop asserting modem control (do not stop asserting modem
control) on last close.
The first time it is run there will be a reset, since setting that flag entails opening the port, which causes a reset. So you need to add a delay long enough to get past the bootloader - minimum 2 seconds, best make it 3.
You can do that with the sleep
command: sleep 3
Note that you now won't be able to upload a new sketch since you have disabled the auto-reset feature that allows you to enter the bootloader, so before uploading a new sketch you need to turn the reset back on with
stty -F $tty hupcl
If you don't disable the reset then every operation in your script that opens and then closes the serial port will trigger a reset of the board. So in summary try this:
#!/bin/bash
tty=/dev/ttyUSB0
stty -F $tty cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts -hupcl
sleep 3
echo temp >$tty
read reply <$tty
echo "$reply"
stty -F $tty hupcl
-
I was wishing to use reset feature. Why doesn't it work with it?Dims– Dims2015年10月12日 19:44:17 +00:00Commented Oct 12, 2015 at 19:44
-
In that case leave the stty as it is, but just add the three second delay so it can get past the bootloader before you send your request.Majenko– Majenko2015年10月12日 19:45:07 +00:00Commented Oct 12, 2015 at 19:45
-
I was waiting long enough with
tail -f
, also I was observing directly, that Serial Monitor does see the output, whiletail -f
does not.Dims– Dims2015年10月12日 19:45:36 +00:00Commented Oct 12, 2015 at 19:45 -
You can't read the data with two things at once - only one thing will get it. Also you have to wait before sending "temp" otherwise it just won't ever get it.Majenko– Majenko2015年10月12日 19:46:43 +00:00Commented Oct 12, 2015 at 19:46
-
By the way, what are exact conditions for resetting? I have 3 commands in the script, touching the port:
stty
,echo
andread
. Will it reset 3 times or only one, how to know?Dims– Dims2015年10月12日 19:47:22 +00:00Commented Oct 12, 2015 at 19:47