LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 06-02-2004, 06:56 PM   #1
philetus
LQ Newbie
 
Registered: Jun 2004
Distribution: gentoo
Posts: 12

Rep: Reputation: 0
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?
 
Old 06-02-2004, 07:20 PM   #2
itsjustme
Senior Member
 
Registered: Mar 2003
Location: Earth
Distribution: Slackware, Ubuntu, Smoothwall
Posts: 1,571

Rep: Reputation: 45
Hmmm... Don't know. Have you looked at minicom?

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

Last edited by itsjustme; 06-02-2004 at 07:22 PM.
 
Old 06-03-2004, 08:45 PM   #3
philetus
LQ Newbie
 
Registered: Jun 2004
Distribution: gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
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.
 
Old 06-03-2004, 09:03 PM   #4
posixjunkie
Member
 
Registered: Jan 2004
Location: NJ
Posts: 33

Rep: Reputation: 15
what is the output of
ls -la /dev/TTYS0
are teh permissions correct?
 
Old 06-04-2004, 03:35 PM   #5
philetus
LQ Newbie
 
Registered: Jun 2004
Distribution: gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
$ 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.
 
Old 06-04-2004, 04:40 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,715

Rep: Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380Reputation: 2380
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
 
Old 06-04-2004, 05:09 PM   #7
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Did you try reversing the serial cable? Maybe the adapter screwed up the RX line on the Linux machine's end.
 
Old 06-09-2004, 01:40 PM   #8
philetus
LQ Newbie
 
Registered: Jun 2004
Distribution: gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 06-24-2004, 02:27 AM   #9
prolinux
LQ Newbie
 
Registered: Jun 2004
Posts: 1

Rep: Reputation: 0
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.
 
Old 06-24-2004, 06:46 PM   #10
philetus
LQ Newbie
 
Registered: Jun 2004
Distribution: gentoo
Posts: 12

Original Poster
Rep: Reputation: 0
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mounted read-write Fat32 partition suddenly becomes read-only?? hohead Linux - General 8 04-05-2007 01:17 PM
Example share in smb.conf doesn't work - read/write vs read only kleptophobiac Linux - Networking 0 09-01-2004 08:14 PM
Permission denied when trying to write to ttyS0 thepoint Linux - General 0 06-03-2004 10:36 PM
Open office read only, K-write read/write mode lwtvh Linux - Newbie 1 07-19-2003 12:33 PM
Change from Read only to Read Write File System? justiceisblind Linux - Newbie 3 03-03-2002 08:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 06:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration