📄 setiddlg.cpp
字号:
// SetIDDlg.cpp : implementation file
//
#include "stdafx.h"
#include "pcomm.h"
#include "SetID.h"
#include "SetIDDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSetIDDlg dialog
CSetIDDlg::CSetIDDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSetIDDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSetIDDlg)
m_ComNo = 1;
m_ID = 0;
m_RXBUF = _T("");
m_TXBUF = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSetIDDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSetIDDlg)
DDX_Control(pDX, IDC_CMDTYPE, m_CmdType);
DDX_Text(pDX, IDC_COMNO, m_ComNo);
DDV_MinMaxUInt(pDX, m_ComNo, 1, 20);
DDX_Text(pDX, IDC_ID, m_ID);
DDV_MinMaxUInt(pDX, m_ID, 0, 65535);
DDX_Text(pDX, IDC_RXBUF, m_RXBUF);
DDX_Text(pDX, IDC_TXBUF, m_TXBUF);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSetIDDlg, CDialog)
//{{AFX_MSG_MAP(CSetIDDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSetIDDlg message handlers
BOOL CSetIDDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
// TODO: Add extra initialization here
m_ComNo = 1;
m_CmdType.AddString("读取ID");
m_CmdType.AddString("设置ID");
m_CmdType.SetCurSel(0);
m_ID = 0;
openflag = 0;
waitflag = 0;
Count = 0;
rxpt = 0;
handlept = 0;
if(SetTimer(31,50,NULL)==0) MessageBox("Cann't install timer!");
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CSetIDDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSetIDDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSetIDDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/////////////////////////////////////////////////////////////////////////////////
BYTE GetLPC(BYTE *buf,WORD len)
{
WORD i;
BYTE c = 0xff;
for ( i=0; i<len; i++ )
c ^= buf[i];
return c;
}
/////////////////////////////////////////////////////////////////////////////////
void CSetIDDlg::InitMoxa()
{
CString str;
int ret = sio_open(m_ComNo);
if ( ret < 0 ) {
openflag = 0;
str.Format("打开串口%d错误,返回码为%d",m_ComNo,ret);
MessageBox("警告",str);
return;
}
oldcomno = m_ComNo;
openflag = 1;
int mode = BIT_8 | P_NONE | STOP_1;
sio_ioctl(m_ComNo,B115200,mode); // 115200BPS
// Set RTS/CTS flow control
sio_flowctrl(m_ComNo,0);
}
/////////////////////////////////////////////////////////////////////////////////
void CSetIDDlg::OnOK()
{
// TODO: Add extra validation here
if ( waitflag ) {
MessageBox("警告","正在等待数据返回,请稍候!");
return;
}
UpdateData(TRUE);
type = (BYTE)m_CmdType.GetCurSel();
BYTE buf[6],len;
int ret;
CString str;
buf[0] = 0xeb;
buf[1] = 0x90;
if ( type ) {
buf[2] = 5;
buf[3] = (BYTE)m_ID;
buf[4] = (BYTE)(m_ID >> 8);
len = 6;
}
else {
buf[2] = 7;
len = 4;
}
buf[len-1] = GetLPC(buf,len-1);
if ( !openflag ) InitMoxa();
else {
if ( m_ComNo != oldcomno ) {
sio_close(oldcomno);
InitMoxa();
}
}
if ( !openflag ) return;
waitflag = 1;
Count = 60;
rxpt = 0;
handlept = 0;
ret = sio_write(m_ComNo,(LPSTR)buf,len);
if ( ret < 0 ) {
CString str;
str.Format("发送数据出错,返回码为%d",ret);
MessageBox("警告",str);
return;
}
CString str1;
str = "发送:";
for ( int i=0; i<len; i++ ) {
str1.Format("%02X ",buf[i]);
str += str1;
}
m_TXBUF = str;
m_RXBUF = "收到:";
UpdateData(FALSE);
}
void CSetIDDlg::OnCancel()
{
// TODO: Add extra cleanup here
KillTimer(31);
if ( openflag ) sio_close(m_ComNo);
CDialog::OnCancel();
}
void CSetIDDlg::ReadMoxa()
{
if ( waitflag ) {
long len = sio_data_status(m_ComNo);
if ( len < 0 ) {
InitMoxa();
return;
}
else if ( len > 0 ) return;
else {}
len = sio_iqueue(m_ComNo);
if ( !len ) {
Count--;
if ( !Count ) waitflag = 0;
return;
}
else if ( len < 0 )
{
InitMoxa();
return;
}
else {}
if ( len <= (RXBUF_LENGTH - rxpt) )
{
len = sio_read(m_ComNo,(LPSTR)&rxbuf[rxpt],len);
if ( len <= 0 )
{
InitMoxa();
return;
}
}
else {
len = sio_read(m_ComNo,(LPSTR)&rxbuf[rxpt],(RXBUF_LENGTH-rxpt));
if ( len <= 0 )
{
InitMoxa();
return;
}
rxpt = 0;
len = sio_read(m_ComNo,(LPSTR)&rxbuf[rxpt],RXBUF_LENGTH);
if ( len <= 0 )
{
InitMoxa();
return;
}
}
rxpt += (WORD)len;
rxpt &= RXBUF_LENGTH1;
}
}
void CSetIDDlg::RxHandle()
{
BYTE buf1[RXBUF_LENGTH];
BYTE *buf;
WORD len;
WORD i,j;
CString str1,str;
if ( !waitflag ) return;
if ( handlept == rxpt ) return;
if ( handlept < rxpt ) {
for ( i=0; i<(rxpt - handlept); i++ )
buf1[i] = rxbuf[handlept+i];
len = i;
}
else {
for ( i=0; i<(RXBUF_LENGTH-handlept); i++ )
buf1[i] = rxbuf[handlept+i];
for ( j=0; j<rxpt; j++ )
buf1[i+j] = rxbuf[j];
len = i+j;
}
if ( len < 6 ) return;
str = "收到:";
for ( i=0; i<len; i++ ) {
str1.Format("%02X ",buf1[i]);
str += str1;
}
m_RXBUF = str;
UpdateData(FALSE);
buf = &buf1[0];
while(len > 0) {
if ( buf[0] != 0xEB) {
buf++;
len--;
handlept = (handlept + 1) & RXBUF_LENGTH1;
continue;
}
if ( buf[1] != 0x90) {
buf += 2;
len -= 2;
handlept = (handlept + 2) & RXBUF_LENGTH1;
continue;
}
BYTE cmdno = buf[2]; //by lhl
WORD ID;
waitflag = 0;
if ( cmdno == 0x06 ) {
BYTE lpc = GetLPC(buf,6);
if ( lpc ) {
buf += 3;
len -= 3;
handlept = (handlept + 3) & RXBUF_LENGTH1;
continue;
}
ID = buf[3] + (buf[4] << 8);
m_ID = ID;
UpdateData(FALSE);
buf += 6;
len -= 6;
handlept = (handlept + 6) & RXBUF_LENGTH1;
}
else {
handlept += 3;
buf += 3;
len -= 3;
}
}
}
void CSetIDDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent!=31) return;
ReadMoxa();
RxHandle();
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -