Какие различия при конфигурировании Com порта в Xp и 98 ?
Вот по такому инишнику конфигурируеться порт.
; Port number. Available values: (1, 2, 3, 4)
PORT_NUMBER=2
;
; Baud rate. Available values: (110, 300, 600, 1200, 2400, 4800, 9600, 14400,
; 19200, 38400, 56000, 57600, 115200)
BAUD_RATE=115200
;
; Data bits on controller. Available values: (7, 8)
DATA_BITS=8
;
; Parity check. Available values: (0-None, 1-Odd parity, 2-Even parity,
; 3-Mark parity, 4-Space parity)
PARITY=2
;
;Stop bits quantity. Available values: (0-1 Stop bit, 1-1.5 Stop bits,
; 2-2 Stop bits)
STOP_BITS=0
;
; DTR/DSR flow control. Available values: (0-Disable, 1-Enable, 2-Handshake)
DTR_DSR=0
;
; RTS/CTS flow control. Available values: (0-Disable, 1-Enable,
; 2-Handshake, 3-Toggle)
RTS_CTS=0
;
; XON/XOFF flow control. Available values: (0-Disable, 1-Enable)
XON_XOFF=0
;
; XON/XOFF characters. Actual only if XON_XOFF=1. Available values: (0..255)
; Default values: (XON_CHAR=17, XOFF_CHAR=19)
XON_CHAR=17
XOFF_CHAR=19
;
; Send address (for RS-485)
SEND_ADDR=0x48
;
; Receive address (for RS-485)
RECV_ADDR=0x48
А вот струкруты, нужные для понимания кода ниже.
typedef struct tagRsConfig {
HANDLE RsDev;
COMMTIMEOUTS RsTimeouts;
DCB RsDcb;
unsigned char SndAddr;
unsigned char RecvAddr;
BYTE RsID;
DWORD NRecvPack;
DWORD NSendPack;
} /*RS_CONFIG, *LPRS_CONFIG,*/ RsConfigType;
typedef struct tagRsParam
{ DWORD PortNum; /* COM-port number */
DWORD Baud; /* Baud rate */
DWORD DataBits; /* Quantity of data bits */
DWORD Parity; /* Parity check */
DWORD StopBits; /* Quantity of stop bits */
DWORD DtrDsr; /* DTR/DSR flow control */
DWORD RtsCts; /* RTS/CTS flow control */
DWORD XonXoff; /* XON/XOFF flow contol */
DWORD XonCh; /* XON character */
DWORD XoffCh; /* XOFF character */
DWORD SndAddr;
DWORD RecvAddr;
} /*RS_PARAM, *LPRS_PARAM,*/ RsParamType;
А вот код конфигурирования
int RsSetup( RsParamType *SetupParam, RsConfigType *RsCfg )
{
char PortName[16];
memset( RsCfg, 0, sizeof(RsConfigType) );
RsCfg->SndAddr = (unsigned char)SetupParam->SndAddr;
RsCfg->RecvAddr = (unsigned char)SetupParam->RecvAddr;
sprintf( PortName, "COM%d", SetupParam->PortNum );
RsCfg->RsDev = CreateFile( PortName,
GENERIC_READ|GENERIC_WRITE,
0, /* Exclusive access */
NULL, /* No security attrs */
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL /*|*/ /* overlapped I/O */
/*FILE_FLAG_OVERLAPPED*/,
NULL );
if ( RsCfg->RsDev == (INVALID_HANDLE_VALUE) ) {
return RS_ERR_OFILE;
}
if ( SetCommMask(RsCfg->RsDev,EV_RXCHAR) == FALSE ) {
return RS_ERR_COMM_MASK;
}
if ( SetupComm(RsCfg->RsDev,RS_RX_SIZE,RS_TX_SIZE) == FALSE ) {
return RS_ERR_SETUP_COMM;
}
if ( PurgeComm(RsCfg->RsDev,PURGE_TXABORT|PURGE_RXABORT|
PURGE_TXCLEAR|PURGE_RXCLEAR) == FALSE ) {
return RS_ERR_PURGE_COMM;
}
RsCfg->RsTimeouts.ReadIntervalTimeout = 0;//MAXDWORD;
RsCfg->RsTimeouts.ReadTotalTimeoutMultiplier = 0;
RsCfg->RsTimeouts.ReadTotalTimeoutConstant = 0;//RS_RX_MAXTIME;
RsCfg->RsTimeouts.WriteTotalTimeoutMultiplier = 10;
RsCfg->RsTimeouts.WriteTotalTimeoutConstant = RS_TX_SIZE;
if ( SetCommTimeouts(RsCfg->RsDev,&(RsCfg->RsTimeouts)) == FALSE ) {
return RS_ERR_TIMEOUTS;
}
RsCfg->RsDcb.DCBlength = sizeof( DCB );
if ( GetCommState(RsCfg->RsDev, &(RsCfg->RsDcb)) == FALSE ) {
return RS_ERR_GETCOMMSTATE;
}
// main parameters setup
RsCfg->RsDcb.BaudRate = SetupParam->Baud;
RsCfg->RsDcb.ByteSize = (BYTE)(SetupParam->DataBits);
RsCfg->RsDcb.fParity = (SetupParam->Parity) ? (1) : (0);
if ( RsCfg->RsDcb.fParity ) {
RsCfg->RsDcb.Parity = SetupParam->Parity;
}
RsCfg->RsDcb.fBinary = TRUE;
RsCfg->RsDcb.StopBits = (BYTE)(SetupParam->StopBits);
// setup hardware flow control
switch( SetupParam->DtrDsr ) {
case 0 : RsCfg->RsDcb.fDtrControl = DTR_CONTROL_DISABLE;
RsCfg->RsDcb.fDsrSensitivity = FALSE;
break;
case 1 : RsCfg->RsDcb.fDtrControl = DTR_CONTROL_ENABLE;
RsCfg->RsDcb.fDsrSensitivity = TRUE;
break;
case 2 : RsCfg->RsDcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
RsCfg->RsDcb.fDsrSensitivity = TRUE;
break;
default: MessageBox( NULL, "Incorrect value of DTR/DSR parameter",
"Parameters error", MB_OK|MB_ICONHAND );
}
switch( SetupParam->RtsCts ) {
case 0 : RsCfg->RsDcb.fRtsControl = RTS_CONTROL_DISABLE;
break;
case 1 : RsCfg->RsDcb.fRtsControl = RTS_CONTROL_ENABLE;
break;
case 2 : RsCfg->RsDcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
break;
case 3 : RsCfg->RsDcb.fRtsControl = RTS_CONTROL_TOGGLE;
break;
default: MessageBox( NULL, "Incorrect value of RTS/CTS parameter",
"Parameters error", MB_OK|MB_ICONHAND );
}
switch( SetupParam->XonXoff ) {
case 0 : RsCfg->RsDcb.fOutX = FALSE;
RsCfg->RsDcb.fInX = FALSE;
break;
case 1 : RsCfg->RsDcb.fOutX = TRUE;
RsCfg->RsDcb.fInX = TRUE;
RsCfg->RsDcb.XonChar = (BYTE)SetupParam->XonCh;
RsCfg->RsDcb.XoffChar = (BYTE)SetupParam->XoffCh;
break;
default: MessageBox( NULL, "Incorrect value of XON/XOFF parameter",
"Parameters error", MB_OK|MB_ICONHAND );
}
if ( SetCommState(RsCfg->RsDev,&(RsCfg->RsDcb)) == FALSE ) {
return RS_ERR_SETCOMMSTATE;
}
RsCfg->NRecvPack = 0;
RsCfg->NSendPack = 0;
RsCfg->RsID = SetupParam->PortNum;
а вот код закрытия порта ... на функции CloseHandle всё виснет. на функции WriteFile тоже.
if ( CloseHandle(RsCfg->RsDev) ) {
memset( RsCfg, 0, sizeof(RsConfigType) );
return RS_OK;
}