I have been trying to send the bytes (in order): 2201
, 2211
etc. to my Arduino board, which I've connected to my computer, using a USB to RS485 adapter.
However, when I connect it yo my computer, whenever I try to send 2201
to my Arduino, using the command echo 2 > /dev/ttyUSB0;echo 2 > /dev/ttyUSB0;echo 0 > /dev/ttyUSB0;echo 1 > /dev/ttyUSB0
, it sometimes works, turning the LED on normally as it should, but other times it just doesn't, displaying bash: /dev/ttyUSB0: Input/output error
.
If you have any way of getting through this, that'd be great. Also, please let me know if there's any better command than echo X > /dev/ttyUSB0
to send data to my Arduino. I'm new to serial communication, so I'm not sure what the best way to do this is.
First, a question: why are you using multiple echo
statements, rather than a single echo 2201 > /dev/ttyUSB0
? The latter requires substantially less typing.
A problem with both your solution and the one I just proposed is that the echo
command appends a newline to its output. So if I were to direct output to a file; like this:
echo 2 >afile; echo 2 >>afile; echo 0 >>afile; echo 1 >>afile
I end up with a file that contains:
2\n2\n0\n1\n
And that's exactly what you're sending out the serial port. You can use the -n
option to echo to suppress newline, so:
echo -n 2201 > /dev/ttyUSB0
You would probably be better off using something other than the shell to interact with serial ports. Python (with the pyserial
module) is a good choice, but so is just about anything else. If you do keep using the shell, there are some other stackexchange questions worth looking at, such as this one and this one .
If you want to have interactive access to the serial port, there are a number of common applications that can help out with that (screen, ckermit, picocom, moserial, minicom, etc).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.