Click to See Complete Forum and Search --> : how to use a commport


Defiant
July 31st, 2002, 07:48 AM
Heya all,

my problem is as follows.
I want to find the comports on a computer and send data to it.

i was looking for hyperterminal source code, but unlikely that i'll find that.
So i was hoping that one of you had a little program that could detect the commport and write and receive data from it.
detection stuff and all that is not neccesary at this moment. i'll hack that later to it.

if anyone has it or can point me somewhere, then i would be very gratefull.

zdf
July 31st, 2002, 09:38 AM
See the attachment.

Regards,

Clifford
July 31st, 2002, 09:40 AM
HI ...

Found in MSDN :

char *pcCommPort = "COM2";

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

Opens commport COM2 ..

Hope this helps :)

See u. Cliff

jontom
July 31st, 2002, 10:02 AM
I was looking into this a few months ago when writing a serial port handling class. There is actually quite a lot of stuff out there. Look in the VisualC++ forum archives. It is all slightly different, and one class that EVERYONE was quoting was FULL of bugs.

I am not sure I can give you my code since I wrote it on company time, but if you are really, really desparate, drop me an email at jontomxire@hotmail.com for some tips/chunks of code.

Defiant
August 1st, 2002, 04:14 AM
thanks for all the advice.

The tty.zip is a very long source code file with a lot of windows handle source. I sort of understand the parts of writing, reading, opening, closing the ports. A similar piece of the MSDN that Clifford also appears in the tty.zip.
But i can't compile the file, cause it needs a windows form, i believe, I can't make heads or tails out of all those windows handle names.

I am trying to filter out a simple way to write and read to a commport and offcourse detect commport 2. (or is comport to always at the fixed address. Is there some way you can use the static address of comport 2?

I also have the rs232.c and rs232.h file at my disposal. It should be very easy to use them, but i can not really figure out how.
I welcome any insight in understanding this kind of material. in the sdk of win32 it isnt all that clear and doenst give me the big picture of all the elements i should take into account.


ps. i am looking in the VC++ at this moment, hoping to find some
stuff.

jontom
August 1st, 2002, 04:27 AM
You're right, MSDN is absolutely cr&p and confusing when it comes to COM ports.

Ok, here are some points to make;

Com ports don't have addresses as such. To open com port 2, you just use the string "COM2" in the function call. Presumably if there is no com port 2 it returns an error.

Com ports can be opened using overlapped IO or not. I don't know how much you know about overlapped IO. It is quite a neat idea in principle, but it sucks in many respects, not least is it's sheer complexity. If your app is a console app, or you are only reading or only writing, you may be able to get away with not using overlapped IO. Otherwise, you need to spawn a separate thread just to handle the com port, AND use overlapped IO.

If you email me at jontomxire@hotmail.com, I will send you some sample code that is neatly and clearly written. When I was looking into this, I couldn't find much that was clearly written, and one class I found was SOOOOOO full of obvious bugs. It was quite clearly written though :)

Clifford
August 1st, 2002, 04:32 AM
Originally posted by Defiant
thanks for all the advice.
I am trying to filter out a simple way to write and read to a commport and offcourse detect commport 2. (or is comport to always at the fixed address. Is there some way you can use the static address of comport 2?

Ok ... can't imagine a simpliest way than using CreateFile ..
What's the problem with that?
Just CreateFile, then you can use ReadFile, WriteFile, etc ...
and CloseHandle to close it.
You don't need to use the static address. This address is defined in your system and you just have to use "COM1", "COM2" (i think it works for "LPT1", etc ...


ps. i am looking in the VC++ at this moment, hoping to find some
stuff.

I found everything in the MSDN, in the VC++ section actually.. You'll find what you want really soon :)

Cheers :p

jhreich
August 1st, 2002, 10:51 AM
Can someone please have a look at the MSDN ReadFile documentation and let me know what they mean by

'If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation.'

This was taken from the Return Values section in ReadFile.

I am working in WinCE and I am always getting a return value of non zero, yet with the number of bytes read 0.

Any help please?