📄 messagetest.cpp
字号:
// MessageTest.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "MessageTest.h"
#include "MessageTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMessageTestApp
BEGIN_MESSAGE_MAP(CMessageTestApp, CWinApp)
//{{AFX_MSG_MAP(CMessageTestApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_MESSAGE(WM_COMMNOTIFY,OnCommNotify)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMessageTestApp construction
CMessageTestApp::CMessageTestApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CMessageTestApp object
CMessageTestApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CMessageTestApp initialization
BOOL CMessageTestApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CMessageTestDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
//入口参数为字符串的源地址、转换后的字节数组的目标地址、字符串的长度
void String_Byte(char *sour,unsigned char *dest,int len)
{
int index;
for(index=0;index<len;index+=2,sour++,dest++)
{
if(*sour>='0' && *sour<='9')
*dest=(*sour-'0')<<4;
else if(*sour>='A' && *sour<='F')
*dest=(*sour-'A'+10)<<4;
sour++;
if(*sour>='0' && *sour<='9')
*dest|=*sour-'0';
else if(*sour>='A' && *sour<='F')
*dest|=*sour-'A'+10;
}
}
//入口参数为字节数组的源地址、字符串的目标地址、字节数组的长度
void Byte_String(unsigned char *sour,char *dest,int len)
{
int index;
char tab[]="0123456789ABCDEF";
for(index=0;index<len;index++,sour++,dest++)
{
*dest=tab[*sour>>4];
dest++;
*dest=tab[*sour&0x0f];
}
*dest='\0';
}
//入口参数为转换前号码的字节数组源地址、转换后号码的字节数组目标地址、字节数组的长度
void Address_Convert(unsigned char *sour,unsigned char *dest,int len)
{
int index;
for(index=0;index<len;index++,sour++,dest++)
*dest=(*sour>>4)|(*sour<<4);
}
//入口参数为解码前用户数据的源地址、解码后bit7编码的目标地址、用户数据的字节数
void bit7_Decode(unsigned char *sour,char *dest,int len)
{
int gnumber;
int gcount;
char gindex;
char left;
char gleft;
gnumber=len/7;
for(gcount=0;gcount<gnumber;gcount++)
{
for(gindex=0,left=0;gindex<7;gindex++,sour++,dest++)
{
*dest=((*sour<<gindex)|left)&0x7f;
left=*sour>>(7-gindex);
}
*dest=left;
dest++;
}
gleft=len%7;
if(gleft)
for(gindex=0,left=0;gindex<gleft;gindex++,sour++,dest++)
{
*dest=((*sour<<gindex)|left)&0x7f;
left=*sour>>(7-gindex);
}
*dest='\0';
}
void bit8_Decode(unsigned char *sour,unsigned char *dest,int len)
{
int index;
for(index=0;index<len;index++,sour++,dest++)
*dest=*sour;
}
void UCS2_Decode(unsigned char *sour,char *dest,int len)
{
int index;
int dlen;
WCHAR wchar[128];
for(index=0;index<len/2;index++,sour++)
{
wchar[index]=*sour<<8;
sour++;
wchar[index]|=*sour;
}
dlen=::WideCharToMultiByte(CP_ACP,0,wchar,len/2,dest,160,NULL,NULL);
dest[dlen]='\0';
}
int bit7_Encode(char *sour,unsigned char *dest,int len)
{
int gnumber;
int gcount;
char gindex;
char left;
char gleft;
gnumber=len/8;
for(gcount=0;gcount<gnumber;gcount++)
{
left=*sour;
sour++;
for(gindex=1;gindex<8;gindex++,sour++,dest++)
{
*dest=(*sour<<(8-gindex))|left;
left=*sour>>gindex;
}
}
gleft=len%8;
if(gleft)
{
left=*sour;
sour++;
for(gindex=1;gindex<gleft;gindex++,sour++,dest++)
{
*dest=(*sour<<(8-gindex))|left;
left=*sour>>gindex;
}
*dest=left;
dest++;
}
return 7*gnumber+gleft;
}
int bit8_Encode(char *sour,unsigned char *dest,int len)
{
int index;
for(index=0;index<len;index++,sour++,dest++)
*dest=*sour;
return index;
}
int UCS2_Encode(char *sour,unsigned char *dest,int len)
{
int index;
int dlen;
WCHAR wchar[128];
dlen=::MultiByteToWideChar(CP_ACP,0,sour,len,wchar,128);
for(index=0;index<dlen;index++,dest++)
{
*dest=wchar[index]>>8;
dest++;
*dest=wchar[index]&0xff;
}
return dlen*2;
}
CMessageTestDlg *pView;
/*----------------------------
线程控制函数:
在打开并正确设置串口设备后程序启动一个工作者线程,用于监视串口。
如果线程检测到串口接收到任何字符,读取并在文本框显示。
-----------------------------*/
UINT ComProcess(LPVOID pParam)
{
OVERLAPPED os;
DWORD dwEvtMask,dwEvtTrans;
COMSTAT ComStat;
DWORD dwErrorFlags;
pView=(CMessageTestDlg *)pParam;
//初始化OVERLAPPED结构,为结构创建一个手工重置事件,初始为无信号状态
memset(&os,0,sizeof(OVERLAPPED));
if((os.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL))==NULL)
return (UINT)-1;
//一直监视串口设备,直到串口连接被挂断
while(pView->m_bConnected)
{
//清除所有I/O错误,并返回设备当前状态
ClearCommError(pView->COMFile,&dwErrorFlags,&ComStat);
//如果接收缓冲区中字符的字节数不为0,在允许的情况下通知对话框读取并显示
if(ComStat.cbInQue)
{
//无限等待WM_COMMNOTIFY消息被处理完
WaitForSingleObject(pView->m_hMsgEvent,INFINITE);
ResetEvent(pView->m_hMsgEvent);
//通知对话框
PostMessage(pView->m_hDlgWnd,WM_COMMNOTIFY,EV_RXCHAR,0);
continue;
}
dwEvtMask=0;
if(!WaitCommEvent(pView->COMFile,&dwEvtMask,&os))//重置操作
{
if(GetLastError()==ERROR_IO_PENDING)
//无限等待重置操作结果
GetOverlappedResult(pView->COMFile,&os,&dwEvtTrans,TRUE);
else
{
CloseHandle(os.hEvent);
return (UINT)-1;
}
}
}
CloseHandle(os.hEvent); //关闭事件句柄
return 0;
}
LRESULT CMessageTestApp::OnCommNotify(WPARAM wParam, LPARAM lParam)
{
DWORD nLength;
char abIn[256];
//是否是EV_RXCHAR事件
if(!pView->m_bConnected || (wParam & EV_RXCHAR)!=EV_RXCHAR)
{
SetEvent(pView->m_hMsgEvent);
return 0L;
}
nLength=pView->ReadCommBlock(abIn,161);
abIn[nLength]='\0';
//输出接收字符(编码),并进行解码,显示短消息内容等
if(nLength>0)
{
pView->m_nREncode.Format("%s",abIn);
pView->UpdateData(FALSE);
pView->Decode();
}
//允许发送下一个WM_COMMNOTIFY消息
SetEvent(pView->m_hMsgEvent);
return 0L;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -