• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

reading/writing to serial port in linux

AznMaverick

Platinum Member
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.

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


 
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.
 
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.
 
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.
 
you might need to use ioctl() to tweak some parameters of the serial port. I think there's a serial programming howto.
 
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?
 
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.
 
Back
Top