RS232 serial port access

28 views
Skip to first unread message

Denis

unread,
Dec 20, 2008, 8:46:17 AM12/20/08
to Visual C++ Lovers
hi...
I'm trying to write simple program to communicate with RS232 port, but
have error message with it:

the code:
#include <iostream>

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

using namespace std;

#define PORT1 0x3F8

void main () {

//int _inp( unsigned short port ); to read a byte in from a data
port
//int _outp( unsigned short port, int databyte ); to write a byte
to a data
int c;
int ch;
_outp(PORT1 + 1 , 0); /* Turn off interrupts - Port1 */

/* PORT 1 - Communication Settings */
_outp(PORT1 + 3 , 0x80); /* SET DLAB ON */
_outp(PORT1 + 0 , 0x03); /* Set Baud rate - Divisor Latch Low Byte
*/
/* Default 0x03 = 38,400 BPS */
/* 0x01 = 115,200 BPS */
/* 0x02 = 56,700 BPS */
/* 0x06 = 19,200 BPS */
/* 0x0C = 9,600 BPS */
/* 0x18 = 4,800 BPS */
/* 0x30 = 2,400 BPS */
_outp(PORT1 + 1 , 0x00); /* Set Baud rate - Divisor Latch High
Byte */
_outp(PORT1 + 3 , 0x03); /* 8 Bits, No Parity, 1 Stop Bit */
_outp(PORT1 + 2 , 0xC7); /* FIFO Control Register */
_outp(PORT1 + 4 , 0x0B); /* Turn on DTR, RTS, and OUT2 */
printf("\nSample Comm's Program. Press ESC to quit \n");
do { c = _inp(PORT1 + 5); /* Check to see if char has been */
/* received. */
if (c & 1) {
ch = _inp(PORT1); /* If so, then get Char */
printf("%c",ch);} /* Print Char to Screen */
if (kbhit()){ch = getch(); /* If key pressed, get Char */
_outp(PORT1, ch);} /* Send Char to Serial Port */
} while (ch !=27); /* Quit when ESC (ASC 27) is pressed */
}

the error:
Unhandled exception at 0x004113ec in rs232_trial.exe: 0xC0000096:
Privileged instruction.

on first call to _outp

can anyone suggest solution?

Thanx,
Denis

Denis

unread,
Dec 20, 2008, 8:46:17 AM12/20/08
to Visual C++ Lovers
Reply all
Reply to author
Forward
0 new messages