use of serial and parrallel port using C

  • Thread starter The Man With The Harmonica
  • Start date
T

The Man With The Harmonica

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
 
M

Mark A. Odell

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

Thomas Matthews

The said:
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


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
 
S

SM Ryan

# 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".
 
D

Dan Pop

^^^^^ ^^^^^^^^^
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
 
T

The Man With The Harmonica

Mark A. Odell said:
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.

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
 
F

Flash Gordon

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

SM Ryan

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

Default User

SM said:
# 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
 
F

Flash Gordon

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

SM Ryan

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

SM Ryan

# On Thu, 19 Aug 2004 21:33:23 -0000
#
# > # 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.
 
J

John Smith

SM Ryan said:
# 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.

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

John Smith

Flash Gordon said:
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.

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

Flash Gordon

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

Default User

SM said:
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
 
C

CBFalconer

John said:
.... snip ...

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

SM Ryan

# 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?
 
K

Kenny McCormack

....
(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).
 
O

Old Wolf

SM Ryan said:
# On Thu, 19 Aug 2004 21:33:23 -0000
#
# > # 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top