📄 2007_11_13_s12_com2_testdlg.cpp
字号:
// 2007_11_13_S12_COM2_TestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "2007_11_13_S12_COM2_Test.h"
#include "2007_11_13_S12_COM2_TestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
HANDLE hfile;
HANDLE hAPIfile;
CString sComm=_T("COM3:");
HANDLE hReadPortThread;
FILE *fstream;
char NumberToChar(unsigned char data); //数字转字符
void WriteSysTimeToTxtFile(FILE *fstream, SYSTEMTIME ti,UINT message);
/////////////////////////////////////////////////////////////////////////////
// CMy2007_11_13_S12_COM2_TestDlg dialog
CMy2007_11_13_S12_COM2_TestDlg::CMy2007_11_13_S12_COM2_TestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy2007_11_13_S12_COM2_TestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy2007_11_13_S12_COM2_TestDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy2007_11_13_S12_COM2_TestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy2007_11_13_S12_COM2_TestDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy2007_11_13_S12_COM2_TestDlg, CDialog)
//{{AFX_MSG_MAP(CMy2007_11_13_S12_COM2_TestDlg)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_WriteFile_Com2, OnWriteFileCom2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy2007_11_13_S12_COM2_TestDlg message handlers
BOOL CMy2007_11_13_S12_COM2_TestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
MoveWindow(0,0,320,240);
RETAILMSG(1,(TEXT("++++CMy2007_11_13_S12_COM2_TestDlg::OnInitDialog( )----\r\n")));
//-----------------------------------------------------------------------------------------
iBand = 9600; //串口波特率
//全局变量初始化
iReadAto = 31;
iReadBto = 0;
iReadCto = 4;
iWriteAto = 31;
iWriteCto = 4;
//--------------------------------------------------------------------------------------------
DWORD m_dwReadPortThreadID;
//打开串口
//CString sComm=_T("COM2:");
hfile = CreateFile (sComm, GENERIC_READ|GENERIC_WRITE,
0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
CString strError;
if (hfile == INVALID_HANDLE_VALUE )
{
//不能打开端口
strError.Format(_T("Can't open %s, Error No.=%d"),
sComm, GetLastError());
// MessageBox (NULL, strError,TEXT("Error!"), MB_OK);
MessageBox ( strError,TEXT("Error!"), MB_OK);
}
else
{
//能打开端口
strError.Format(_T("Open the %s port in success"),
sComm);
// MessageBox (NULL, strError,TEXT("Error!"), MB_OK);
MessageBox ( strError,TEXT("Error!"), MB_OK);
}
//-----------------------------------------------------------------
//指定端口监测的事件集
SetCommMask (hfile, EV_RXCHAR|EV_TXEMPTY);
//配置串行端口
if(!InitDCB())
return FALSE;
//设置端口超时值
if(!InitCommTimeouts())
return FALSE;
//分配设备缓冲区
SetupComm(hfile,512,512);
//初始化缓冲区中的信息
PurgeComm(hfile,PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_RXABORT|PURGE_TXABORT);
//设置端口上指定信号的状态
// SETDTR: 发送DTR (data-terminal-ready)信号
// SETRTS: 发送RTS (request-to-send)信号
EscapeCommFunction(hfile,SETDTR);
EscapeCommFunction(hfile,SETRTS);
//创建一个从串口读取数据的线程
hReadPortThread = CreateThread(NULL,0,ReadPortThread,this,0,&m_dwReadPortThreadID);
//=====================================================
//2007--12--19 XQH
// CFile file;
// file.Open(
hAPIfile=CreateFile(TEXT("\\NandFlash\\Comtest.txt"), //2007--8--23 XQH 这个INI文件里面存储有触摸屏的校正数据
GENERIC_READ|GENERIC_WRITE,
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
// OPEN_EXISTING, // comm devices must use OPEN_EXISTING
CREATE_ALWAYS,
0, // Async I/O
NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
BOOL CMy2007_11_13_S12_COM2_TestDlg::InitDCB()
{
DCB PortDCB;//声明一个DCB结构
DWORD dwError;
PortDCB.DCBlength = sizeof (DCB);//初始化 DCBlength
//得到端口的默认设置信息
GetCommState (hfile, &PortDCB);
//改变DCB结构设置
PortDCB.BaudRate = iBand; //波特率
//PortDCB.fBinary = TRUE; //Win32不支持非二进制串行传输模式,必须为TRUE
PortDCB.fParity = FALSE; //启用奇偶校验
PortDCB.fNull = FALSE;//设为TRUE将使串行驱动程序忽略收到的空字节
// PortDCB.fRtsControl = RTS_CONTROL_ENABLE; //启用RTS线
// PortDCB.fAbortOnError = FALSE; //WINCE串行驱动程序的默认执行将忽略这个字段
PortDCB.ByteSize = 8; //每字节的位数
PortDCB.Parity = NOPARITY;//无奇偶校验
PortDCB.StopBits = ONESTOPBIT; //每字节一位停止位
//根据DCB结构配置端口
if (!SetCommState (hfile, &PortDCB))
{
//不能配置串行端口
//MessageBox (NULL, TEXT("Unable to configure the serial port!"),
//TEXT("Error!"), MB_OK);
dwError = GetLastError ();//获得错误原因的代码
return FALSE;
}
return TRUE;
}
BOOL CMy2007_11_13_S12_COM2_TestDlg::InitCommTimeouts()
{
COMMTIMEOUTS CommTimeouts;//声明一个COMMTIMEOUTS结构
DWORD dwError;
//得到超时参数
GetCommTimeouts (hfile, &CommTimeouts);
//改变COMMTIMEOUTS结构设置
//设定间隔超时为MAXDWORD,ReadFile读一次缓冲区中的内容后,立即返回不管是否读入了要求的字符
/*
//全局变量初始化
iReadAto = 31;
iReadBto = 0;
iReadCto = 4;
iWriteAto = 31;
iWriteCto = 4;
*/
CommTimeouts.ReadIntervalTimeout = iReadBto; //0
CommTimeouts.ReadTotalTimeoutMultiplier = iReadAto; //31
CommTimeouts.ReadTotalTimeoutConstant = iReadCto; //4
//设定WriteFile的总超时(10*n+1000)ms
CommTimeouts.WriteTotalTimeoutMultiplier = iWriteAto; //31
CommTimeouts.WriteTotalTimeoutConstant=iWriteCto; //4
//设置端口超时值
if (!SetCommTimeouts(hfile,&CommTimeouts))
{ //不能设置超时值
dwError = GetLastError ();//获得错误原因的代码
return FALSE;
}
return TRUE;
}
DWORD CMy2007_11_13_S12_COM2_TestDlg::ReadPortThread(LPVOID lparam)
{
BOOL fReadState;//读入信息时返回值,若为TRUE就是正确读入
DWORD dwBytesTransferred,dwCommModemStatus;//收到的字节数,串口状态,以判断是否有事件发生
COMSTAT ComStat;//串口状态的详细情况表,
DWORD dwErrorFlags;//读串口状态的标志
BOOL readStatus=FALSE;//读入信息的状态,若为TRUE就是正在读入但还没有读完
BYTE Data;
DWORD returntxt;
memset(&Data,0,sizeof(Data));//clear Data
//程序在串口有效的状态下,无限循环
while(TRUE) //死循环,不退出
{
//在循环中响应系统消息
SetCommMask (hfile, EV_RXCHAR);//重新设置程序响应的事件,但这个只是保证程序的安全性
//等待串口的事件发生,当dwCommModemStatus值为1时表示接收到数据
WaitCommEvent (hfile, &dwCommModemStatus, 0);
//SetCommMask (CPublic::hPort, EV_RXCHAR);
//一般并不起作用
if (dwCommModemStatus & EV_RXCHAR) //检测收到的事件是否为"接收字符"的事件
{
ClearCommError(hfile,&dwErrorFlags,&ComStat);//清除串口状态标志,并返回当前状态
//cbInQue返回在串行驱动程序输入队列中的字符数
dwBytesTransferred = ComStat.cbInQue;
if(dwBytesTransferred>0)//防止无故产生事件
{
//从串口读取数据
//读入数据,并返回数据长度,采用同步方式
do
{
fReadState=ReadFile(hfile,&Data,1,&dwBytesTransferred,NULL);
//读取接收到的数据(该数据触发了相应的串口事件)
if(!fReadState)
{
//不能从串口读取数据
//return 0;
}
else
{
if (dwBytesTransferred == 1)//2007--11--13 xqh 接收到了数据,并对数据进行处理
{
//2007--11--13 XQH 向串口显示从串口中接收到的数据
// RETAILMSG(1,(TEXT("++++ReadPortThread( )----the recieved Data is 0x%x\r\n"),Data));
/*
//2007--12--17 XQH 把从串口捕捉到的信息,写到文件中!!!
SYSTEMTIME ti;
GetSystemTime(&ti); //获取当前时间
fstream=fopen("\\NFDisk\\logoCOM2.txt","a");
fseek(fstream, 0L, SEEK_END); //移动到文件的末尾
WriteSysTimeToTxtFile(fstream, ti,Data);
*/
//=======================================================
/*
if(Data==0x0d)
{
SetFilePointer (hAPIfile, 0, NULL, FILE_END) ;
WriteFile(hAPIfile,_T("\r"),2,&returntxt,NULL); //写入回车换行
}
else if(Data==0x0a)
{
SetFilePointer (hAPIfile, 0, NULL, FILE_END) ;
WriteFile(hAPIfile,_T("\n"),2,&returntxt,NULL);
}
else
{
SetFilePointer (hAPIfile, 0, NULL, FILE_END) ;
WriteFile(hAPIfile,&Data,1,&returntxt,NULL);
}
*/
//-------------------------------------------------------
SetFilePointer (hAPIfile, 0, NULL, FILE_END) ;
WriteFile(hAPIfile,&Data,1,&returntxt,NULL);
}//if
}//else
}while(dwBytesTransferred == 1);//do while
//=========================================================
//对数据进行处理
}//if(dwBytesTransferred>0)//防止无故产生事件
}//if (dwCommModemStatus & EV_RXCHAR) //检测收到的事件是否为"接收字符"的事件
}//while(TRUE)
return 0;
}
void CMy2007_11_13_S12_COM2_TestDlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
RETAILMSG(1,(TEXT("++++::OnClose( )----\r\n")));
CloseHandle(hfile);
CDialog::OnClose();
}
void CMy2007_11_13_S12_COM2_TestDlg::OnWriteFileCom2() //按键
{
// TODO: Add your control notification handler code here
RETAILMSG(1,(TEXT("++++::OnWriteFileCom2( )----\r\n")));
int i;
unsigned long ulRWLen;
unsigned char byte[10];
for(i=0;i<10;i++)
{
byte[i]=0x30+i;
}
//AfxMessageBox(_T("Entering OnWriteFileCom2()!"),NULL,MB_OK);
WriteFile(hfile,byte,10,&ulRWLen,NULL);
}
//=====================================================================================
char NumberToChar(unsigned char data) //数字转字符
{
char retval;
if((data>=0)&&(data<=9))
{
retval=data-0+0x30;
}
else if((data>=0x0a)&&(data<=0x0f))
{
retval=data-0x09+0x40;
}
return retval;
}
void WriteSysTimeToTxtFile(FILE *fstream, SYSTEMTIME ti,UINT message)
{
char signal=':';
WORD time[1];
unsigned char data[4];
char c;
//-----------------------------------------------------------------
//小时
time[0]=ti.wHour;
data[0]=time[0]&0xff;
data[1]=data[0]/10;
data[2]=data[0]%10;
c=NumberToChar(data[1]);
fwrite(&c, 1, 1, fstream);
c=NumberToChar(data[2]);
fwrite(&c, 1, 1, fstream);
fwrite(&signal, 1, 1, fstream);
//---------------------------------------------------------------------
//分
time[0]=ti.wMinute ;
data[0]=time[0]&0xff;
data[1]=data[0]/10;
data[2]=data[0]%10;
c=NumberToChar(data[1]);
fwrite(&c, 1, 1, fstream);
c=NumberToChar(data[2]);
fwrite(&c, 1, 1, fstream);
fwrite(&signal, 1, 1, fstream);
//--------------------------------------------------------------------
//秒
time[0]=ti.wSecond ;
data[0]=time[0]&0xff;
data[1]=data[0]/10;
data[2]=data[0]%10;
c=NumberToChar(data[1]);
fwrite(&c, 1, 1, fstream);
c=NumberToChar(data[2]);
fwrite(&c, 1, 1, fstream);
fwrite(&signal, 1, 1, fstream);
//----------------------------------------------------------------------
//写入“标识”
c='m';
fwrite(&c, 1, 1, fstream);
c='p';
fwrite(&c, 1, 1, fstream);
fwrite(&signal, 1, 1, fstream);
//-------------------------------------------------------------------------
//写入收到的“消息”
c='0';
fwrite(&c, 1, 1, fstream);
c='x';
fwrite(&c, 1, 1, fstream);
data[0]=message/16; //数据写入
data[1]=(message-data[0]*16);
c=NumberToChar(data[0]);
fwrite(&c, 1, 1, fstream);
c=NumberToChar(data[1]);
fwrite(&c, 1, 1, fstream);
//-------------------------------------------------------------------------
//写入“回车换行”
c='\r';
fwrite(&c, 1, 1, fstream);
c='\n';
fwrite(&c, 1, 1, fstream);
//-----------------------------------------------------------------------
//CloseHandle(ghlogotxt);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -