我打算用opencomm 方式打开串口com1,把数据通过gsm短信发送器发送出去,发现怎么也不能打开串口。必须用微软windows自带的超级终端 通信工具发生一条短信才能用程序发送,奇怪,请大侠帮助,如何解决问题,多谢!
如下类似代码无法打开串口,奇怪。
BOOL OpenComm()
{
DCB dcb;//串口控制块
COMMTIMEOUTS timeouts={//串口超时控制参数
100,//读字符间隔超时时间:
1, //读操作时每字符的时间:
500,//基本的读超时时间:
1, //基本的写超时时间:
500};//基本的写超时时间:
hComm=CreateFile("COM1",
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hComm==INVALID_HANDLE_VALUE) return FALSE;
GetCommState(hComm,&dcb);//取dcb
dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
SetCommState(hComm,&dcb);//设置dcb
SetupComm(hComm,4096,1024);//设置输入,输出缓冲区大小
SetCommTimeouts(hComm,&timeouts);//设置超时
return TRUE; }
//关闭串口
BOOL CloseComm()
{
return CloseHandle(hComm);}
如下类似代码无法打开串口,奇怪。
BOOL OpenComm()
{
DCB dcb;//串口控制块
COMMTIMEOUTS timeouts={//串口超时控制参数
100,//读字符间隔超时时间:
1, //读操作时每字符的时间:
500,//基本的读超时时间:
1, //基本的写超时时间:
500};//基本的写超时时间:
hComm=CreateFile("COM1",
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hComm==INVALID_HANDLE_VALUE) return FALSE;
GetCommState(hComm,&dcb);//取dcb
dcb.BaudRate=9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
SetCommState(hComm,&dcb);//设置dcb
SetupComm(hComm,4096,1024);//设置输入,输出缓冲区大小
SetCommTimeouts(hComm,&timeouts);//设置超时
return TRUE; }
//关闭串口
BOOL CloseComm()
{
return CloseHandle(hComm);}
DWORD dwBaudRate,
BYTE byParity,
BYTE byStopBits,
BYTE byByteSize)
{
DCB dcb; // structure that defines the control setting for a serial communications device
COMMTIMEOUTS CommTimeOuts;
BOOL bSuccess;
m_hCom = CreateFile(lpszPortNum, // pointer to name of the file
GENERIC_READ|GENERIC_WRITE, // access mode
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributs
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
FILE_FLAG_OVERLAPPED|FILE_ATTRIBUTE_NORMAL, // overlapped I/O
NULL); // hTemplate must be NULL for comm devices if ( m_hCom == INVALID_HANDLE_VALUE )
{
// handle the error
ComAccess::ErrorToString("Open(): CreateFile() failed, invalid handle value");
return FALSE;
} //
// Omit the call to SetupComm to use the default queue sizes.
// Get the current configuration.
//
CommTimeOuts.ReadIntervalTimeout = 10;
CommTimeOuts.ReadTotalTimeoutConstant = 30;
CommTimeOuts.ReadTotalTimeoutMultiplier = 10;
CommTimeOuts.WriteTotalTimeoutConstant = 10;
CommTimeOuts.WriteTotalTimeoutMultiplier = 10; bSuccess = SetCommTimeouts(m_hCom,&CommTimeOuts); if(!bSuccess)
{
// Handle the error.
ComAccess::ErrorToString("Open(): SetCommTimeouts() failed");
ComAccess::Close(); return FALSE;
} bSuccess = SetupComm(m_hCom,1024,1024); if(!bSuccess)
{
// Handle the error.
ComAccess::ErrorToString("Open(): SetCommTimeouts() failed");
ComAccess::Close(); return FALSE;
} bSuccess = GetCommState(m_hCom, &dcb); if ( ! bSuccess )
{
// Handle the error.
ComAccess::ErrorToString("Open(): GetCommState() failed");
ComAccess::Close(); return FALSE;
} //
// Fill in the DCB: baud=9600, 8 data bits, no parity, 1 stop bit are default parameters
// dcb.BaudRate = dwBaudRate;
dcb.ByteSize = byByteSize;
dcb.Parity = byParity;
dcb.StopBits = byStopBits;
bSuccess = SetCommState(m_hCom, &dcb); if ( ! bSuccess )
{
// Handle the error.
ComAccess::ErrorToString("Open(): SetCommState() failed");
ComAccess::Close(); return FALSE;
} PurgeComm(m_hCom, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); return TRUE;
}VOID ComAccess::Close(VOID)
{
if ( m_hCom > 0 )
{
CloseHandle(m_hCom);
} m_hCom = 0;
}