LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-26-2009, 04:26 AM   #1
nathanhi
LQ Newbie
 
Registered: Jul 2009
Location: Flensburg, Germany
Distribution: Arch Linux
Posts: 5

Rep: Reputation: 0
C - How to communicate (natively) with ttySx?


Hello,

I just want to write a little piece of software in C to communicate with a "special" device over serial Port. Will I have to use fopen() or are there any better ways to realize this? I want to write and read to/from the serial device. Last but not least a second question: How can I distribute parts of my Code in several files and compile them that I get one Binary? I'm currently writing functions into an header file, is this the right way?

Greetings,
Nathan

P.S.: I know, my English is bad. Sorry.

Last edited by nathanhi; 11-26-2009 at 04:31 AM.
 
Old 11-26-2009, 09:15 AM   #2
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,506

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
In Linux, devices (like serial ports), are abstracted as files. To use them, you use the same paradigm as used for accessing files: open(), read() write(), close(), etc. For serial ports, you also use the termios interface to set parameters such as bit rate, word size, parity, and especially terminal oriented processing performed by the device driver, such as end-of-line processing.
For more good information on serial port programming, and useful sample code:
Serial HOWTO
Serial Programming HOWTO
Serial Programming Guide for POSIX Operating Systems

As a general rule, you can compile individual source files to object code, and then link them into a single runtime binary executable.
Code:
gcc -o module1.o -c module1.c
gcc -o module2.o -c module2.c
gcc -o module3.o -c module3.c
ld -o mainmodule module1.o module2.o module3.o
gcc can perfom the linkage phase, as well. There are many many options that can be applied, many of which are normally required, such as supplying directives to allow the compiler and linker to locate header files and libraries. See the gcc man page for a start.

Your use of English puts many native English writers to shame. No apologies required.

--- rod.
 
1 members found this post helpful.
Old 11-27-2009, 08:11 AM   #3
nathanhi
LQ Newbie
 
Registered: Jul 2009
Location: Flensburg, Germany
Distribution: Arch Linux
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
In Linux, devices (like serial ports), are abstracted as files. To use them, you use the same paradigm as used for accessing files: open(), read() write(), close(), etc. For serial ports, you also use the termios interface to set parameters such as bit rate, word size, parity, and especially terminal oriented processing performed by the device driver, such as end-of-line processing.
For more good information on serial port programming, and useful sample code:
Serial HOWTO
Serial Programming HOWTO
Serial Programming Guide for POSIX Operating Systems

As a general rule, you can compile individual source files to object code, and then link them into a single runtime binary executable.
Code:
gcc -o module1.o -c module1.c
gcc -o module2.o -c module2.c
gcc -o module3.o -c module3.c
ld -o mainmodule module1.o module2.o module3.o
gcc can perfom the linkage phase, as well. There are many many options that can be applied, many of which are normally required, such as supplying directives to allow the compiler and linker to locate header files and libraries. See the gcc man page for a start.

Your use of English puts many native English writers to shame. No apologies required.

--- rod.
Hi,

thank you! You made my day.

Greetings,
Nathan
 
  


Reply

Tags
serial


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hex String to ttysX digmeupnow Linux - Newbie 1 11-25-2008 03:33 AM
Scrolling in a TTYSX? zajelo3 Linux - General 2 07-24-2004 08:52 PM
Null modem cable .... cat < /dev/ttySx problem.... chvsspraveen Linux - Networking 1 03-24-2004 12:12 AM
kernel 2.6.0 probs with terms and /dev/ttySx Oxagast Linux - General 3 12-22-2003 03:36 AM
ititilize ttySX using other than getty frieza Linux - Software 3 10-01-2002 11:46 AM


All times are GMT -5. The time now is 03:07 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration