出厂前必须对m161的rtc时间进行设置。实际产品将网口去掉,但可以通过串口来设置。因为console同时在使用串口,因此在ask方面存在问题。
主要代码如下:
HANDLE hCom;
DCB ComCfg;
COMMTIMEOUTS ComTimeOut;
char buff[16];
char buff_w[17];
int i,WriteSize,ReadSize;
DWORD CommMask=0,dwMask=0,dwTrans=0;
OVERLAPPED OLRead, OLWrite,OLos;
PACKET_CMD_SET_SERVER_TIME packet_cmd;
MS_TIME ms_os_time;
//打开com1
hCom = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
get_error();
}
if(GetCommState(hCom,&ComCfg)==0)
{
get_error();
}
//配置串口1的属性
ComCfg.BaudRate=CBR_57600;
ComCfg.fParity=FALSE;
ComCfg.fOutxCtsFlow=FALSE;
ComCfg.fOutxDsrFlow=FALSE;
ComCfg.fDtrControl=DTR_CONTROL_DISABLE;
ComCfg.fDsrSensitivity=FALSE;
ComCfg.fTXContinueOnXoff=TRUE;
ComCfg.fOutX=FALSE;
ComCfg.fInX=FALSE;
ComCfg.fErrorChar=FALSE;
ComCfg.fNull=FALSE;
ComCfg.fRtsControl=RTS_CONTROL_DISABLE;
ComCfg.XonLim=500;
ComCfg.XoffLim=500;
ComCfg.ByteSize=8;
ComCfg.Parity=NOPARITY;
ComCfg.StopBits=ONESTOPBIT;
ComCfg.XonChar=17;
ComCfg.XoffChar=19;
ComCfg.ErrorChar=0;
//ComCfg.StopBits=TWOSTOPBITS;
//ComCfg.Parity=EVENPARITY;
if(SetCommState(hCom,&ComCfg)==0)
{
get_error();
}
ComTimeOut.ReadIntervalTimeout=10;
ComTimeOut.ReadTotalTimeoutConstant=0;
ComTimeOut.ReadTotalTimeoutMultiplier=0;
ComTimeOut.WriteTotalTimeoutConstant=0;
ComTimeOut.WriteTotalTimeoutMultiplier=0;
if(SetCommTimeouts(hCom,&ComTimeOut)==0)
{
get_error();
}
GetCommMask(hCom,&CommMask);
CommMask=EV_RXFLAG|EV_RXCHAR|EV_TXEMPTY|EV_CTS|EV_EVENT2;
/*if(SetCommMask(hCom,CommMask)==0)
{
get_error();
}
SetupComm(hCom,MAXBLOCK,MAXBLOCK);
memset(&OLRead, 0, sizeof(OVERLAPPED));
memset(&OLWrite, 0, sizeof(OVERLAPPED));
memset(&OLos, 0, sizeof(OVERLAPPED));
*/
/*if((OLRead.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
return FALSE;
if((OLWrite.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
return FALSE;
if((OLos.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
return FALSE;*/
get_os_time(&ms_os_time);
/*buf_w[0]-buff[7]为协议头*,省略赋值/
buff_w[8]=ms_os_time.year;
buff_w[9]=ms_os_time.month;
buff_w[10]=ms_os_time.day;
buff_w[11]=ms_os_time.hour;
buff_w[12]=ms_os_time.minute;
buff_w[13]=ms_os_time.second;
buff_w[14]=buff_w[15]=0;
while(1)
{
//memcpy(buff_w,&packet_cmd,16);
//memcpy(buff,buff_w,16);
if(WriteFile(hCom,buff_w,sizeof(PACKET_CMD_SET_SERVER_TIME),(unsigned long*)&WriteSize,0)==0)
{
get_error();
//continue;
}
DBGPRINT(("write_size:%d\n",WriteSize));
break;
if(ReadFile(hCom,(unsigned long *)buff,16,(unsigned long*)&ReadSize,0)==0)
{
get_error();
}
//printf("writesize:%d,readsize:%d\n",WriteSize,ReadSize);
printf("\n");
Sleep(100);
//break;
}
CloseHandle(hCom);
存在问题:
如果采用memcpy对buff_w赋值,则m161收不到完整的数据;使用packed_cmd结构也收不到完整是数据,原因尚不清楚,郁闷中!只好算偏移地址对buff_w赋值,麻烦ing!
m161执行代码如下:
PPACKET_CMD_SET_SERVER_TIME pt_rtc;
struct timeval tv;
fd_set readfd;
int n=0;
int nsize;
MS_U8 buf[256];
struct rtc_time rt;
memset(buf, 0, 256);
tv.tv_sec=0;
tv.tv_usec=0;
FD_ZERO(&readfd);
FD_SET(fd,&readfd);
n=select(fd+1, &readfd, NULL, NULL, &tv);
while((nsize=read(fd,buf,256))>0){
//处理数据
}
注:在没有插U盘是该段代码会被周期性的调度,检测从串口收到的数据,根据收到数据的包头进行相应的操作。
阅读(2420) | 评论(0) | 转发(0) |