📄 mvc65.tmp
字号:
// InsTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "InsTest.h"
#include "InsTestDlg.h"
#include "serial.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CInsTestDlg dialog
CInsTestDlg::CInsTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CInsTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CInsTestDlg)
m_send = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CInsTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CInsTestDlg)
DDX_Control(pDX, IDC_EDIT2, m_rec);
DDX_Text(pDX, IDC_EDIT1, m_send);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CInsTestDlg, CDialog)
//{{AFX_MSG_MAP(CInsTestDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_COMMAND(ID_CONN, OnConn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CInsTestDlg message handlers
DWORD WINAPI ComReadThread(LPVOID lpVoid);
BOOL CInsTestDlg::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
SetWindowText(L"GPRS指令测试");
((CButton *)GetDlgItem(IDC_BUTTON1))->SetWindowText(L"发送");
if (!m_serial.OpenPort(L"COM1:"))
SendMessage(WM_CLOSE);
DWORD dwThreadID;
if (m_hThread = CreateThread(NULL, 0, ComReadThread, this, 0,&dwThreadID)) {
CloseHandle(m_hThread);
} else { //不能创建线程
MessageBox (TEXT("Can't create thread!"), TEXT("Error!"), MB_OK);
SendMessage(WM_CLOSE);
}
return TRUE; // return TRUE unless you set the focus to a control
}
static BYTE * Buffer =new BYTE[1024 + 64];
CSerial * CInsTestDlg::GetSerialPort()
{
return &m_serial;
}
DWORD WINAPI ComReadThread(LPVOID lpVoid)
{
CInsTestDlg *dlg = (CInsTestDlg *)lpVoid;
CSerial * serial = dlg->GetSerialPort();
HANDLE hPort = serial->GetHandle();
DWORD ret; //读入信息时返回值,若为TRUE就是正确读入
DWORD dwCommModemStatus;//串口状态,以判断是否有事件发生
DWORD dwLength;//读入信息的长度
COMSTAT ComStat;//串口状态的详细情况表,
DWORD dwErrorFlags; //读串口状态的标志
//设置程序响应的事件
SetCommMask (hPort, EV_RXCHAR);
//程序在串口有效的状态下,无限循环
while (hPort != INVALID_HANDLE_VALUE) {
//等待串口的事件发生,当dwCommModemStatus值为1时表示接收到数据
WaitCommEvent (hPort, &dwCommModemStatus, 0);
SetCommMask (hPort, EV_RXCHAR);//重新设置程序响应的事件,但这个只是保证程序的安全性
//一般并不起作用
if (dwCommModemStatus & EV_RXCHAR) //检测收到的事件是否为"接收字符"的事件
{
ClearCommError(hPort, &dwErrorFlags, &ComStat);//清除串口状态标志,并返回当前状态
//cbInQue返回在串行驱动程序输入队列中的字符数
dwLength = (ComStat.cbInQue < 1024) ? ComStat.cbInQue : 1024;
if(dwLength > 0) { //防止无故产生事件
//读入数据,并返回数据长度,采用同步方式
ret = serial->ReadPort(Buffer, dwLength);
if(ret <= 0) { //不能从串口读取数据
MessageBox(NULL,TEXT("Can't fetch the data!"),TEXT("Reading Error!"),MB_OK);
} else {
dlg->OnComReadData(Buffer, ret);
}
}
}
// PurgeComm(hPort,PURGE_RXCLEAR);//清空串口的接收缓冲区,必要性不大,但有保证作用
//重新获得串口状态,必要性不大,48表示没有事件产生
GetCommModemStatus (hPort, &dwCommModemStatus);
}
delete[] Buffer;//释放缓冲区
//Just for debug
::MessageBox (NULL, TEXT("Thread Run Out!"), TEXT("Error!"), MB_OK);
return 0L;
}
/*
void CInsTestDlg::OnComReadData(BYTE *pDat, DWORD dwLen)
{
unsigned short DisplayBuf[1025];
static BOOL flag=FALSE;
int tmp;
DWORD i,j;
for(i=0;i<dwLen;i++){
if(pDat[i]==0x5B || flag=TRUE){
j=0;
while(i < dwLen){
flag=FALSE;
tmp=pDat[i]/16;
if (tmp < 9) DisplayBuf[j++]=tmp+'0';
else DisplayBuf[j++]=tmp+'A'-10;
tmp=pDat[i]%16;
if (tmp < 9) DisplayBuf[j++]=tmp+'0';
else DisplayBuf[j++]=tmp+'A'-10;
if(pDat[i]==0x5D) {
flag=TRUE;
DisplayBuf[j++]=' '
break;
}
i++;
}
DisplayBuf[j++]=0;
}
m_rec.ReplaceSel(DisplayBuf);
}
}
*/
void CInsTestDlg::OnComReadData(BYTE *pDat, DWORD dwLen)
{
DWORD i,j;
int nLength;
unsigned short buf[1024];
for (i =0, j=0; i < dwLen; i++,j++){
if (pDat[i]==8){
nLength=m_rec.SendMessage(WM_GETTEXTLENGTH);
if (j>0){
buf[j-1]=0;
m_rec.SetSel(nLength,nLength);
}
else {
buf[0]=0;
m_rec.SetSel(nLength-1,nLength);
}
m_rec.ReplaceSel(buf);
j=-1;
}
else
buf[j]=pDat[i];
}
if(j>0){
buf[j]=0;
nLength=m_rec.SendMessage(WM_GETTEXTLENGTH);
m_rec.SetSel(nLength,nLength);
m_rec.ReplaceSel(buf);
}
}
void CInsTestDlg::OnButton1()
{
CString strSend;
UpdateData(TRUE);
strSend.Format(L"%d",m_send.GetLength());
strSend = "AT^SISW=1,"+strSend+"\r\n";
m_serial.WritePort((unsigned char *)(strSend.GetBuffer(0)),strSend.GetLength());
m_serial.WritePort((unsigned char *)(m_send.GetBuffer(0)),m_send.GetLength());
strSend="\r\n";
m_serial.WritePort((unsigned char *)(strSend.GetBuffer(0)),strSend.GetLength());
}
void CInsTestDlg::OnConn()
{
CString strConn;
strConn=L"at^sics=0,conType,GPRS0\r\nat^sics=0,passwd,gprs\r\nat^sics=0,user,cm\r\nat^sics=0,apn,cmnet\r\n";
m_serial.WritePort((unsigned char *)(strConn.GetBuffer(0)),strConn.GetLength());
strConn=L"at^siss=1,srvType,socket\r\nat^siss=1,conId,0\r\nat^siss=1,address,\"socktcp:\/\/60.177.59.30:5100\"\r\n";
m_serial.WritePort((unsigned char *)(strConn.GetBuffer(0)),strConn.GetLength());
strConn=L"AT^SISO=1\r\n";
m_serial.WritePort((unsigned char *)(strConn.GetBuffer(0)),strConn.GetLength());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -