📄 hs.cpp
字号:
// hs.cpp
// (c) Mobilink Telecom 1999, 2000
//
// Nick Steffen
// May 11, 2000
//
// Adapted from original calibration code
//
// Rev. 0 May 11, 2000
//
// June 27, 2000
// Added pac_cal_type data member & manipulation functions
//
#include "caltypes.h"
#include <stdio.h>
#include <io.h>
#include <math.h>
#include <time.h>
#include <afxwin.h>
#include <afxdlgs.h>
#include <assert.h>
#include "HS.h"
#define STRING_END 0x00
#define LINE_FEED 0x0a
#define CARRIAGE_RETURN 0x0d
#define SPACE_KEY 0x20
#define BACKSPACE_KEY 0x08
#define TAB_KEY 0x09
#define WAITING_TIME 3 //seconds
#define MAX_CHECK_LINE 50
//*******************************************************************************
hs::hs()
{
strcpy(HSMsg[0].command,"RXLEV");
HSMsg[0].num_parm=1;
strcpy(HSMsg[1].command,"RAMP");
HSMsg[0].num_parm=19;
strcpy(HSMsg[2].command,"GSYS");
HSMsg[0].num_parm=1;
strcpy(log_filename,"cal_log.txt");
logging=0;
}
//*******************************************************************************
HANDLE hs::HS_GetDeviceHandle()
{
return CommDesc.Sio;
}
//*******************************************************************************
void hs::HS_CloseComPort()
{
CloseHandle(CommDesc.Sio);
}
//*******************************************************************************
void hs::HS_SetPACCalType(PACCalType_t type)
{
pac_cal_type=type;
}
//*******************************************************************************
void hs::HS_GetPACCalType(PACCalType_t *type)
{
*type=pac_cal_type;
}
//*******************************************************************************
void hs::HS_SetLogFilename(char *filename)
{
strcpy(log_filename,filename);
}
//*******************************************************************************
void hs::HS_LogSIO(char *buf)
{
HANDLE hFile;
unsigned long fp;
unsigned long count;
char cr[2]={0x0d,0x0a};
if(logging)
{
hFile=CreateFile(log_filename,GENERIC_WRITE,FILE_SHARE_READ,NULL,
OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
// go to end of file
fp=SetFilePointer (hFile,0,NULL, FILE_END);
WriteFile(hFile,buf,strlen(buf),&count,NULL);
WriteFile(hFile,cr,2,&count,NULL); // append a newline
CloseHandle(hFile);
}
}
//*******************************************************************************
void hs::HS_EnableLogging(BOOL enable)
{
logging=enable;
}
//*******************************************************************************
void hs::HS_SetComPort(ComPort_t comport)
{
ComPort=comport;
}
//*******************************************************************************
BOOL hs::HS_InitComPort()
{
BOOL fStatus;
switch(ComPort)
{
case COM1:
strcpy(CommDesc.ComId, "COM1");
break;
case COM2:
strcpy(CommDesc.ComId, "COM2");
break;
case COM3:
strcpy(CommDesc.ComId, "COM3");
break;
case COM4:
strcpy(CommDesc.ComId, "COM4");
break;
case COM5:
strcpy(CommDesc.ComId, "COM5");
break;
case COM6:
strcpy(CommDesc.ComId, "COM6");
break;
case COM7:
strcpy(CommDesc.ComId, "COM7");
break;
case COM8:
strcpy(CommDesc.ComId, "COM8");
break;
default: break;
}
//--- create communication
CommDesc.Sio = CreateFile(
CommDesc.ComId,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL |
FILE_FLAG_OVERLAPPED,
NULL
);
//--- check if we can open the port
if( CommDesc.Sio == (HANDLE) (-1) )
{
return FALSE;
}
//----- Initialize Serial Port
// create I/O event used for overlapped reads / writes
CommDesc.olRead.hEvent = CreateEvent(
NULL, // no security
TRUE, // explicit reset req
FALSE, // initial event reset
NULL ) ; // no name
CommDesc.olWrite.hEvent = CreateEvent(
NULL, // no security
TRUE, // explicit reset req
FALSE, // initial event reset
NULL ) ; // no name
CommDesc.Dcb.DCBlength = sizeof( DCB ) ;
//--- set port
fStatus=GetCommState( CommDesc.Sio, &(CommDesc.Dcb) ) ;
CommDesc.Dcb.BaudRate = CBR_115200;
CommDesc.Dcb.ByteSize = 8 ;
CommDesc.Dcb.Parity = NOPARITY ;
CommDesc.Dcb.StopBits = ONESTOPBIT ;
CommDesc.Dcb.fInX = 0 ;
CommDesc.Dcb.fOutX = 0 ;
// other various settings
CommDesc.Dcb.fBinary = TRUE ;
CommDesc.Dcb.fParity = TRUE ;
fStatus=SetCommState( CommDesc.Sio, &(CommDesc.Dcb) ) ;
// get any early notifications
fStatus=SetCommMask( CommDesc.Sio, EV_RXCHAR ) ;
// setup device buffers
fStatus=SetupComm( CommDesc.Sio, 4096, 4096 ) ;
// purge any information in the buffer
fStatus=PurgeComm( CommDesc.Sio, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
SioPurge(); // empty buffers
SioSendCR(); // prompt the phone
Sleep(1000); // wait for phone's response
/*
// read all available characters
{
unsigned char c;
while(ReceiveChar(&c)==TRUE)
{
}
}
*/
SioPurge(); // and purge it
return TRUE;
}
//*******************************************************************************
BOOL hs::HS_GetHSRxlev(UInt16 *Rxlev)
{
Int16 i;
UInt8 NumParm;
Parm_t Parm;
Cmd_t Cmd;
SioPurge(); // empty buffers
//Send request
SioSendString("rxlev?\n"); //0 = GSM
Sleep(250);
for(i=0; i< 10; i++)
{
if(!SioReadOneLine(&Cmd, &NumParm, &Parm))
{
return FALSE;
}
if(strcmp((char *)Cmd, HSMsg[0].command) == 0)
{
*Rxlev = Parm[0];
return TRUE;
}
}
return FALSE;
}
//*******************************************************************************
BOOL hs::HS_GetHSCurRamp(BandIndex_t BandIndex, Parm_t *p)
{
Int8 i;
UInt8 NumParm;
Parm_t Parm;
Cmd_t Cmd;
Int8 buf[50];
Int8 BandIndex_850;
// empty the SIO buffer before query
SioPurge();
Sleep(10);
if(BandIndex==3)
BandIndex_850=0;
else
BandIndex_850=BandIndex;
sprintf((char *)buf,"ramplist? %d\n",BandIndex_850);
SioSendString(buf); //0 = GSM
//wait
Sleep(100);
for(i=0; i< MAX_CHECK_LINE; i++)
{
if(!SioReadOneLine(&Cmd, &NumParm, &Parm))
{
return FALSE;
}
if(strcmp(Cmd, HSMsg[1].command) == 0)
{
memcpy(p, &Parm[0], sizeof(Parm_t));
return TRUE;
}
}
return FALSE;
}
//*******************************************************************************
BOOL hs::HS_GetHSCurGsys(BandIndex_t BandIndex, UInt16 GsysIndex, Int16 *GsysVal)
{
Int8 i;
UInt8 NumParm;
Parm_t Parm;
Cmd_t Cmd;
Int8 buf[50];
Int8 BandIndex_850;
SioPurge(); // empty buffers
if(BandIndex==GSM850)
BandIndex_850=0;
else
BandIndex_850=BandIndex;
sprintf((char *)buf,"gsys? %d %d\n",BandIndex_850, GsysIndex);
SioSendString(buf); //0 = GSM
Sleep(500);
for(i=0; i< 3; i++)
{
if(!SioReadOneLine(&Cmd, &NumParm, &Parm))
{
return FALSE;
}
if(strcmp(Cmd, HSMsg[2].command) == 0)
{
*GsysVal = Parm[0];
return TRUE;
}
}
return FALSE;
}
//*******************************************************************************
//--- receive one char from UART when data ready
BOOL hs::ReceiveChar(UInt8 *ReceivedCh)
{
DWORD ErrorFlags;
COMSTAT ComStat;
Int8 RdBuf;
DWORD RecLength;
BOOL flag= FALSE;
clock_t start, current;
start = clock();
ClearCommError(CommDesc.Sio, &ErrorFlags, &ComStat);
while(!ReadFile( CommDesc.Sio, &RdBuf, 1, &RecLength, &(CommDesc.olRead) ) )
{
if( GetLastError() == ERROR_IO_PENDING)
{
//pending but still return FALSE, if no charactar is received yet.
while ( !GetOverlappedResult( CommDesc.Sio, &(CommDesc.olRead), &RecLength, FALSE) )
{
current = clock();
if (current > start + WAITING_TIME * CLOCKS_PER_SEC )
{
return FALSE;
}
if( GetLastError() == ERROR_IO_INCOMPLETE ) continue;
else
{
ClearCommError( CommDesc.Sio, &ErrorFlags, &ComStat);
break;
}
}
flag = TRUE;
}
else
{
ClearCommError( CommDesc.Sio, &ErrorFlags, &ComStat);
}
current = clock();
if (current > start + WAITING_TIME * CLOCKS_PER_SEC )
{
return FALSE;
}
}
*ReceivedCh = RdBuf;
return TRUE;
}
//*******************************************************************************
void hs::HS_SetBand(BandIndex_t band)
{
Int8 buf[30];
if(band==PCS || band==GSM850)
{
sprintf(buf,"ISPCS 1\n");
}
else
{
sprintf(buf,"ISPCS 0\n");
}
SioSendString(buf);
}
//*******************************************************************************
void hs::HS_SetArfcn(UInt16 arfcn)
{
Int8 buf[30];
sprintf(buf,"arfcn %d\n",arfcn);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -