//********************************************************************
//  模块: MD_CA530.cpp
//  描述: 本模块为东亚公司的CA530血凝仪的联机程序(Win95版)
//      函数:
//              DLLEXPORT DWORD WINAPI CommMonitorProc( LPSTR lpData );
//              DLLEXPORT void  BeforeSetCommState(DCB *pComDcb);
//  编写:
//  最后修改日期:   09/02/1999
//********************************************************************
#include <stdio.h>
#include <Const.h>
#include <Device.h>
#include <str.h> 
#define c_RxBufferLength        4096     //接收缓冲区长
#define c_MaxItemNum            50              //一个包的最大项目数
#define c_SampleIDOff           26
#define c_FirstItemOff          53
#define c_ItemLen               3
#define c_ResultLen             5
#define c_EachDistance          9int WINAPI FormatResult(LPSTR lpDataBuffer, PRESULT lpResult)
{
static WORD  wSampleID;
static SYSTEMTIME  AssayTime;
static int i;
static LPSTR lpBeginPos, lpEndPos;
LPSTR  lpTemp;
int    j, k; GetLocalTime(&AssayTime); //得到系统北京时间
if ((lpBeginPos =strchr(lpDataBuffer, 0x02))==NULL)
return 0; lpEndPos =lpBeginPos;
lpEndPos +=c_SampleIDOff;
for(lpEndPos;*(lpEndPos) ==0x30;lpEndPos++);
wSampleID =atoi(lpEndPos);
lpBeginPos +=c_FirstItemOff;
for(i =0;i<c_MaxItemNum;i++)
{
lpResult[i].SampleID =wSampleID;
strcpy(lpResult[i].SampleType, "");
strncpy(lpResult[i].ItemID, lpBeginPos, c_ItemLen);
lpResult[i].ItemID[c_ItemLen] ='\0';
strncpy(lpResult[i].AssayResult, lpBeginPos+c_ItemLen, c_ResultLen);
lpResult[i].AssayResult[c_ResultLen] ='\0';
strcpy(lpResult[i].AssayResult2, "");
        memcpy(&(lpResult[i].AssayTime), &AssayTime, sizeof(SYSTEMTIME));
        DeleteSpace(lpResult[i].AssayResult);
j =strlen(lpResult[i].AssayResult);
lpTemp =lpResult[i].AssayResult;
for(k =0;k<j;k++,lpTemp++)
{
if(*(lpTemp) <=0x2D || *(lpTemp) ==0x2F || *(lpTemp) >=0x3A)
{
strcpy(lpResult[i].AssayResult2,lpResult[i].AssayResult);
strcpy(lpResult[i].AssayResult, "");
break;
}
}
lpBeginPos +=c_EachDistance;
if (*(lpBeginPos) ==ETX)
break;
}
return (i+1);
}
//************************************************************************
//DLLEXPORT DWORD WINAPI CommMonitorProc( LPSTR lpData )
//
//  描述:
//     串口监控线程的启动函数
//  参数:
//     LPSTR lpData
//        生化仪接口控制块
//************************************************************************
DWORD WINAPI CommMonitor( LPSTR lpData)
{
//定义变量
DEVICE  * lpDevice =(LPDEVICE)lpData;
DWORD dwReadLength=0, dwWrittenLength=0, dwRxBufferLength=0;
DWORD    dwError =0, dwEvtMask;
COMSTAT  ComStat;
BYTE     RxChar=0, RxBuffer[c_RxBufferLength] ;//接收缓冲区
RESULT Result[c_MaxItemNum]; //存储结果
int i, nItemCount; //赋初始值
for (i =0; i<c_MaxItemNum; i++)
{
strcpy(Result[i].InstCode, lpDevice->InstCode); //仪器缩写
Result[i].ResultType =c_Route;//c_TypeRoutine | c_TypeNumber;
Result[i].ErrorCode  =c_Reliable;
memset(LPSTR(Result[i].Comment), 0, c_MaxCommentLength);
}

PurgeComm( lpDevice->hComm, PURGE_RXCLEAR);
SetCommMask(lpDevice->hComm, EV_RXFLAG); while (TRUE)
{
//----第一步:接收数据----
ClearCommError(lpDevice->hComm, &dwError, &ComStat);
while( ComStat.cbInQue ==0) //接收缓冲区无数据
{
do{//等待LF
WaitCommEvent(lpDevice->hComm, &dwEvtMask, NULL);
}while((dwEvtMask & EV_RXFLAG) != EV_RXFLAG);
ClearCommError(lpDevice->hComm, &dwError, &ComStat);
}
do{
ReadFile(lpDevice->hComm, &RxChar, 1, &dwReadLength, NULL);
}while(RxChar !=STX);
memset(RxBuffer, 0, c_RxBufferLength);
RxBuffer[0] =RxChar;
i =1;
do{
ReadFile(lpDevice->hComm, &RxChar, 1, &dwReadLength, NULL);
RxBuffer[i++] =RxChar;
}while(RxChar !=ETX); //----第二步:将原始数据写入文本文件----
if (lpDevice->hRawDataFile !=INVALID_HANDLE_VALUE)
{
SetFilePointer(lpDevice->hRawDataFile, 0, 0, FILE_END);
WriteFile(lpDevice->hRawDataFile, (LPSTR)RxBuffer, i, &dwWrittenLength, NULL);
WriteFile(lpDevice->hRawDataFile,"\x0d\x0a" , 2, &dwWrittenLength, NULL);
} //----第三步:处理数据----
nItemCount =FormatResult((LPSTR)RxBuffer, Result);
if (nItemCount>0)
{
(*(lpDevice->lpResultProcessRoutine))(Result, nItemCount);
lpDevice->dwRecordCount++;
lpDevice->wSampleID=Result[nItemCount-1].SampleID;
} //----第四步:----
}
return TRUE;} // end of CommWatchProc()void WINAPI BeforeSetCommState(DCB *pComDcb)
{
pComDcb->EvtChar =ETX;
}int WINAPI ReadBakData(PDEVICE lpDevice)
{
DWORD dwRxBufferLength=0, dwReadLength=0;
BYTE     RxBuffer[c_RxBufferLength] ;//接收缓冲区
RESULT Result[c_MaxItemNum]; //存储结果
int i, nItemCount, nItemTotalCount=0, nMarkCount=0; //赋初始值
for (i =0; i<c_MaxItemNum; i++)
{
strcpy(Result[i].InstCode, lpDevice->InstCode); //仪器缩写
Result[i].ResultType = c_Route ;//c_TypeRoutine | c_TypeNumber;
Result[i].ErrorCode  =c_Reliable;
memset(LPSTR(Result[i].Comment), 0, c_MaxCommentLength);
} SetFilePointer(lpDevice->hRawDataFile, 0, 0, FILE_BEGIN);
while(TRUE)
{
memset(RxBuffer, 0, c_RxBufferLength);
do{
ReadFile(lpDevice->hRawDataFile, RxBuffer, 1, &dwReadLength, NULL);
if (dwReadLength ==0)
return nItemTotalCount;
}while(RxBuffer[0]!=STX);

i=1;
do{
ReadFile(lpDevice->hRawDataFile, &(RxBuffer[i]), 1, &dwReadLength, NULL);
if (dwReadLength ==0)
return nItemTotalCount;
}while(RxBuffer[i++]!=ETX); nItemCount =FormatResult((LPSTR)RxBuffer, Result);
if (nItemCount>0)
{
(*(lpDevice->lpResultProcessRoutine))(Result, nItemCount);
lpDevice->dwRecordCount++;
lpDevice->wSampleID=Result[nItemCount-1].SampleID;
}
}
return nItemTotalCount;
}