reading/writing to serial port in linux

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
i want to know how to output data through a serial port and also how to read in data from a serial port. Also, is there a way to automatically write data that comes into the serial port to a file? basically, i am a newbie in linux that just wants to test data communication between two computers, (one windows machine via hyperterminal and one linux machine).

i've also posted this in justlinux, but u guys are a LOT faster with the replies, and i figure the more input, the merrier.

Any help welcome.
Thanks.

 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
open /dev/ttyS0 (or whatever serial port you want) as a file, and read and write to it just like a file.
 

drag

Elite Member
Jul 4, 2002
8,708
0
0
Originally posted by: sciencewhiz
open /dev/ttyS0 (or whatever serial port you want) as a file, and read and write to it just like a file.

hows that work exactly, I've never tried it myself.

What something like
cat /dev/ttyS0 > ~/filename (on the receiving computer)
cat file > /dev/ttyS0 (on the sending compute)

???
Seems simple enough...


 

sciencewhiz

Diamond Member
Jun 30, 2000
5,885
8
81
Well, I was referring to C, but it works the same way from the commandline. If you don't know C, the commands that drag gave would work.

If you just want to test communication, you can use a terminal emulator in linux too, such as minicom.
 

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
ok, i'm sorry for all the stupid questions, but i really appreciate the help. I know a little C (took a "C for engineers" course), how exactly do you open and read files as far as the syntax goes? also will i have to do something like define the file as some kind of macro, in the header of the C program?
Thanks again for the help.

edit: to give you some understanding of what i know regarding files. basically, all we had to do is take a file and read it into a program or have a program put data into a file (basically dealing with the actual execution of a compiled program), we never really worked with files within programs.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
No macros are needed, just the file name. You can either let the user specify the file in some fashion (commandline, menu options, config file, etc) or you can just hard code it to use a certain serial port and then call open("/dev/ttyS0", O_RDONLY) to open it read-only. run 'man 2 open' to get the manual page for the open function. Then call the read and write functions to read or write data to the port.
 

rjain

Golden Member
May 1, 2003
1,475
0
0
you might need to use ioctl() to tweak some parameters of the serial port. I think there's a serial programming howto.
 

AznMaverick

Platinum Member
Apr 4, 2001
2,776
0
0
ok, i'm trying to do this at school on a redhat9 machine, but i get "permission denied" errors. As a user i do not have rights to this port, will i need root permissions?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Not necessarily, the serial ports are just like any other file and they have rights assigned to them. By default root probably is the only one with access to them, but your teacher should be able to give you access to them, especially if he expects you to use them.