BOOL serial_ctrl::open_set_serial_port(LPCTSTR serial_port, DCB dcb) { serial_handle = CreateFile( serial_port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if( serial_handle == (HANDLE)0xFFFFFFFF) { AfxMessageBox("Open the serial port wrong"); return FALSE; } if( GetCommState(serial_handle, &config) == 0) { AfxMessageBox(_T("Wrong in get configuration port.")); return FALSE; } //set new configuration //以后添加修改波特率的bottom /* config.BaudRate = dcb.BaudRate; config.StopBits = dcb.StopBits; config.Parity = dcb.Parity; config.ByteSize = dcb.ByteSize; */ if( SetCommState(serial_handle, &config) == 0) { AfxMessageBox(_T("Wrong in set config of serial port")); return FALSE; } //set the buffer of sending and reciving SetupComm(serial_handle, 1024, 1024); PurgeComm(serial_handle, PURGE_TXCLEAR);
//set the time out parity COMMTIMEOUTS com_time_out; memset(&com_time_out, 0, sizeof(com_time_out)); GetCommTimeouts(serial_handle, &com_time_out);//get the time out set now //set the read time out //return at once while get one charater com_time_out.ReadIntervalTimeout = MAXDWORD; com_time_out.ReadTotalTimeoutConstant = 0; com_time_out.ReadTotalTimeoutMultiplier = 0; //set the write time out com_time_out.WriteTotalTimeoutConstant = 2; com_time_out.WriteTotalTimeoutMultiplier = 3; //set the time out parity SetCommTimeouts(serial_handle, &com_time_out); return TRUE; } 我vc下的参数设置如上?不知道哪里错了~
{
serial_handle = CreateFile( serial_port,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL); if( serial_handle == (HANDLE)0xFFFFFFFF)
{
AfxMessageBox("Open the serial port wrong");
return FALSE;
} if( GetCommState(serial_handle, &config) == 0)
{
AfxMessageBox(_T("Wrong in get configuration port."));
return FALSE;
} //set new configuration
//以后添加修改波特率的bottom
/*
config.BaudRate = dcb.BaudRate;
config.StopBits = dcb.StopBits;
config.Parity = dcb.Parity;
config.ByteSize = dcb.ByteSize;
*/ if( SetCommState(serial_handle, &config) == 0)
{
AfxMessageBox(_T("Wrong in set config of serial port"));
return FALSE;
} //set the buffer of sending and reciving
SetupComm(serial_handle, 1024, 1024);
PurgeComm(serial_handle, PURGE_TXCLEAR);
//set the time out parity
COMMTIMEOUTS com_time_out;
memset(&com_time_out, 0, sizeof(com_time_out));
GetCommTimeouts(serial_handle, &com_time_out);//get the time out set now //set the read time out
//return at once while get one charater
com_time_out.ReadIntervalTimeout = MAXDWORD;
com_time_out.ReadTotalTimeoutConstant = 0;
com_time_out.ReadTotalTimeoutMultiplier = 0; //set the write time out
com_time_out.WriteTotalTimeoutConstant = 2;
com_time_out.WriteTotalTimeoutMultiplier = 3; //set the time out parity SetCommTimeouts(serial_handle, &com_time_out); return TRUE;
}
我vc下的参数设置如上?不知道哪里错了~