1

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.

5
  • I was incorrect above, you should refer here [previous question][1] [1]: unix.stackexchange.com/questions/89182/… Commented 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? Commented 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. Commented 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. Commented 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. Commented May 20, 2015 at 21:52

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Running this gives me following error: $ 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 list
Sorry, I dont have anything to test this on. It looks like data must be cast to a string. Try the corrected version shown above.
that also does not work, but now there is no output, the script is working so far. Thank you for that. I think there is something wrong with Serial module in python
this new edit will print out what its trying to send to help you make sense of it.
I saw, that this answer has no port.close() so i tried it without ser.close() and now it works fine. thx
|
0

I see two problems with sudo echo "8933" > /dev/ttyUSB0.

  1. echo appends a newline to whatever it outputs. Try printf "8933" instead.

  2. The sudo command only applies to the actual echo. The output file is still opened by "you" (not root) before sudo runs. Try printf "8933" | sudo tee /dev/ttyUSB0 > /dev/null.

1 Comment

Sorry, that didn't work. My arduino dies not show an incorrect input, it just "reboots" without any serial-transfered-data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.