积极答复者
怎么知道哪个COM口对应GSM?

问题
答案
-
- 已标记为答案 Jiong ShiMVP, Moderator 2011年3月23日 3:54
全部回复
-
There are my codes below, any mistakes?
wchar_t Buff[36];
char CharBuff[256];
COMMTIMEOUTS ctos;
DCB dcb;
wsprintf(Buff, L"COM%d:", 4);//my GSM is COM4
HANDLE hFile = CreateFile(Buff, GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
if(!GetCommState(hFile, &dcb))
{
outputMsgIntoFile("GetCommState failed, error code is 0x%X\n", GetLastError());
CloseHandle(hFile);
return;
}
//PrintDCB(&dcb);
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.fParity = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = TRUE;
dcb.fDtrControl = 0;
dcb.fRtsControl = 0;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.fTXContinueOnXoff = 0;
SetupComm(hFile, 512, 512);
if(!SetCommState(hFile, &dcb))
{
outputMsgIntoFile("SetCommState failed, error code is 0x%X\n", GetLastError());
CloseHandle(hFile);
return;
}outputMsgIntoFile("COM opened.\n");
ctos.ReadIntervalTimeout = MAXDWORD;
ctos.ReadTotalTimeoutMultiplier = 0;
ctos.ReadTotalTimeoutConstant = 0;
ctos.WriteTotalTimeoutConstant = 5;
ctos.WriteTotalTimeoutMultiplier = 5;
if(!SetCommTimeouts(hFile, &ctos))
{
outputMsgIntoFile("SetCommTimeouts failed, error code is 0x%X\n", GetLastError());
CloseHandle(hFile);
return;
}
outputMsgIntoFile("SetCommTimeouts done");
sprintf(CharBuff, "AT\r\n");
DWORD dwWritten;
if(!WriteFile(hFile, &CharBuff[0], strlen(CharBuff), &dwWritten, NULL))
{
outputMsgIntoFile("WriteFile failed, error code is 0x%X\n", GetLastError());
CloseHandle(hFile);
return;
}
outputMsgIntoFile("WriteFile OK");
DWORD dwRead;
memset(&CharBuff[0], 0, 256);Sleep(15000);
DWORD dwErrorFlags;
COMSTAT ComStat ;
ClearCommError(hFile, &dwErrorFlags, &ComStat );
if(ComStat.cbInQue)
{
if(!ReadFile(hFile, &CharBuff[0], (ComStat.cbInQue > 200) ? 200 : ComStat.cbInQue, &dwRead, NULL))
{
outputMsgIntoFile("ReadFile failed, error code is 0x%X\n", GetLastError());
CloseHandle(hFile);
return;
}
outputMsgIntoFile("ReadFile OK, got:%s", CharBuff);
}
else
{
outputMsgIntoFile("no data");
}
CloseHandle(hFile);
}
else
{
outputMsgIntoFile("Can not opened");
} -
- 已标记为答案 Jiong ShiMVP, Moderator 2011年3月23日 3:54