By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,659 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,659 IT Pros & Developers. It's quick & easy.

use of serial and parrallel port using C

P: n/a
Hi All,

What kind of commands can I use to control the serial and parallel port of
my pc. I want to use my laptop instead of microcontrollers and write my
software in ANSI C to be used for control purposes of sensors and other
electronic devices.

I haven't done any C for a while now so I would assume my knowledge is that
of a beginner now.

A thousand thanks for any help offered or intended.

Naveed
Nov 14 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
"The Man With The Harmonica" <co****************@yahoo.co.uk> wrote in
news:cf**********@rdel.co.uk:
Hi All,

What kind of commands can I use to control the serial and parallel port
of my pc.


None. ISO C doesn't have any commands nor does it know anything about
serial or parrallel ports. You'll need to ask this in a newsgroup that
programs for the platform you are using, e.g. DOS, Linux, Win32, etc.

--
- Mark ->
--
Nov 14 '05 #2

P: n/a
The Man With The Harmonica wrote:
Hi All,

What kind of commands can I use to control the serial and parallel port of
my pc. I want to use my laptop instead of microcontrollers and write my
software in ANSI C to be used for control purposes of sensors and other
electronic devices.

I haven't done any C for a while now so I would assume my knowledge is that
of a beginner now.

A thousand thanks for any help offered or intended.

Naveed


news:comp.arch.embedded

One can use ANSI C to access serial ports and parallel ports
as well as other devices as long as those devices are memory
mapped. Whether the operating system, if there is one, allows
this is a different issue.

However, this code would be platform specific and not portable
to platforms that do not have the same memory mapping.

The technique is to access the hardware devices by dereferencing
pointers. For example, one would assign a pointer to an integer
with the value of the device's address:
volatile unsigned char * Serial_Receive_Register =
(volatile unsigned char *) 0x40000;

unsigned char Read_Serial_Port(void)
{
return * Serial_Receive_Register;
}

Before reading the receive register, one should check that
the status first and also have a design to handle when
nothing has been received.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #3

P: n/a
# What kind of commands can I use to control the serial and parallel port of
# my pc. I want to use my laptop instead of microcontrollers and write my
# software in ANSI C to be used for control purposes of sensors and other
# electronic devices.

Depends in part on whether the operating system provides file names for
those devices that you can fopen. For example on unices, you can open
serial ports with file names like "/dev/ttyS0".

--
SM Ryan http://www.rawbw.com/~wyrmwif/
The whole world's against us.
Nov 14 '05 #4

P: n/a
In <6i*****************@newssvr17.news.prodigy.com> Thomas Matthews <Th****************************@sbcglobal.net> writes:
The Man With The Harmonica wrote:
Hi All,

What kind of commands can I use to control the serial and parallel port of ^^ my pc. I want to use my laptop instead of microcontrollers and write my ^^^^^ ^^^^^^^^^
software in ANSI C to be used for control purposes of sensors and other
electronic devices.

I haven't done any C for a while now so I would assume my knowledge is that
of a beginner now.
news:comp.arch.embedded


Since when do PC's count as embedded control systems?
One can use ANSI C to access serial ports and parallel ports
as well as other devices as long as those devices are memory
mapped.
Chapter and verse, please.
Whether the operating system, if there is one, allows
this is a different issue.
If ANSI C supported the feature, the OS would be irrelevant.
However, this code would be platform specific and not portable
to platforms that do not have the same memory mapping.
Then, it wouldn't be code blessed by ANSI C.
The technique is to access the hardware devices by dereferencing
pointers. For example, one would assign a pointer to an integer
with the value of the device's address:
volatile unsigned char * Serial_Receive_Register =
(volatile unsigned char *) 0x40000;


According to ANSI C, any attempt to use this pointer value results in
undefined behaviour.

The *right* thing is to use the OS interface to the serial port. The
kind of code you're showing belongs exclusively to device drivers and
is highly unlikely to work in userland programs. Imagine what happens
when two programs attempt to access the same port directly, at the same
time (assuming that it would be possible): complete chaos.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5

P: n/a
"Mark A. Odell" <od*******@hotmail.com> wrote in message
news:Xn********************************@130.133.1. 4...
"The Man With The Harmonica" <co****************@yahoo.co.uk> wrote in
news:cf**********@rdel.co.uk:
Hi All,

What kind of commands can I use to control the serial and parallel port
of my pc.


None. ISO C doesn't have any commands nor does it know anything about
serial or parrallel ports. You'll need to ask this in a newsgroup that
programs for the platform you are using, e.g. DOS, Linux, Win32, etc.

--
- Mark ->
--


Hi Mark,

I'm using an old Toshiba laptop with win95. I want to use the parallel port
to control some circuits and to read the state of some control lines.
Basically its going to be controlling a four wheeled buggy and I want to do
this using C and through the parallel port.

thanks

Naveed
Nov 14 '05 #6

P: n/a
On Thu, 19 Aug 2004 16:51:06 +0100
"The Man With The Harmonica" <co****************@yahoo.co.uk> wrote:
"Mark A. Odell" <od*******@hotmail.com> wrote in message
news:Xn********************************@130.133.1. 4...
"The Man With The Harmonica" <co****************@yahoo.co.uk> wrote
in news:cf**********@rdel.co.uk:
What kind of commands can I use to control the serial and parallel
port of my pc.


None. ISO C doesn't have any commands nor does it know anything
about serial or parrallel ports. You'll need to ask this in a
newsgroup that programs for the platform you are using, e.g. DOS,
Linux, Win32, etc.


I'm using an old Toshiba laptop with win95. I want to use the
parallel port to control some circuits and to read the state of some
control lines. Basically its going to be controlling a four wheeled
buggy and I want to do this using C and through the parallel port.


So go and ask on a a group for you platform as Mark suggested. Standard
C does not provide you with any way to to what you want and this group
talks about standard C.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #7

P: n/a
# So go and ask on a a group for you platform as Mark suggested. Standard
# C does not provide you with any way to to what you want and this group
# talks about standard C.

Still shooting from the hip and blowing off your own toes. Depending on
the operating system, it may be possible to fopen a device name and
then use stdio to read/write the port.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Why are we here?
whrp
Nov 14 '05 #8

P: n/a
SM Ryan wrote:

# So go and ask on a a group for you platform as Mark suggested. Standard
# C does not provide you with any way to to what you want and this group
# talks about standard C.

Still shooting from the hip and blowing off your own toes. Depending on
the operating system, it may be possible to fopen a device name and
then use stdio to read/write the port.

Exactly how stupid are you? There is no standard way to do that. There
are only platform-specific ways, and so the person seeking the
information should go ask on a newsgroup dedicated to the specific
platform.

Since you first graced our newsgroup, you've been a fount of bad
information and bad attitude, as well as continuing to use those
non-standard reply delimiters.

Brian Rodenborn
Nov 14 '05 #9

P: n/a
On Thu, 19 Aug 2004 21:33:23 -0000
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
# So go and ask on a a group for you platform as Mark suggested.
Standard# C does not provide you with any way to to what you want and
this group# talks about standard C.

Still shooting from the hip and blowing off your own toes. Depending ^^^^^^^^^ on the operating system, it may be possible to fopen a device name and ^^^^^^^^^^^^^^^^^^^^^^^ then use stdio to read/write the port.


You said it yourself, it DEPENDS ON THE OPERATING SYSTEM. Therefor it
needs to be asked in a group dealing with the operating system. If the
OP find it can be done using fopen and friends, then questions can be
asked about the things which can be done in standard C. However, if the
OP knew that was possible then the question would not have been asked.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #10

P: n/a
Default User <fi********@boeing.com.invalid> wrote:
# SM Ryan wrote:
# >
# > # So go and ask on a a group for you platform as Mark suggested. Standard
# > # C does not provide you with any way to to what you want and this group
# > # talks about standard C.
# >
# > Still shooting from the hip and blowing off your own toes. Depending on
# > the operating system, it may be possible to fopen a device name and
# > then use stdio to read/write the port.
#
#
# Exactly how stupid are you? There is no standard way to do that. There

Since when did fopen, fgets, fputs, iovbuf, and system stop being part
of ANSI C? How stupid are you?

# are only platform-specific ways, and so the person seeking the

Every mapping of a file name to some external entity is platform
specific. Does that mean you forbid discussion of all programs which
call fopen?

# Since you first graced our newsgroup, you've been a fount of bad
# information and bad attitude, as well as continuing to use those
# non-standard reply delimiters.

Tough shit. I'm not here for the approval of your clique. I'm here
for the lost who get chased away because of your kneejerk responses.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Nov 14 '05 #11

P: n/a
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
# On Thu, 19 Aug 2004 21:33:23 -0000
# SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
#
# > # So go and ask on a a group for you platform as Mark suggested.
# > Standard# C does not provide you with any way to to what you want and
# > this group# talks about standard C.
# >
# > Still shooting from the hip and blowing off your own toes. Depending
# ^^^^^^^^^
# > on the operating system, it may be possible to fopen a device name and
# ^^^^^^^^^^^^^^^^^^^^^^^
# > then use stdio to read/write the port.
#
# You said it yourself, it DEPENDS ON THE OPERATING SYSTEM. Therefor it

If you actually botherred to think about it for a moment, openning any
file DEPENDS ON THE OPERATING SYSTEM. Disk file, terminal, any file at
all. So is openning a disk file something you cannot do in standard C,
just because it requires a system to map file names into disk files?

# needs to be asked in a group dealing with the operating system. If the
# OP find it can be done using fopen and friends, then questions can be
# asked about the things which can be done in standard C. However, if the
# OP knew that was possible then the question would not have been asked.

That's a silly claim. I don't know if the OP knows whether his system
maps devices to file names or not. That part is system specific. But if
it is possible, then the device can be openned and read and written in
standard C. Explain exactly what part of the following code is not ANSI C.

#include <stdio.h>

static FILE *openport(int port,int speed,int csize,char parity) {
char path[100],conf[100];
sprintf(path,"/dev/ttyS%d",port);
sprintf(conf,"stty -f /dev/stty -f /dev/ttyS%d speed %d cs%d %s",
port,speed,csize,
parity=='n' ? "-parenb"
: parity=='o' ? "parenb parodd"
: "parenb -parodd");
if (!system(0)) return 0;
if (system(conf)) return 0;
return fopen(path,"r+");
}

And then tell me again how it is impossible to talk to a device in
ANSI C.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Wow. A sailboat.
Nov 14 '05 #12

P: n/a

"SM Ryan" <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote in
message news:10*************@corp.supernews.com...
Default User <fi********@boeing.com.invalid> wrote:
# SM Ryan wrote:
# >
# > # So go and ask on a a group for you platform as Mark suggested. Standard # > # C does not provide you with any way to to what you want and this group # > # talks about standard C.
# >
# > Still shooting from the hip and blowing off your own toes. Depending on # > the operating system, it may be possible to fopen a device name and
# > then use stdio to read/write the port.
#
#
# Exactly how stupid are you? There is no standard way to do that. There

Since when did fopen, fgets, fputs, iovbuf, and system stop being part
of ANSI C? How stupid are you?

# are only platform-specific ways, and so the person seeking the

Every mapping of a file name to some external entity is platform
specific. Does that mean you forbid discussion of all programs which
call fopen?

# Since you first graced our newsgroup, you've been a fount of bad
# information and bad attitude, as well as continuing to use those
# non-standard reply delimiters.

Tough shit. I'm not here for the approval of your clique. I'm here
for the lost who get chased away because of your kneejerk responses.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.


Well said. I agree with your philosophy: Usenet should be a useful place to
all comers, not just those 'elite' who think they are above the rest.

I tend to think they respond with "wrong group" because they either don't
know the answer or couldn't be bothered to work out the problems posed.
Nov 14 '05 #13

P: n/a

"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:ls************@brenda.flash-gordon.me.uk...
On Thu, 19 Aug 2004 21:33:23 -0000
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
# So go and ask on a a group for you platform as Mark suggested.
Standard# C does not provide you with any way to to what you want and
this group# talks about standard C.

Still shooting from the hip and blowing off your own toes. Depending

^^^^^^^^^
on the operating system, it may be possible to fopen a device name and

^^^^^^^^^^^^^^^^^^^^^^^
then use stdio to read/write the port.


You said it yourself, it DEPENDS ON THE OPERATING SYSTEM. Therefor it
needs to be asked in a group dealing with the operating system. If the
OP find it can be done using fopen and friends, then questions can be
asked about the things which can be done in standard C. However, if the
OP knew that was possible then the question would not have been asked.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.


Uhhhh.... the OP said he was rusty, and so didn't know the answer in the
first place. Dunce.
Nov 14 '05 #14

P: n/a
On Fri, 20 Aug 2004 12:50:53 +0100
"John Smith" <so*****@microsoft.com> wrote:
Well said. I agree with your philosophy: Usenet should be a useful
place to all comers, not just those 'elite' who think they are above
the rest.
If a group is filled with off-topic posts then it becomes useless
because you can't find the on-topic posts.
I tend to think they respond with "wrong group" because they either
don't know the answer or couldn't be bothered to work out the problems
posed.


I know a bit about Win95, which is what the OP is using. However, a lot
more expertise will be found on a Win95 programming group than here. So
it is actually in the OPs interest to go to a Win95 programming group in
order to get the best possible advice.

Also, the OP wants to read from the parallel port, something which may
not be as simple as using fopen because you may need to switch the
hardware in to it's bidirectional mode (either EPP or ECP), something
early centronics interfaces did not support. Help on this won't be
obtained here and any responses with errors suggesting how to do this
won't get corrected.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #15

P: n/a
SM Ryan wrote:
Since when did fopen, fgets, fputs, iovbuf, and system stop being part
of ANSI C? How stupid are you?
Since when are they guaranteed to have anything to do with serial and
parallel ports? They are for opening files.
Tough shit. I'm not here for the approval of your clique. I'm here
for the lost who get chased away because of your kneejerk responses.

The "lost" will be much better served by redirection to correct
newsgroups where they will receive complete, peer-reviewed answers
rather than your half-assed, unhelpful crap.


Brian Rodenborn
Nov 14 '05 #16

P: n/a
John Smith wrote:
"SM Ryan" wrote:
Default User <fi********@boeing.com.invalid> wrote:

.... snip ...

# Since you first graced our newsgroup, you've been a fount of
# bad information and bad attitude, as well as continuing to use
# those non-standard reply delimiters.

Tough shit. I'm not here for the approval of your clique. I'm
here for the lost who get chased away because of your kneejerk
responses.


Well said. I agree with your philosophy: Usenet should be a useful
place to all comers, not just those 'elite' who think they are
above the rest.

I tend to think they respond with "wrong group" because they
either don't know the answer or couldn't be bothered to work out
the problems posed.


If you want to discuss off-topic things you are quite free to get
your own newsgroup going. The alt hierarchy is quite easy to set
up. Then you have to persuade ISPs to carry it.

Ryan has already been plonked by many people for refusal to
co-operate. Do not emulate his idiocy.

If a news group discusses anything at all it becomes a morass and
totally meaningless. That is the purpose of having multiple
groups - each can stick to a general subject.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #17

P: n/a
Default User <fi********@boeing.com.invalid> wrote:
# SM Ryan wrote:
#
# > Since when did fopen, fgets, fputs, iovbuf, and system stop being part
# > of ANSI C? How stupid are you?
#
# Since when are they guaranteed to have anything to do with serial and
# parallel ports? They are for opening files.

What exactly is a file?

--
SM Ryan http://www.rawbw.com/~wyrmwif/
You face forward, or you face the possibility of shock and damage.
Nov 14 '05 #18

P: n/a
In article <cg**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
....
(Totally O/T)
The *right* thing is to use the OS interface to the serial port.
I would like to have heard you say that 20 years ago, in DOS days, when it
was well understood by everybody that the standardized, officially
supported, OS-supplied serial port APIs simply did not work, and thus any
program that needed performance had to talk directly to the hardware.
The kind of code you're showing belongs exclusively to device drivers and
is highly unlikely to work in userland programs. Imagine what happens
when two programs attempt to access the same port directly, at the same
time (assuming that it would be possible): complete chaos.


I often get the feeling that these (obviously OT) posts that we keep seeing
here are coming from people in obscure corners of the world, where they
don't realize that the world has, indeed, changed in the last 20 years
(i.e., that the assertions made above are, in fact, no longer true).

Nov 14 '05 #19

P: n/a
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
# On Thu, 19 Aug 2004 21:33:23 -0000
# SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
#
# > # So go and ask on a a group for you platform as Mark suggested.
# > Standard# C does not provide you with any way to to what you want and
# > this group# talks about standard C. Explain exactly what part of the following code is not ANSI C.
Your code does not open a serial port on my fully ANSI-C conforming
MS windows system. Also it does not open a serial port on my
Linux system, where my usual username does not have permission to
open those devices. Please respond when you have fixed your code
so that it works on all ANSI-C conforming systems (including those
with no serial ports).
#include <stdio.h>

static FILE *openport(int port,int speed,int csize,char parity) {
char path[100],conf[100];
sprintf(path,"/dev/ttyS%d",port);
sprintf(conf,"stty -f /dev/stty -f /dev/ttyS%d speed %d cs%d %s",
port,speed,csize,
parity=='n' ? "-parenb"
: parity=='o' ? "parenb parodd"
: "parenb -parodd");
Buffer overflow, on 64-bit systems
if (!system(0)) return 0;
Undefined behaviour: system() in the C standard library expects
a pointer and you gave it an int. (You should fix this by
including stdlib.h)
if (system(conf)) return 0;
return fopen(path,"r+");
}

And then tell me again how it is impossible to talk to a device in
ANSI C.


It is impossible to portably talk to a device in ANSI C.
Nov 14 '05 #20

P: n/a
# Your code does not open a serial port on my fully ANSI-C conforming
# MS windows system. Also it does not open a serial port on my
# Linux system, where my usual username does not have permission to
# open those devices. Please respond when you have fixed your code
# so that it works on all ANSI-C conforming systems (including those
# with no serial ports).

Sorry, kiddo, but wrong answer.

You're whinging that different implementations may have different results
for the same code, but this can happen for something as simple (4.2-4.1).

If you really want to claim that any code that can have different results
is not ANSI C, then you're saying all I/O and real arithmetic is not ANSI
C. Doesn't leave alot left. Even something like
fopen("abcdefghijklmnopqrstuvwxyz","w") will fail on the system C was
originally implemented on.

# > if (!system(0)) return 0;
#
# Undefined behaviour: system() in the C standard library expects
# a pointer and you gave it an int. (You should fix this by
# including stdlib.h)

I think you better read the definition of system() a little more closely.
And you should also learn how 0 is really parsed in C. The behaviour is
fully defined and produces an important result.

# It is impossible to portably talk to a device in ANSI C.

You answerred the wrong question. The original question was whether
it was possible to talk to a serial port or parallel port in ANSI C.
The answer is yes contingent on the system makes the ports accessible
and configurable through fopen, system, etc.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
God's a skeeball fanatic.
Nov 14 '05 #21

P: n/a
On Mon, 23 Aug 2004 07:11:34 -0000
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.org> wrote:
# Your code does not open a serial port on my fully ANSI-C conforming
# MS windows system. Also it does not open a serial port on my
# Linux system, where my usual username does not have permission to
# open those devices. Please respond when you have fixed your code
# so that it works on all ANSI-C conforming systems (including those
# with no serial ports).

Sorry, kiddo, but wrong answer.

You're whinging that different implementations may have different
results for the same code, but this can happen for something as simple
(4.2-4.1).

If you really want to claim that any code that can have different
results is not ANSI C, then you're saying all I/O and real arithmetic
is not ANSI C. Doesn't leave alot left. Even something like
fopen("abcdefghijklmnopqrstuvwxyz","w") will fail on the system C was
originally implemented on.
That is defined as opening the file in the specified mode if it is
possible to. opening /dev/whatever is not defined as opening a device,
it just happens that on some systems is does.
# > if (!system(0)) return 0;
#
# Undefined behaviour: system() in the C standard library expects
# a pointer and you gave it an int. (You should fix this by
# including stdlib.h)

I think you better read the definition of system() a little more
closely. And you should also learn how 0 is really parsed in C. The
behaviour is fully defined and produces an important result.
You did not include stdlib.h therefor the compiler does not know that
system takes a pointer and thus won't convert 0 to a null pointer. On
some implementations this will mean setting a data register to 0 and not
touching the address register from which the system function will read
the pointer value. So Old Wolf is correct in what he said.

You should learn C.
# It is impossible to portably talk to a device in ANSI C.

You answerred the wrong question. The original question was whether
it was possible to talk to a serial port or parallel port in ANSI C.
The answer is yes contingent on the system makes the ports accessible
and configurable through fopen, system, etc.


No, the original question was how to do it and your suggestion will not
work on most systems including the systems the OP is most likely to be
using. Therefor the question belongs on a group dedicated to the
appropriate system where they will know such things as how to get a
parallel port into a mode where you can read from it, something it may
not be in by default.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.