Sending bytes to serial port from UNIX command line?

23,919
#!/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.

Share:
23,919
Chunky Chunk
Author by

Chunky Chunk

Coder who went to art school. I program with my feelings.

Updated on September 29, 2020

Comments

  • Chunky Chunk
    Chunky Chunk over 3 years

    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.

  • andcoz
    andcoz over 13 years
    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).
  • Chunky Chunk
    Chunky Chunk over 13 years
    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?
  • SourceSeeker
    SourceSeeker over 13 years
    @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
  • SourceSeeker
    SourceSeeker over 13 years
    ...or printf '%b' '\x32' > /dev/cu.usbserial-A700dYoR
  • Chunky Chunk
    Chunky Chunk over 13 years
    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?
  • Justin808
    Justin808 over 13 years
    @TDI1978: Check out 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.
  • Philipp Ludwig
    Philipp Ludwig over 3 years
    @ChunkyChunk you probably found out by now, but man ascii is your friend.