//處理數據
int CVICALLBACK thread_process()
{
char frame[500]={0};
int frameLen=0;
int readLen=0;
char temp;
//協議解析
while (G_threadRuning)
{
memset(frame,0,sizeof frame);
//讀隊列長度
readLen=CmtReadTSQData (G_queue,frame,sizeof frame, 100, OPT_TSQ_READ_PEEK);
//如果沒有4個數據就繼續下次循環
if (readLen < 4)
continue;
//有四個數據就判斷幀頭
if (0x55!=(frame[0]&0xff) || 0x55!=(frame[1]&0xff))
{
//如果幀頭不對,就把隊列第一個值去掉,然後繼續下次循環.
CmtReadTSQData (G_queue,&temp,1, 100, 0);
continue;
}
//有幀頭,就獲取幀長度
frameLen=(frame[3]&0xff);
//判斷隊列長度是否夠一幀,不夠就循環繼續讀取.
if(readLen < frameLen)
continue;
//幀長度夠就判斷幀尾.
if (0xaa!=(frame[frameLen-2]&0xff) || 0xaa!=(frame[frameLen-1]&0xff))
{
//幀尾不對,也把隊列第一格去掉繼續下次循環.
CmtReadTSQData (G_queue,&temp,1, 100, 0);
continue;
}
char sum1= frame[frameLen-3]&0xff;
char sum2= checkSum(frame,frameLen);
//判斷校驗和
if (sum1 != sum2)
{
//數據錯誤,整幀丟棄
CmtReadTSQData (G_queue,&frame,frameLen, 100, 0);
continue;
}
//都正確說明找到一個正確幀,按長度取出幀,並且把隊列裏面的數據去掉.
memset(frame,0,sizeof frame);
CmtReadTSQData (G_queue,&frame,frameLen, 100, 0);
//處理幀
frameProcess(frame,frameLen);
}
return 0;
}
//轉16進制字符串
void frameProcess(char * frame ,int len)
{
int one;
char str[20]={0};
char oneframe[2000]={0};
for (int i=0; i<len; i++)
{
one=frame[i];
sprintf(str,"0x%.2x ",one&0xff);
strcat(oneframe,str);
}
//寫日誌
base_log_write(oneframe);
//SetCtrlVal(g_panelMain,PANEL_MAIN_TEXTBOX,"\r\n");
//SetCtrlVal(g_panelMain,PANEL_MAIN_NUMERIC_recTime,second_end-second_start);
}