📄 comm.cpp
字号:
// comm.cpp: implementation of the Ccomm class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FLADS.h"
#include "comm.h"
#include "FLADSDoc.h"
#include "FLADSView.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CFLADSApp theApp;
HANDLE PidThreadHandle;
DWORD PidThreadId;
BOOL PidRun = FALSE;
HANDLE WarnThreadHandle;
DWORD WarnThreadId;
BOOL WarnRun = FALSE;
Ccomm::Ccomm()
{
Com1_Repeate = 0;
Com1_TimeOut = 0;
Com1_TxdBuffer = new BYTE[3000];
Com1_RxdBuffer = new BYTE[3000];
memset(Com1_TxdBuffer,0,3000);
memset(Com1_RxdBuffer,0,3000);
Com1_WorkStatus = NOUSER; //Set No User Flag;
Com1_ErrorFlag = 0;
Com1_Error = 0;
Com2_Repeate = 0;
Com2_TimeOut = 0;
Com2_TxdBuffer = new BYTE[3000];
Com2_RxdBuffer = new BYTE[3000];
memset(Com2_TxdBuffer,0,3000);
memset(Com2_RxdBuffer,0,3000);
Com2_WorkStatus = NOUSER; //Set No User Flag;
Com2_ErrorFlag = 0;
Com2_Error = 0;
}
Ccomm::~Ccomm()
{
delete [] Com1_TxdBuffer;
delete [] Com1_RxdBuffer;
delete [] Com2_TxdBuffer;
delete [] Com2_RxdBuffer;
}
BOOL Ccomm::InitCom1()
{
int i;
DCB dcb;
char *comPort[]={"COM1","COM2","COM3","COM4"};
char *data[]={",5",",6",",7",",8"};
char *stop[]={",1",",1.5",",2"};
char *baud[]={":110",":150",":300",":600",":1200",":2400",":4800",":9600"};
char *parity[]={",N",",E",",O"};
if ((Com1_idComDev =
CreateFile(_T("COM1:"),
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
0,
NULL )) == (HANDLE) -1 )
{
MessageBox(NULL,_T("串口1打开失败!"),_T("警告"),MB_OK);
Com1_ErrorFlag = 1;
return ( 0 ) ;
}
else
{
if (!GetCommState( Com1_idComDev, &dcb ))
{
MessageBox(NULL,_T("读取串口1状态失败!"),_T("警告"),MB_OK);
Com1_ErrorFlag = 1;
return ( 0 );
}
// get any early notifications
// SetCommMask( Com1_idComDev, EV_TXEMPTY ) ;
// setup device buffers
i=SetupComm( Com1_idComDev, 4096, 4096 );
if(i==0) Com1_ErrorFlag=1;
// purge any information in the buffer
i=PurgeComm( Com1_idComDev, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
if(i==0) Com1_ErrorFlag=1;
// set up for overlapped I/O
Com1_CommTimeOuts.ReadIntervalTimeout = 10;//0xFFFFFFFF ;
Com1_CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
Com1_CommTimeOuts.ReadTotalTimeoutConstant = 1000 ;
Com1_CommTimeOuts.WriteTotalTimeoutMultiplier = 0 ;
Com1_CommTimeOuts.WriteTotalTimeoutConstant = 1000 ;
SetCommTimeouts( Com1_idComDev, &Com1_CommTimeOuts ) ;
}
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8 ;
dcb.Parity = NOPARITY ;
dcb.StopBits = ONESTOPBIT ;
return(SetCommState( Com1_idComDev, &dcb ));
}
BOOL Ccomm::InitCom2()
{
int i;
DCB dcb;
char *comPort[]={"COM1","COM2","COM3","COM4"};
char *data[]={",5",",6",",7",",8"};
char *stop[]={",1",",1.5",",2"};
char *baud[]={":110",":150",":300",":600",":1200",":2400",":4800",":9600"};
char *parity[]={",N",",E",",O"};
if ((Com2_idComDev =
CreateFile(_T("COM2:"),
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attrs
OPEN_EXISTING,
0,
NULL )) == (HANDLE) -1 )
{
MessageBox(NULL,_T("串口2打开失败!"),_T("警告"),MB_OK);
Com2_ErrorFlag = 1;
return ( 0 ) ;
}
else
{
if (!GetCommState( Com2_idComDev, &dcb ))
{
MessageBox(NULL,_T("读取串口2状态失败!"),_T("警告"),MB_OK);
Com2_ErrorFlag = 1;
return ( 0 );
}
// get any early notifications
// SetCommMask( Com2_idComDev, EV_TXEMPTY ) ;
// setup device buffers
i=SetupComm( Com2_idComDev, 4096, 4096 );
if(i==0) Com2_ErrorFlag=1;
// purge any information in the buffer
i=PurgeComm( Com2_idComDev, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
if(i==0) Com2_ErrorFlag=1;
// set up for overlapped I/O
Com2_CommTimeOuts.ReadIntervalTimeout = 10;//0xFFFFFFFF ;
Com2_CommTimeOuts.ReadTotalTimeoutMultiplier = 0 ;
Com2_CommTimeOuts.ReadTotalTimeoutConstant = 1000 ;
Com2_CommTimeOuts.WriteTotalTimeoutMultiplier = 0 ;
Com2_CommTimeOuts.WriteTotalTimeoutConstant = 1000 ;
SetCommTimeouts( Com2_idComDev, &Com2_CommTimeOuts ) ;
}
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
return(SetCommState( Com2_idComDev, &dcb ));
}
void Ccomm::CloseCom1()
{
// disable event notification and wait for thread
// to halt
SetCommMask(Com1_idComDev, 0 ) ;
// drop DTR
EscapeCommFunction(Com1_idComDev, CLRDTR );
// purge any outstanding reads/writes and close device handle
PurgeComm( Com1_idComDev, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle(Com1_idComDev) ;
} // end of CloseConnection()
void Ccomm::CloseCom2()
{
// disable event notification and wait for thread
// to halt
SetCommMask(Com2_idComDev, 0 ) ;
// drop DTR
EscapeCommFunction(Com2_idComDev, CLRDTR );
// purge any outstanding reads/writes and close device handle
PurgeComm( Com2_idComDev, PURGE_TXABORT | PURGE_RXABORT |
PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
CloseHandle(Com2_idComDev) ;
} // end of CloseConnection()
BOOL Ccomm::ReceiveSum()
{
BOOL ok;
ok=false;
unsigned char ch;
unsigned int ui,len;
ch=0;
len=Com1_RxdBuffer[1];
for(ui=1;ui<len+3;ui++)
{
ch=ch+Com1_RxdBuffer[ui];
}
if(ch==Com1_RxdBuffer[len+3]) ok=true;
return(ok);
}
BOOL Ccomm::ReceiveSum2()
{
BOOL ok;
ok=false;
unsigned char ch;
unsigned int ui,len;
union WDAT work;
unsigned int sum;
len=0;
len=Com2_RxdBuffer[1];
if(len==0) return (ok);
sum=0;
for(ui=0;ui<(len-2)/2;ui++)
{
work.Word=0;
work.B.Low =Com2_RxdBuffer[2*ui+1];
work.B.High=Com2_RxdBuffer[2*ui+2];
sum=sum+work.Word;
}
work.Word=0;
work.B.Low =Com2_RxdBuffer[len-1];
work.B.High=Com2_RxdBuffer[len];
if(sum==work.Word) ok=true;
return(ok);
}
//////////////////////////////////////////////////////////////////////////
BOOL Ccomm::SendFrame1()
{
BOOL SendLen;
OVERLAPPED OverLappedTX;
if(Com1_ErrorFlag)
{
MessageBox(NULL,_T("程序检测到,串口1发生了严重错误,通信工作无法继续进行!"),_T("系统提示"),MB_OK|MB_ICONSTOP);
return (0);
}
PurgeComm( Com1_idComDev, PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
SendLen=WriteFile(Com1_idComDev,Com1_TxdBuffer,Com1_dwLengthTX,&Com1_dwLengthTX,&OverLappedTX);
if(SendLen==0)
{
MessageBox(NULL,_T("WriteFile False!"),_T("MESSAGE..."),MB_OK);
}
return(SendLen);
}
//////////////////////////////////////////////////////////////////////////
BOOL Ccomm::SendFrame2()
{
BOOL SendLen;
OVERLAPPED OverLappedTX;
if(Com2_ErrorFlag)
{
MessageBox(NULL,_T("程序检测到,串口2发生了严重错误,通信工作无法继续进行!"),_T("系统提示"),MB_OK|MB_ICONSTOP);
return (0);
}
PurgeComm( Com2_idComDev, PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
SendLen=WriteFile(Com2_idComDev,Com2_TxdBuffer,Com2_dwLengthTX,&Com2_dwLengthTX,&OverLappedTX);
if(SendLen==0)
{
MessageBox(NULL,_T("WriteFile False!"),_T("MESSAGE..."),MB_OK);
}
return(SendLen);
}
//////////////////////////////////////////////////////////////////////////
// ReceiveFrame
BOOL Ccomm::ReceiveFrame1()
{
int nLength,i;
union WDAT work;
OVERLAPPED OverLappedRX;
double dx,dy,dt;
CString string;
switch(theApp.RSCASE)
{
case 0: Com1_dwLengthRX=0x1b+4; //召唤温度
break;
case 1: Com1_dwLengthRX=0x0E+4; //召唤温度参数
break;
case 2: Com1_dwLengthRX=0x1b+4; //召唤温度
break;
case 3: Com1_dwLengthRX=0x23+4; //召唤VFDS参数
break;
case 4: Com1_dwLengthRX=0x1b+4; //召唤温度
break;
case 5: Com1_dwLengthRX=0x01+4;// 3 PID 参数设置
break;
case 6: Com1_dwLengthRX=0x1b+4; //召唤温度
break;
case 7: Com1_dwLengthRX=0x16+4;// 4 传送数据
break;
case 8: Com1_dwLengthRX=0x04; // 8 复位
break;
case 9: Com1_dwLengthRX=0x04; // 9 4018 设置命令
break;
case 10:Com1_dwLengthRX=0x04; // 10 4012 设置命令
break;
case 11:Com1_dwLengthRX=0x04; // 11 4021 设置命令
break;
default:Com1_dwLengthRX=1;
break;
}
if(Com1_ErrorFlag)
{
MessageBox(NULL,_T("程序检测到,串口发生了严重错误,通信工作无法继续进行!"),_T("系统提示"),MB_OK|MB_ICONSTOP);
return (0);
}
nLength=ReadFile(Com1_idComDev,Com1_RxdBuffer,Com1_dwLengthRX,&Com1_dwLengthRX,&OverLappedRX);
if(nLength==0)
{
MessageBox(NULL,_T("ReadFile False!"),_T("MESSAGE..."),MB_OK);
}
if(Com1_RxdBuffer[0]==0xaa)
{
if(ReceiveSum())
{
switch(Com1_RxdBuffer[2])
{
case 0x00:theApp.yxbuf00=Com1_RxdBuffer[6];
theApp.yxbuf01=Com1_RxdBuffer[7];
theApp.Warn[0]=Com1_RxdBuffer[10];
theApp.Warn[1]=Com1_RxdBuffer[11];
theApp.Warn[2]=Com1_RxdBuffer[12];
theApp.Warn[3]=Com1_RxdBuffer[13];
for(i=0;i<8;i++)
{
work.Word=0;
work.B.Low =Com1_RxdBuffer[14+i*2];
work.B.High=Com1_RxdBuffer[15+i*2];
dx=(double)work.Word;
if(i==0)
{
dy=theApp.Split(theApp.setpt[0],dx,3);
dx=dy;
}
dx=dx/10;
theApp.temper_value[i]=dx;
}
theApp.yxtask.krbuf[0].Value=~theApp.yxbuf00;
theApp.yxtask.krbuf[1].Value=~theApp.yxbuf01;
theApp.yxtask.krbuf[8].Value =theApp.Warn[0];
// theApp.yxtask.krbuf[9].Value =theApp.Warn[1];
theApp.yxtask.krbuf[10].Value=theApp.Warn[2];
theApp.yxtask.krbuf[11].Value=theApp.Warn[3];
theApp.ComCount++;
break;
case 0x01://召唤温度参数
theApp.yxbuf00=Com1_RxdBuffer[6];
theApp.yxbuf01=Com1_RxdBuffer[7];
theApp.Warn[0]=Com1_RxdBuffer[10];
theApp.Warn[1]=Com1_RxdBuffer[11];
theApp.Warn[2]=Com1_RxdBuffer[12];
theApp.Warn[3]=Com1_RxdBuffer[13];
theApp.A4018_TT=Com1_RxdBuffer[14];
theApp.A4018_CC=Com1_RxdBuffer[15];
theApp.A4018_FF=Com1_RxdBuffer[16];
theApp.ComCount++;
theApp.yxtask.krbuf[0].Value=~theApp.yxbuf00;
theApp.yxtask.krbuf[1].Value=~theApp.yxbuf01;
theApp.yxtask.krbuf[8].Value =theApp.Warn[0];
// theApp.yxtask.krbuf[9].Value =theApp.Warn[1];
theApp.yxtask.krbuf[10].Value=theApp.Warn[2];
theApp.yxtask.krbuf[11].Value=theApp.Warn[3];
break;
case 0x02: //召唤VFDS参数
theApp.yxbuf00=Com1_RxdBuffer[6];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -