Linux write to serial

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 .

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

Читайте также:  С unit testing linux

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.

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

Читайте также:  Linux network connections by process

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.

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

Источник

Writing to Serial Port from Linux Command Line

Writing to the serial port from the Linux command line

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

Читайте также:  Linux install rpm with yum

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.

Linux Shell, How to send a command over Serial port and return?

Here is how we fixed this issue:

    Config serial port with RAW setting:

stty -F /dev/ttyS1 speed 9600 cs8 -cstopb -parenb raw
echo -ne "reboot\r" > /dev/ttyS1

I believe that the above might include a typo. If the above doesn’t work, try:

 echo -ne "reboot\n\r" > /dev/ttyS1

Reading and writing to serial port in C on Linux

I’ve solved my problems, so I post here the correct code in case someone needs similar stuff.

int USB = open( "/dev/ttyUSB0", O_RDWR| O_NOCTTY );

Set parameters

struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( USB, &tty ) != 0 ) std::cout >

/* Save old tty parameters */
tty_old = tty;

/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);

/* Setting other Port Stuff */
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;

tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines

/* Make raw */
cfmakeraw(&tty);

/* Flush Port, then applies attributes */
tcflush( USB, TCIFLUSH );
if ( tcsetattr ( USB, TCSANOW, &tty ) != 0) std::cout >
unsigned char cmd[] = "INIT \r";
int n_written = 0,
spot = 0;

do n_written = write( USB, &cmd[spot], 1 );
spot += n_written;
> while (cmd[spot-1] != '\r' && n_written > 0);

It was definitely not necessary to write byte per byte, also int n_written = write( USB, cmd, sizeof(cmd) -1) worked fine.

At last, read:

int n = 0, 
spot = 0;
char buf = '\0';

/* Whole response*/
char response[1024];
memset(response, '\0', sizeof response);

do n = read( USB, &buf, 1 );
sprintf( &response[spot], "%c", buf );
spot += n;
> while( buf != '\r' && n > 0);

if (n < 0) std::cout >
else if (n == 0) std::cout >
else std::cout >

This one worked for me. Thank you all!

Reading and writing to a serial port using shell and Java

Instead of screen you may use command cu from UUCP package.
To install UUCP package sudo apt-get install uucp or sudo yum install uucp .

Then use this command:
static String command = «cu -l » + port + » -s » + baudRate;

Some explanation:

  • screen -d detaches session (it runs in background) that’s why you do not see any data.
  • screen requires terminal which is not easy from java. See How to open a command terminal in Linux?

Источник

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