45

From Windows I can communicate with a serial port device using the following commands:

mode com1: baud=9600 data=8 parity=n stop=1
copy con com1
alt+18alt+2ctrl+z

The device starts the requested operation.

When I try to accomplish the same operation from a stand-alone Debian box or from a Debian VirtualBox instance of the same Windows machine, I had no luck so far.

Here are equivalent Linux commands (at least I think so):

stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb
echo '\x12\x02' > /dev/ttyS0

Nothing happens.

How can I do it?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
erin c
  • 1,345
  • 2
  • 20
  • 36

3 Answers3

36

If you want to use hexadecimal codes, you should add the -e option to enable interpretation of backslash escapes by echo (but the result is the same as with echo Ctrl + R,Ctrl + B). And as wallyk said, you probably want to add -n to prevent the output of a newline:

echo -en '\x12\x02' > /dev/ttyS0

Also make sure that /dev/ttyS0 is the port you want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
praetorian droid
  • 2,989
  • 1
  • 17
  • 19
17
echo '\x12\x02'

will not be interpreted, and will literally write the string \x12\x02 (and append a newline) to the specified serial port. Instead use

echo -n ^R^B

which you can construct on the command line by typing CtrlVCtrlR and CtrlVCtrlB. Or it is easier to use an editor to type into a script file.

The stty command should work, unless another program is interfering. A common culprit is gpsd which looks for GPS devices being plugged in.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Thanks so much for your response, I tried the echo -n CTRL+VCTRL+RCTRRL+VCTRL+B from command line but I couldn't make it work, I want to try other commands, therefore I want to know how you come up with these translations(0x12 = ^R , 0x02 = ^B), these are not ascii translations I suppose. – erin c Jan 16 '12 at 08:46
  • 1
    @erincarikan: use `man ascii` to see how 0x12 relates to Ctrl-R. They are pure ascii. It is possible that some ctrl combinations won't work, like ctrl-@ (NUL). It could be easier to write a program to do a binary protocol like this. – wallyk Jan 16 '12 at 08:56
  • thanks I totally got it, but unfortunately it doesn't work, I am suspecting that something is interfering with stty , I don't have gpsd running. I got to look into this more. – erin c Jan 16 '12 at 09:39
  • 1
    It finally worked, I had a problem in my virtualbox serial port configuation, thanks for the help. – erin c Jan 16 '12 at 13:35
7

Using Screen:

Note: Screen is actually not able to send hexadecimal, as far as I know. To do that, use echo or printf.

I was using the suggestions in this post to write to a serial port, and then using the information from another post to read from the port, with mixed results. I found that using Screen is an "easier" solution, since it opens a terminal session directly with that port. (I put easier in quotes, because Screen has a really weird interface, IMO, and takes some further reading to figure it out.)

You can issue this command to open a screen session, and then anything you type will be sent to the port, plus the return values will be printed below it:

screen /dev/ttyS0 19200,cs8

(Change the above to fit your needs for speed, parity, stop bits, etc.) I realize Screen isn't the "Linux command line" as the post specifically asks for, but I think it's in the same spirit. Plus, you don't have to type echo and quotes every time.

echo

It follows praetorian droid's answer. However, this didn't work for me until I also used the cat command (cat < /dev/ttyS0) while I was sending the echo command.

printf

I found that one can also use printf's '%x' command:

c="\x"$(printf '%x' 0x12)
printf $c >> $SERIAL_COMM_PORT

Again, for printf, start cat < /dev/ttyS0 before sending the command.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MrUser
  • 1,187
  • 15
  • 25
  • 1
    Nothing worked for me until I did as MrUser said: you have to be listening over /dev/ttyS0 before you can write to it. – Gouda Jul 22 '15 at 16:02