📄 icemodem.cpp
字号:
/* ICEModem.cpp
*
* 对串口进行操作的类。
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "stdafx.h"
#include "CSerial.h"
#include "common.h"
ICEModem::ICEModem()
{
iSequence = 0;
iModemReturn = MODEM_UNKNOWSTATIC; // MODEM 返回。
iSendType = 0;
AutoResp = 0;
}
ICEModem::~ICEModem()
{
}
// 选择串口。
BOOL ICEModem::Init(int ComPort,int Speed,char *InitCmd)
{
char sRet[200] = {0};
int iResponse = 0,iRet = 0;
do
{
switch(Speed)
{
case 110:
Speed = CBR_110;
break;
case 300:
Speed = CBR_300;
break;
case 600:
Speed = CBR_600;
break;
case 1200:
Speed = CBR_1200;
break;
case 2400:
Speed = CBR_2400;
break;
case 4800:
Speed = CBR_4800;
break;
case 9600:
Speed = CBR_9600;
break;
case 14400:
Speed = CBR_14400;
break;
case 19200:
Speed = CBR_19200;
break;
case 38400:
Speed = CBR_38400;
break;
case 56000:
Speed = CBR_56000;
break;
case 57600:
Speed = CBR_57600;
break;
case 115200:
Speed = CBR_115200;
break;
case 128000:
Speed = CBR_128000;
break;
case 256000:
Speed = CBR_256000;
break;
default:
Speed = CBR_9600;
break;
}
if(ComPort.Open(ComPort,Speed))
{
break;
}
else{
return FALSE;
}
}while(1);
// 初始化MODEM。
// ComPort = 3;
iResponse = ComPort.SendCommand(InitCmd, strlen(InitCmd));
if(iResponse < (int)strlen(InitCmd))
{
return FALSE;
}
Sleep(10);
int Times=0;
sRet[0] = '\0';
//等待Modem响应初始化命令
while( 1 )
{
Times++;
if( Times > 300 ) // 超时: 3s
break;
if( ComPort.ReadData( sRet, 200 ) > 3 )
if( strstr( sRet, "OK" ) )
break;
Sleep(10);
}
if( Times > 300 )
{
iStatus = 10; // 当前和串口连接建立。
return FALSE;
}
else
{
iStatus = 20; // 当前和MODEM连接建立。
}
return TRUE;
}
unsigned int ICEModem::ReadData(char *pBuf)
{
return 0;
}
unsigned int ICEModem::WriteData(char *pBuf)
{
return 0;
}
// 分析接收到的数据。
// 返回值:成功为真(不重发此帧数据);失败为假(需重发此帧数据).
// piType返回此帧的数据类型
BOOL ICEModem::AnalyseData(char *pBuf, int iSize,int *piType)
{
//static unsigned int uiSequence = 0;
//SendData_T SendData = pBuf;
WORD wCrc = 0;
int i = 0;
BOOL bRet = TURE;
if(pBuf == NULL || strlen(pBuf) != iSize || iSize < 3) //参数错误
{
return ;
}
piType = pBuf[0];
switch(piType)
{
case SOH: //数据传输开始标志
uiSequence = 0;
if(uiSequence == pBuf[1]) //序列号正确
{
wCrc = CalCRC(SendData.cData);
if(wCrc == SendData.wCRC)
{
bRet = TURE;
uiSequence++;
}
else{ //请求重发此帧
bRet = FALSE;
}
}
else{ //请求重发引帧
bRet = FALSE;
}
break;
case EOT: //数据传输结束标志
if(uiSequence == pBuf[1]) //序列号正确
{
wCrc = CalCRC(SendData.cData);
if(wCrc == SendData.wCRC)
{
bRet = TURE;
uiSequence = 0;
}
else{ //请求重发此帧
bRet = FALSE;
}
}
else{ //请求重发引帧
bRet = FALSE;
{
break;
case TYPE_ACK: //确认
ResData = pBuf;
wCrc = CalCRC(SendData.cData);
if(wCrc == SendData.wCRC)
{
bRet = TRUE;
uiSequence++;
}
else{ //请求重发此帧
bRet = FALSE;
}
break;
case TYPE_DATA: //数据类型
SendData = pBuf;
if(uiSequence == pBuf[1]) //序列号正确
{
wCrc = CalCRC(SendData.cData);
if(wCrc == SendData.wCRC)
{
bRet = TRUE;
uiSequence++;
}
else{ //请求重发此帧
bRet = FALSE;
}
}
else{ //请求重发此帧
bRet = FALSE;
}
break;
default: //其它标志:错误
bRet = FALSE;
break;
}
return bRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -