i like to send some data through the serial from my Raspberry Pi to the arduino. My problem is sending it, i read something like sudo echo "8933" > /dev/ttyUSB0 but it is not working. Sending though the Serial Monitor in the Arduino-IDE works fine and sudo screen /dev/ttyUSB0 too.
How can i do that programmatically?
It would be nice if it were a bash-script because i want to run it via ssh.
-
I was incorrect above, you should refer here [previous question][1] [1]: unix.stackexchange.com/questions/89182/…sam– sam2015-05-19 21:17:27 +00:00Commented May 19, 2015 at 21:17
-
@sam pySerial works fine, as far as typing manually. Can you explain how to do that programmatically? Otherwise the method using termios just returns errors that it coudn't open the file... You see I am very new to Python, can you (or someone else) write that to a short py-script?WuerfelDev– WuerfelDev2015-05-20 20:19:13 +00:00Commented May 20, 2015 at 20:19
-
Well, what data are you trying to send? i.e. is this data being read from a text file? or is it the output os some script you've written? We need more to go on. You can go about this task any number of ways.sam– sam2015-05-20 20:36:17 +00:00Commented May 20, 2015 at 20:36
-
@sam i get the number (my data) from a sh-script (which reads it using curl from the web) and want send that number to the arduino.WuerfelDev– WuerfelDev2015-05-20 20:43:24 +00:00Commented May 20, 2015 at 20:43
-
Take a look at my revised answer. Its not a great long-term solution, but it will get you going.sam– sam2015-05-20 21:52:40 +00:00Commented May 20, 2015 at 21:52
2 Answers
If you already have some script generating data you'd like to send to your Arduino serial monitor you could try a simple python wrapper to send your data. Something like:
#!/usr/bin/python2
import sys
import serial
def main():
data = sys.argv
if data[1:]:
ser = serial.Serial('/dev/ttyUSB0', 19200, timeout=1)
for d in data[1:]:
print d
ser.write(str(d))
ser.close()
else:
print "No Input given!\n"
if __name__ == "__main__":
main()
This script will take whatever is passed in as an argument, and send it over serial (i.e. to your Arduino serial monitor).
./[this_script] `[your_script]`
This is just something I just whipped together, it should probably be cleaned up. I'd consider getting familiar with Python as its perfectly suited for one-off scriptable tasks such as this.
8 Comments
python serial_write.py 6334 Traceback (most recent call last): File "serial_write.py", line 18, in <module> main() File "serial_write.py", line 11, in main ser.write(data) File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 471, in write n = os.write(self.fd, d) TypeError: must be string or buffer, not listdata must be cast to a string. Try the corrected version shown above.port.close() so i tried it without ser.close() and now it works fine. thxI see two problems with sudo echo "8933" > /dev/ttyUSB0.
echoappends a newline to whatever it outputs. Tryprintf "8933"instead.The
sudocommand only applies to the actual echo. The output file is still opened by "you" (not root) beforesudoruns. Tryprintf "8933" | sudo tee /dev/ttyUSB0 > /dev/null.