Member Avatar for cayman

Hi there folks

I have done quite a bit of C programming for embedded environments and PC console programs but never for windows applications and only once before with the PC COM port.

When I did program for the COM port before it was on older machine, and used conio.h and dos.h. I understand these header files are no use anymore on newer windows machines as they worked with DOS.

I now need to be able to communicate with a USB device (USB to I2C converter) that appears as a COM port when plugged into the machine

Are there any good resources online for explaining how to use the COM port in C available online? Or even a bare bones example program?...
I've not had much luck with google etc, everyone these days seems to be using C# or VB :(

Also are there any good online resources available for an amateur to show how to create C windows applications?? again googling gives me only C# or VB :(


An old example program where I used conio.h and dos.h

/* Prog to send 'G' to Serial Com Port1 */
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int com2Address = 0x2f8;	/* Assign a var name to com2*/
int lCR = 0x2FB;       		/* Assign a var name to line control register*/
int dLLB = 0x2f8;              	/* Assign a var name to divisor latch low byte */

void main(void)
{
	outp(lCR, 0x8a); 	/* Set line control register to 10001010 */
	outp(dLLB, 0x18);	/* Set divisor latch low byte to 18h for 4800 bps */
        outp(lCR, 0x0a);	/* Set line control register to 00001010, turning off DLAB */
        outp(com2Address, 'G');	/* Set com2 to G */
}

use Windows communications functions. You can't access the ports directly as you did in MS-DOS. Instead, you have to open the com port using CreateFile(), then set up the port parameters with SetCommConfig(). After that you use ReadFile() and WriteFile() to read and write to the com port. For ReadFile() I put that in another thread so that it doesn't block the entire program.

Member Avatar for cayman

Hi thanks for the reply

That is very helpful and I will have to read it at more detail later.
However one issue, all functions seem to work of a "specified" port, this means the user must know what com port is the correct one, or the programmer must have a way of determining the correct one based on information from the port, like some type of device ID or string or something...

Is there a satndard approach to determining which COM port, out of all in-use COM ports, we would like to use?

thanks again for your help :)

In the old days of MS-DOS that was pretty easy -- the program just kept track of which ports were in use. My MS-DOS program had up to 40 comm ports. You can still use that approach but also have to be aware that other processes may open a port. So if CreateFile() fails to open the port then your program must assume that the port is in use by some other process. I haven't used multi-port boards on MS-Windows operating systems, but I assume the manufacturer supplies a device driver, and you will have to read their documentation for details on how to use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.