LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   can write to but not read from ttyS0 (https://www.linuxquestions.org/questions/linux-hardware-18/can-write-to-but-not-read-from-ttys0-188998/)

philetus 06-02-2004 05:56 PM

can write to but not read from ttyS0
 
hi,

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:

$ stty -a --file /dev/ttyS0

speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;
eol2 = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon
-ixoff -iuclc -ixany -imaxbel
opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0
vt0
ff0
isig icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop
-echoprt
echoctl echoke

any suggestions?

itsjustme 06-02-2004 06:20 PM

Hmmm... Don't know. Have you looked at minicom?

Not sure if that will help with communicating back from windows.

philetus 06-03-2004 07:45 PM

hi,

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.

any suggestions would be greatly appreciated.

posixjunkie 06-03-2004 08:03 PM

what is the output of
ls -la /dev/TTYS0
are teh permissions correct?

philetus 06-04-2004 02:35 PM

$ ls -la /dev/ttyS0
> crw-rw-rw- 1 root uucp 4, 64 Jun 4 11:52 /dev/ttyS0

it seems like the permissions are ok, but I'm not sure.

michaelk 06-04-2004 03:40 PM

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?

Here something you can modify:
http://www.comptechdoc.org/os/linux/...pgcserial.html

MS3FGX 06-04-2004 04:09 PM

Did you try reversing the serial cable? Maybe the adapter screwed up the RX line on the Linux machine's end.

philetus 06-09-2004 12:40 PM

hi,

I figured out what was wrong, when I probed the serial port:

$ setserial /dev/ttyS0 -v autoconfig

I got a message that the serial port was being used by another device.

I restarted the computer and then the probe came back with:

> /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4

like it was supposed to, and now I can read from and write to the port
(with cat).

thanks for the suggestions.

mike

prolinux 06-24-2004 01:27 AM

how to read and write ttyS0
 
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...

open /dev/ttyS0 and then send the message..

do tell me if u know how to do it...

thank u

waiting eagerly for ur reply.

philetus 06-24-2004 05:46 PM

hi,

have you set up /dev/ttyS0?

try this:
$ setserial /dev/ttyS0 -v autoconfig

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: $!";

# write something
print SERIAL_WRITER "hello world\n";

# 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

exit 0;

good luck!


All times are GMT -5. The time now is 05:53 AM.