Advertisement

Serial port woes

Started by January 13, 2001 04:38 PM
9 comments, last by Eisen 24 years ago
I need to send twelve byte instruction packets out of the serial port in the back of my computer and into a waiting peripheral that will send back an equal sized acknowledge packet. Would someone be kind enough to tell me how to access the port? I need: The name of the functions that read and write data to the serial port. The function prototypes and where they are located. I''m assuming one of the standard library files has these functions. Instructions on how to use these functions properly. or Directions on where to get this information. I''ve consulted my teachers, text, and my compiler''s help directory to no avail. I realize this is a lengthy and time consuming request but I would be much obliged if someone would take the time to help me out. If any part of my request is unclear or you have any questions about what I''m trying to do, feel free to ask. Thanks in advance, Andrew
It would be rather handy if you told us what operating system you were using.

If it''s windows, use CreateFile, look it up in the MSDN and it gives examples of writing to a com port.
Advertisement
My apologies.

OS - Windows98
Compiler - VC++
Look in the MSDN
Platform SDK->WIndows Base Services->Files and IO->Communications

CreateFile, CloseHandle, ReadFile, WriteFile and so on..
remember the dos days? like outp or something? darn windows api, ugh.
The msdn docs are inaccurate for serial comms, btw.
I''ll try to find my CComPort class & post it...
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Why are they inaccurate? I found nowt wrong with them and made several serial port applications without a problem.
I''ve had no problems using outp in windows, but the headers in my windows compiler don''t have it. I just stold a chunk of code and put it in a header.

extern __inline__ unsigned char
inp (unsigned short _port)
{
unsigned char rv;
__asm__ __volatile__ ("inb %1, %0"
: "=a" (rv)
: "dN" (_port));
return rv;
}

extern __inline__ void
outp (unsigned short _port, unsigned char _data)
{
__asm__ __volatile__ ("outb %1, %0"
:
: "dN" (_port),
"a" (_data));
}

The address of LPT1 is 0x378.

I hope that helps.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
First, I appreciate the help I've received thus far.

smart_idiot,

Your suggestion seems the best as far as ease of implementation goes. However, are you sure that the header code is correct? Compiling as is, I get a ridiculous syntax error saying I should put a ';' before the 'unsigned char' in the first function declaration and an unexpected end of file error.

That aside, I looked at the code and am a little confused. Correct me if I'm wrong but aren't those keywords written '__asm' and '__inline' as opposed to '__asm__' and '__inline__'? If those aren't keywords, then where are they defined?

Lastly, isn't LPT1 the parallel port? I need the address of COM1 the serial port. Hopefully the addresses are universal. If not, I need a port address querying function.


Anyone's thoughts or input on the matter is welcome and appreciated,

Andrew


Edited by - Eisen on January 16, 2001 2:16:44 PM
Try this link for a serialport class from codeguru.com
http://codeguru.earthweb.com/network/serialport.shtml
I have used this code to interface with a bunch of different
devices such as wireless modems, gps recievers, single board
computers and robotic control circuitry. This should be very straightforward to port into your app. I would suggest using an off the shelf serial component such as greenleaf''s COMMX or something like that as they are already debugged and ready to go.
For something simple like you are talking about, you may want
to write your own serial interrupt routines for writing and
reading from a particular port. I could send you some code
examples to do this in WIN/98 if you need.

Kris Herrera
Computer Engineer
Sparton Technology Inc.

This topic is closed to new replies.

Advertisement