views:

818

answers:

4

Using vc++ compiler how one can access serial port. Bioscom() function can be used in Turbo C.

+1  A: 

This page on the Microsoft Developer Network introduces how to work with serial ports in Windows, which I assume is the environment you want to target, based on your choice of compiler.

unwind
A: 

The bios functions are only available if you are using MSDOS or very old versions of Windows (andc are specific to Turbo C). For modern versions of Windows, you will need to use the OS APIs to perform serial I/O.

anon
To access serial port in windows xp, what are the OS APIs?
Shashikiran
See the link posted by unwind in his answer
anon
+2  A: 

Hi, you have to open the appropriate com-device with CreateFile like so. Adapt to your needs.

// Handle of the communication connection 
void *comHandle;

// Port parameters, set to your own needs
unsigned portIndex;
unsigned baudRate;
unsigned dataBits;
Parity   parity;
unsigned stopBits;
bool     handShake;
int      readIntervalTimeout;
int      readTotalTimeoutMultiplier;
int      readTotalTimeoutConstant;
int      writeTotalTimeoutMultiplier;
int      writeTotalTimeoutConstant;
DCB dcb;
COMMTIMEOUTS ct;

// Create COM-device name string 
char comDevice[20];
sprintf(comDevice, "\\\\.\\COM%d", portIndex);

// Open serial port
_comHandle = CreateFile(comDevice, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (comHandle == INVALID_HANDLE_VALUE)
{
  return false;
}


ct.ReadIntervalTimeout         = readIntervalTimeout;        
ct.ReadTotalTimeoutMultiplier  = readTotalTimeoutMultiplier; 
ct.ReadTotalTimeoutConstant    = readTotalTimeoutConstant;   
ct.WriteTotalTimeoutMultiplier = writeTotalTimeoutMultiplier;
ct.WriteTotalTimeoutConstant   = writeTotalTimeoutConstant;  

if (!GetCommState(_comHandle,&dcb))
{
  disconnect();
  return false;
}

dcb.BaudRate        = baudRate;  
dcb.ByteSize        = (BYTE)dataBits;
dcb.Parity          = (parity == None) ? NOPARITY : ((parity == Even) ? EVENPARITY : ODDPARITY);
dcb.StopBits        = (stopBits > 1) ? TWOSTOPBITS : ONESTOPBIT;
dcb.fRtsControl     = handShake ? RTS_CONTROL_HANDSHAKE : RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow    = handShake;
dcb.fOutxDsrFlow    = handShake;
dcb.fDtrControl     = handShake ? DTR_CONTROL_HANDSHAKE : DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = handShake;
dcb.fOutX           = FALSE;
dcb.fInX            = FALSE;
dcb.fErrorChar      = FALSE;
dcb.fNull           = FALSE;
dcb.fAbortOnError   = TRUE;

// Set port state
if( !SetCommState(_omHandle, &dcb) ||
    !SetCommTimeouts(comHandle, &ct) ||
    !SetupComm(comHandle, 64, 64) ||
    !PurgeComm(comHandle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR))
{
  disconnect();
  return false;
}

Read the appropriate MSDN entries for the various functions called. Also, I've left out the disconnect method for brevity reasons.

jhwist
+1  A: 

They are many articles in Code Project regarding serial communication with C++. This is the first article returned. You basically access the port with file I/O operations. It is a bit complicated and I recommend finding an appropriate library for this task.

kgiannakakis