Linux - HardwareThis forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am running slackware 9 and attempting to communicate with a serial device over /dev/ttyS0. for testing I have plugged a female-female serial cable between my serial port and a windows box running a terminal program.
I can write to the serial port, for example if I say:
$ echo hi > /dev/ttySO
'hi' prints out on the windows machine terminal no problem.
however, I cannot seem to read from it. if I say:
$ cat /dev/ttyS0
and then go and send some stuff from the windows terminal, I get nothing, the linux terminal just hangs.
maybe I have my serial port setup wrong? here are the port settings from:
I am pretty sure cat should work, I have seen several howtos that suggest
communicating with the device linked to /dev/ttyX by saying 'cat
</dev/ttyX'.
further description of the problem:
if I turn on echo, 'stty echo </dev/ttyS0' then when I send data from the
windows terminal it is echoed to that terminal, but 'cat </dev/ttyS0'
still hangs without printing anything.
maybe the stuff I am sending is just sitting in the input buffer?
I am ultimately trying to send binary data, so I am trying NOT to use
control flow characters.
cat /dev/ttyS0 doesn't appear to work on my PC either. However since your working with binary data minicom or other terminal software may not work. Your best be would be to write your own application. So can you say what the serial device is your trying to interface?
hi.
i am doing a project, on transfering messages...on a serial cable( a null modem).
i would be thankful if u could tell me how to u
i need to send a message through a serial port.
so i want to know how to read n write the ttyS0.
i have an idea of how to do it....not sure if its right...
if the response is something like:
> /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
your serial port is happening, and if you say:
$ cat -v </dev/ttyS0
anything sent up the cable should print out to the terminal window.
if it still doesn't work, or is garbled, you may have to adjust the tty settings with 'stty'. try 'man stty' to find out about using stty. the most important thing is to set the baud rates to be the same on both ends of the serial line. to set the baud rate to 9600 (the default):
$ stty 9600 </dev/ttyS0
to see the current settings:
$ stty -a </dev/ttyS0
to restore the default settings:
$ stty sane </dev/ttyS0
to send text back down the line, for example 'hello world' open another terminal window and say:
$ echo hello world >/dev/ttyS0
are you trying to send binary or ascii (text)? if you want to send text, but from a program, not the terminal, here is a perl script to send and read text:
#!/usr/bin/perl -w
# your serial port device file
my $device = "/dev/ttyS0";
# put the tty settings you really want here
`stty sane <$device`;
# open filehandles to read and write to serial port
open( SERIAL_READER, "<$device" ) || die "can't open $device: $!";
open( SERIAL_WRITER, ">$device" ) || die "can't open $device: $!";
# read until eof? I think eof character is set by stty
my @in = <SERIAL_READER>;
# read one line at a time, and then respond to each line
while( <SERIAL_READER> ){ # will hang until a newline is received, and loop until eof is received?
print; # print received line to terminal
print SERIAL_WRITER "ok\n"; # respond with a newline at the end of response
}
# close filehandles when you are done
close( SERIAL_READER );
close( SERIAL_WRITER );
exit 0;
if you want binary communication, that is difficult from the terminal, here is a perl script:
#!/usr/bin/perl -w
# device file
my $device = "/dev/ttyS0";
# set tty for binary communication
`stty 9600 raw -echo <$device`;
# open filehandles to read and write to serial port
open( SERIAL_READER, "<$device" ) || die "can't open $device: $!";
open( SERIAL_WRITER, ">$device" ) || die "can't open $device: $!";
# you don't need to do this on unix/linux, only on substandard platforms like windows
# with filesystems that distinguish between binary and text files
binmode( SERIAL_READER );
binmode( SERIAL_WRITER );
# write a list of 8-bit bytes with values from 0-255
my @write_list = ( 0, 128, 255 ); # three integer values to write
my $buffer = pack( "C*", @write_list ); # pack integers into buffer as bytes
syswrite( SERIAL_WRITER, $buffer, scalar(@write_list), 0 ); # write buffer to serial port
# read three bytes from serial port as integer values from 0-255
my $buffer = ""; # buffer to read bytes into
sysread( $device_reader, $buffer, 3, 0 ); # read 3 bytes from serial port into buffer
my @list = unpack( "C*", $buffer ); # unpack bytes to integers
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.