質問

I have this bash file:

#!/bin/bash
stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -imaxbel -opost -onlcr -isig -icanon -         iext en -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts

screen /dev/ttyACM0 9600
echo "1"

This is basically an Arduino connected to my Ubuntu PC and I can run the code perfectly all until the echo "1" section.

I can ...

  1. establish the connection
  2. see the screen of the serial connection
  3. type in "1" and see my light bulb light up, and when I type "0" the light bulb turns off.

The problem I am meeting now is that I would like to control the on/off in code (without me manually typing it out) and it seems almost impossible to do that. The logic is correct but when I start the screen, the code just stops there and runs the screen waiting for me to have some input. All until I plug out the Arduino will the echo finally come out. Is there a way to solve this?

役に立ちましたか?

解決

I had a problem like this before, this was my workaround:

I had more luck with cu then with stty

Start a screen session:

screen -S arduino -dmS cu -l /dev/ttyACM0 -s 9600

Now there is a screen session created called arduino

You can send commands to it from a script:

screen -S arduino -X stuff 1

This will send the 1 to the serial connection just like your example

If you want to control this with a different user make sure rights will allow this and create the screen session with the same user that will be sending commands to the screen session.

If you have more questions just ask me.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top