views:

41

answers:

1

i would like to send a stream of bytes to a serial port using the command line. is this possible? my serial port is at /dev/cu.usbserial-A700dYoR on my Mac. for example, if i wanted to send the integer 50 or the string "data" to that serial port, how can i do that?

my knowledge of UNIX is very limited.

+1  A: 
#!/bin/bash

# Port setting
stty -F /dev/cu.usbserial-A700dYoR raw speed 9600

echo 'Hello' > /dev/cu.usbserial-A700dYoR

or something like that if I remember correctly... Been a few years. You will probably have to be sudo for it to work...

This is sending text... not binary.. to send the number 50 as text

echo '50' > /dev/cu.usbserial-A700dYoR

to send it as a binary integer would be more difficult.

Justin808
`echo '2' > /dev/cu.usbserial-A700dYoR` will send 50 as a single byte. If you really need to send an "integer", you have to discover what is an "integer" for the receiver (e.g. big or little endian, 7 or 8 byte stream, encoding).
andcoz
basically, i'm trying to send something like writeByte(50), or any integer ranging from 0-255, to the serial port. can i specify the endian on the command line?
TheDarkInI1978
@TDI1978: If you're sending bytes individually, you may not need to worry about byte order. To send a decimal 50 byte in Bash: `printf '%c' $'\x32' > /dev/cu.usbserial-A700dYoR`
Dennis Williamson
...or `printf '%b' '\x32' > /dev/cu.usbserial-A700dYoR`
Dennis Williamson
what's the translation? i will be sending bytes whos value ranges from 0 to 255, so if 50 is equivalent to x32, how do i get that equivalency?
TheDarkInI1978
@TDI1978: Check out http://en.wikipedia.org/wiki/Endianness for information on the order of things (including bits, bytes, etc.) if you are talking to 2 of the same kinds of computers you dont have to worry about it. Issues come up when you are talking to machines that are in different orders... The gist of it is there are little and big Endians and you have to translate between the two of them. Its kinda like reading right to left, or left to right.. One way you have Edians, the other you have snaidE.
Justin808