📄 rs232module.cpp
字号:
COMSTAT ComState = {0};
// clear com error
if(::ClearCommError(hCOM, &ComError, &ComState)) {
;
}
}
else if( wbytes < nPerSend ) {
// update the counts of sent bytes
nSentBytes += wbytes;
DWORD dret = GetLastError();
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dret,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Display the string.
OutputDebugString((LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
strDebug.Format(_T("Rs232Module::Write(): code:%d, Retry(%d): WriteFile()=FALSE, send(%d/%d), total(%d/%d).\n"), dret, nRetryCount, wbytes, nPerSend, nSentBytes, size);
OutputDebugString(strDebug);
DWORD ComError = 0;
COMSTAT ComState = {0};
// clear com error
CleanData();
}
else {
// update the counts of sent bytes
nSentBytes += wbytes;
// reset retry count and continue to skip retry
nRetryCount = 0;
continue;
}
// check retry count
if( 1 <= nRetryCount ) {
strDebug.Format(_T("Rs232Module::Write(): Retry(%d): retry %d times failed!, send(%d/%d), total(%d/%d).\n"), nRetryCount, nRetryCount, wbytes, nPerSend, nSentBytes, size);
OutputDebugString(strDebug);
return false;
}
// increase retry count
nRetryCount++;
// receiver might be in busy status, sleep awhile
Sleep(100);
}
return true;
}
bool Rs232Module::Suspend()
{
return true;
}
bool Rs232Module::Resume()
{
return true;
}
bool Rs232Module::GetPduToBuf(char ch)
{
if(ch == cr)
{
return false;
}
else if(ch == lf)
{
return true;
}
else
{
m_pduBuf[m_idxPduBuf++]=ch;
}
return false;
}
bool Rs232Module::GetPduToBuf()
{
unsigned char ch;
unsigned char state=0;
m_idxPduBuf = 0;
while(GetRawData(ch))
{
switch(state)
{
case STATE_WAIT_CR:
if(ch == cr)
state = STATE_WAIT_LF;
else
m_pduBuf[m_idxPduBuf++]=ch;
break;
case STATE_WAIT_LF:
if(ch == lf)
{
m_pduBuf[m_idxPduBuf]='\0';
#ifdef SP_LEVEL1
TRACE("GetPduToBuf:%s\n", m_pduBuf);
#endif
return true;
}
break;
default:
break;
}
}
GetDeviceData();
if(GetPduToBuf(m_idxPduBuf, &state))
{
return true;
}
return false;
}
bool Rs232Module::GetPduToBuf(int &idx, unsigned char *state)
{
unsigned char ch;
while(GetRawData(ch))
{
switch((*state))
{
case STATE_WAIT_CR:
if(ch == cr)
*state = STATE_WAIT_LF;
else
m_pduBuf[m_idxPduBuf++]=ch;
break;
case STATE_WAIT_LF:
if(ch == lf)
{
m_pduBuf[m_idxPduBuf]='\0';
#ifdef SP_LEVEL1
TRACE("GetPduToBuf:%s\n", m_pduBuf);
#endif
return true;
}
break;
default:
break;
}
}
GetDeviceData();
if(GetPduToBuf(idx, state))
{
return true;
}
return false;
}
bool Rs232Module::GetDeviceData()
{
COMSTAT comstat;
DWORD errors;
while(1)
{
::ClearCommError(hCOM, &errors, &comstat);
if( !comstat.cbInQue )
{
Sleep(1);
continue;
}
int dwBytesRead = (DWORD) comstat.cbInQue;
if( 512 <= (int) dwBytesRead )
dwBytesRead = 512;
if( ::ReadFile(hCOM, rawchars, dwBytesRead, &dwRead, NULL) )
{
if( dwRead==0 )
continue;
m_iRead = dwRead;
DumpData(rawchars, m_iRead);
m_idxReadRawBuffer = 0;
#ifdef D_DEBUG
string sDebug((const char *)rawchars, m_iRead);
sDebug[m_iRead]='\0';
TRACE("GetDeviceData: %s\n", sDebug.c_str());
#endif
return true;
}
}
return true;
}
bool Rs232Module::GetOneCrLf(int &state, unsigned char *data, int dataLength)
{
unsigned char ch;
#ifdef SP_DEBUG
char buffer[1024];
memcpy(buffer, data, dataLength);
buffer[dataLength] = '\0';
TRACE("GetOneCrLf from driver:%s\n", buffer);
#endif
#ifdef SP_LEVEL1
TRACE("\tGetOneCrLf()\n");
#endif
while(GetRawData(ch))
{
if(::WaitForSingleObject(m_hResetEvt, 0) == WAIT_OBJECT_0)
{
#ifdef SP_LEVEL1
TRACE("\tGetOneCrLf(): exit by reset event\n");
#endif
break;
}
FillBufferByState( ch, state);
}
return true;
}
void Rs232Module::ResetGetPduState()
{
m_idxPduBuf=0;
}
void Rs232Module::FillBufferByState(unsigned char ch, int &state)
{
// unsigned char ch2;
#ifdef SP_LEVEL1
TRACE("\t\tFillBufferByState(): Enter\n");
#endif
switch(state)
{
case 0:
// Get the first tag of CR
#ifdef SP_LEVEL1
TRACE("\t\t\tstate = 0\n");
#endif
if(ch == GetCrByte())
{
state = 1;
m_respCommand[m_idxRespCommand]='\0';
}
break;
case 1:
// Get the first tag of Lf
#ifdef SP_LEVEL1
TRACE("\t\t\tstate = 1\n");
#endif
if(ch == GetLfByte())
{
state = 2;
writeIndex = 0;
}
break;
case 2:
// Save the Data to respDatabuf if not the second CR
#ifdef SP_LEVEL1
TRACE("\t\t\tstate = 2\n");
#endif
if(m_waitGtAndSpace)
{
#ifdef SP_LEVEL1
TRACE("\t\t\tstate = 2 in wait GT spac\n");
#endif
if(CheckIfGetGtandSpace(ch))
{
::SetEvent(m_hWaitGtAndSpace);
m_waitGtAndSpace = false;
state = 0;
}
}
else
{
if(ch!=GetCrByte())
{
if(writeIndex<RESP_BUFFER_LENGTH)
respDataBuf[writeIndex++]=ch;
}
else
{
if(writeIndex!=1)
{
// respDataBuf not an empty string
state = 3;
}
else
{
// respDataBuf an empty string
// It must get into the condition of
// 0x0d 0x0a 0x0d 0x0A
state = 1;
writeIndex = 0;
}
}
}
break;
case 3:
// Get the 2nd Lf
#ifdef SP_LEVEL1
TRACE("\t\t\tstate = 3\n");
#endif
if(ch == GetLfByte())
{
if(!m_waitGtAndSpace)
{
respDataBuf[writeIndex]='\0';
#ifdef SP_DEBUG
TRACE("FillBufferByState for parsing: %s\n", respDataBuf);
#endif
AnalyingRespString();
state = 0;
writeIndex = 0;
m_idxRespCommand=0;
}
}
break;
default:
break;
}
}
void Rs232Module::SetWaitGtAndSpace(bool flag)
{
m_waitGtAndSpace = flag;
}
void Rs232Module::SetPduFlag(bool flag)
{
pduFlag = flag;
}
bool Rs232Module::CheckIfGetGtandSpace(char *data, int index)
{
if(data[index]=='>' && data[index+1]==' ')
return true;
else
return false;
}
bool Rs232Module::CheckIfGetGtandSpace(char a)
{
unsigned char ch;
// while(GetRawData(ch))
// {
if(a == '>')
{
while(GetRawData(ch))
{
if( ch == ' ')
return true;
else
return false;
}
GetDeviceData();
if(GetRawData(ch))
{
if(ch == ' ')
return true;
else
return false;
}
}
else
{
return false;
}
// }
GetDeviceData();
// return CheckIfGetGtandSpace();
return true;
}
HANDLE Rs232Module::GetWaitGtAndSpaceEvent()
{
return m_hWaitGtAndSpace;
}
char Rs232Module::GetCrByte()
{
return cr;
}
char Rs232Module::GetLfByte()
{
return lf;
}
void Rs232Module::CleanResult(ATResult& rt)
{
rt.resultLst.clear();
#ifdef SP_LEVEL1
TRACE(">>>>>Rs232Module::CleanResult()<<<<<\n");
#endif
}
/*
bool Rs232Module::GetPdu(char *buf, const int maxLen)
{
int wi = 0;
unsigned char ch;
unsigned char ch2;
while(1)
{
if(ch == cr)
{
if (ch2 == lf)
{
buf[wi] = '\0';
return true;
}
else
return false;
}
else
{
if(wi < maxLen)
buf[wi++] = ch;
}
}
}
*/
bool Rs232Module::GetPdu(char *buf, const int maxLen)
{
int wi = 0;
COMSTAT comstat;
DWORD errors;
int getPduState=0;
while(1)
{
//Get the PDU DATA from UART
if( ::WaitForSingleObject(stopEvent, 0)==WAIT_OBJECT_0 )
{
::ClearCommError(hCOM, &errors, &comstat);
break;
}
::ClearCommError(hCOM, &errors, &comstat);
if( !comstat.cbInQue )
{
Sleep(1);
continue;
}
int dwBytesRead = (DWORD) comstat.cbInQue;
if( 512 <= (int) dwBytesRead )
dwBytesRead = 512;
if( ::ReadFile(hCOM, m_pduData, dwBytesRead, &m_dwRead, NULL) )
{
if( m_dwRead==0 )
continue;
m_iPduRead = m_dwRead;
// Fill the data into m_pduDATA
for(int i=0; i<m_iPduRead && !stopEvent; i++)
{
switch(getPduState)
{
case 0:
if(m_pduData[i]==cr)
getPduState=1;
else
buf[wi++]=m_pduData[i];
break;
case 1:
if(m_pduData[i]==lf)
{
buf[wi]='\0';
return true;
}
else
{
return false;
}
break;
}
}
}
}
return true;
}
bool Rs232Module::GetList(char* ptr, int& wi, ATParamLst& lst)
{
ATParamElem elems;
bool intervalFlag=false;
bool stag = false;
wi = 0;
readIndex++; // Skip (
while(respDataBuf[readIndex]!=')')
{
switch(respDataBuf[readIndex])
{
case ',':
GetParam(ptr, elems, stag);
stag = false;
break;
case '"':
GetString(ptr, wi, elems);
stag = true;
wi = 0;
break;
case '-':
// Case of interval
intervalFlag = true;
ptr[wi]='\0';
if(IsNumber(ptr))
elems.int_range_begin = atoi(ptr);
else
return false;
wi = 0;
readIndex++;
while(respDataBuf[readIndex]!=')')
{
ptr[wi++] = respDataBuf[readIndex++];
}
ptr[wi]='\0';
if(IsNumber(ptr))
elems.int_range_end = atoi(ptr);
else
return false;
break;
case '\0':
return false;
break;
case '(':
GetList(ptr, wi, elems);
break;
default:
ptr[wi++] = respDataBuf[readIndex++];
break;
}
}
if(intervalFlag)
{
elems.type = AT_INTERVAL;
}
else
{
elems.type = AT_PARA_LIST;
}
lst.eleLst.push_back(elems);
return true;
}
bool Rs232Module::GetList(char *ptr, int& wi, ATParamElem& elem)
{
ATParamElem elemLocal;
elem.type = AT_PARA_LIST;
bool stag = false;
wi = 0;
readIndex++; // Skip (
while(respDataBuf[readIndex]!=')')
{
switch(respDataBuf[readIndex])
{
case ',':
GetParam(ptr, elem, stag);
ptr[0]='\0';;
wi = 0;
stag = false;
break;
case '"':
GetLstString(ptr, wi, elem);
stag = true;
break;
case '-':
// Case of interval
elem.type = AT_INTERVAL;
ptr[wi]='\0';
if(IsNumber(ptr))
elem.int_range_begin = atoi(ptr);
else
return false;
wi = 0;
readIndex++;
while(respDataBuf[readIndex]!=')')
{
ptr[wi++] = respDataBuf[readIndex++];
}
readIndex--;
ptr[wi]='\0';
if(IsNumber(ptr))
elem.int_range_end = atoi(ptr);
else
return false;
break;
case '\0':
return false;
break;
case '(':
GetList(ptr, wi, elemLocal);
break;
default:
ptr[wi++] = respDataBuf[readIndex];
break;
}
readIndex++;
}
ptr[wi]='\0';
if(sizeof(ptr)!=0)
GetParam(ptr, elem, stag);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -