Linux send to serial port

Writing to the serial port from the Linux command line

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 

3 Answers 3

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:

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

thanks, I am sure that it is the port that I want, is there a simpler way of testing serial connection, echo always returns success.

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

which you can construct on the command line by typing Ctrl V Ctrl R and Ctrl V Ctrl B . 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.

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.

@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.

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.

Using Screen:

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

Читайте также:  Tp link wn722n драйвер kali linux

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:

(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 

Источник

Writing to the serial port from the Linux command line

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 

3 Answers 3

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:

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

thanks, I am sure that it is the port that I want, is there a simpler way of testing serial connection, echo always returns success.

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

which you can construct on the command line by typing Ctrl V Ctrl R and Ctrl V Ctrl B . 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.

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.

Читайте также:  Удалить только пустые папки linux

@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.

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.

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:

(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 

Источник

How to send data to a serial port and see any answer?

On Linux, I want to send a command string (i.e. some data) to a serial port (containing control characters), and listen to the response (which also usually might contain control characters). How can I do this as simplest as possible on Linux? An example is appreciated!

5 Answers 5

All devices on Unix are mapped to a device file, the serial ports would be /dev/ttyS0 /dev/ttyS1 . .

First have a look at the permissions on that file, lets assume you are using /dev/ttyS1 .

You will want read.write access, if this is a shared system then you should consider the security consequences of opening it up for everyone.

Читайте также:  Linux установка сертификата der

A very simple crude method to write to the file, would use the simple echo command.

You can have cat running in one terminal, and echo in a 2nd.

If everything is gibberish, then baud rate, bit settings might need setting before you start sending. stty will do that. !! NOTE stty will use stdin as default file descriptor to affect.

This might be enough for you to script something and log ? Not sure what you are trying to achieve.

For a more interactive, remembers your default settings approach would be to use minicom it is just a program which does everything I’ve mentioned so far. (similar to hyperterminal in Windows, you might be familiar).

An intermediate solution, would use a terminal program like screen which will work on a serial device.

man screen man minicom man stty for more information

Источник

Using UART from a Linux Shell

This guide explains how to use a serial port using linux shell. It shows how to set and read serial port settings, and how to send and receive data on a serial port.

Steps

To follow this guide, a terminal/shell is needed on the TechNexion development kit. The easiest way is to use the debug console facility, but can also be achieved using ssh or adb .

UART naming and numbering

In hardware manuals, schematics and the like, the UARTs are often named «UART1» or «UART2» and so forth. Typically, there is no «UART0». Software, on the other hand, typically numbers the serial port devices starting with the number 0.

To make a long story short, what in schematics or user guides is called «UART1» is /dev/ttymxc0 in software, «UART2» is /dev/ttymxc1 , and so on.

Setting and reading UART parameters

UART parameters are read and set with the stty command. To set the baud rate on UART3 to 115200, use the command:

To enable RTS/CTS flow control, use

and disable flow control with

To view the current settings for UART3, use

Just replace /dev/ttymxc2 with the name of the UART device.

Sending and Receiving data

This is very easy, sending is done by writing data to the device file, for instance by

and to receive is to read the device file

Please note that the receiving terminal can have buffering enabled, so the data read might not be displayed immediately. The buffer is flushed once enough data is entered, or when enough newlines are encountered.

Источник

Оцените статью
Adblock
detector