Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Program the COM Port

Status
Not open for further replies.

aryajur

Programmer
Jul 2, 2003
45
0
0
US
I need to program the COM port to read and write data from it. How can I do that in VC++. I tried the following program but it gives an exception when it is executed:

/* This program is used to pull data from the serial port */

#include <dos.h>
#include <stdio.h>
#include <conio.h>

#define PORT1 0x3F8 /* Defines Serial Port Base Address (COM1 */

void main(void){
unsigned char c = 0;
unsigned char chrctr = 0;
/*int exit = 1; */

_outp(PORT1 + 1, 0); /* Turn off interrupts */

/* PORT1 Communication Settings */

_outp(PORT1 + 3, 0x80); /* Set DLAB ON */
_outp(PORT1 + 0, 0x0C); /* Set the baud rate to 9600 */

_outp(PORT1 + 1, 0x00); /* Set Baud - Divisor latch HIGH */
_outp(PORT1 + 3, 0x03); /* 8 bits, no parity, 1 stop */
_outp(PORT1 + 2, 0xC7); /* FIFO Control Register */
_outp(PORT1 + 4, 0x0B); /* Turn on DTR, RTS, and OUT2) */

printf("Waiting on transmission from source.\nPress ESC to quit.\n");

while(chrctr != 27){ /* Execute the loop if ESC has been hit */
c = _inp(PORT1 + 5);
if (c & 0x01){
chrctr = _inp(PORT1);
printf("%d",chrctr);
}
if (kbhit()){
chrctr = getch();
_outp(PORT1, chrctr);
}
}
}


Any suggestions or tips are welcome.

 
The way you get access to the serial port in win32 is by using the CreateFile API call
Look for "Communications Resources"

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top