📄 zhuantai.cpp
字号:
// Zhuantai.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "Zhuantai.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZhuantai dialog
CZhuantai::CZhuantai(CWnd* pParent /*=NULL*/)
: CDialog(CZhuantai::IDD, pParent)
{
//{{AFX_DATA_INIT(CZhuantai)
m_from = _T("");
m_to = _T("");
//}}AFX_DATA_INIT
}
void CZhuantai::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CZhuantai)
DDX_Control(pDX, IDOK, m_ok);
DDX_Control(pDX, IDC_EDIT_TO, m_cto);
DDX_Control(pDX, IDC_EDIT_FROM, m_cfrom);
DDX_Text(pDX, IDC_EDIT_FROM, m_from);
DDV_MaxChars(pDX, m_from, 20);
DDX_Text(pDX, IDC_EDIT_TO, m_to);
DDV_MaxChars(pDX, m_to, 20);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CZhuantai, CDialog)
//{{AFX_MSG_MAP(CZhuantai)
ON_EN_CHANGE(IDC_EDIT_FROM, OnChangeEditFrom)
ON_EN_SETFOCUS(IDC_EDIT_FROM, OnSetfocusEditFrom)
ON_EN_CHANGE(IDC_EDIT_TO, OnChangeEditTo)
ON_EN_SETFOCUS(IDC_EDIT_TO, OnSetfocusEditTo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CZhuantai message handlers
void CZhuantai::OnOK()
{
SOCKET s_sock;
struct sockaddr_in addrClient;
int nCnt,i,j,sendlen;
char sendbuf[257],recbuf[257];
char from[257],to[257];
unsigned short crc;
UpdateData(TRUE);
WideCharToMultiByte(CP_ACP,0,m_from,-1,from,255,NULL,NULL);
WideCharToMultiByte(CP_ACP,0,m_to,-1,to,255,NULL,NULL);
sprintf(sendbuf,"%c%s%c%s%c%s%c",0x03,from,0x1b,to,0x1b,oprid,0x1b);
crc=0;
sendlen=strlen(sendbuf);
i=0;
while (i<sendlen)
{
crc+=(unsigned char)sendbuf[i];
i++;
}
sendbuf[i]=crc/256;
sendbuf[i+1]=crc%256;
sendbuf[i+2]=0;
sendlen+=3;
s_sock = socket (AF_INET, SOCK_STREAM, 0);
if (s_sock == INVALID_SOCKET)
{
MessageBox(TEXT("通讯失败,请检查设备"));
//CDialog::OnCancel();
return;
}
addrClient.sin_family=AF_INET;
addrClient.sin_addr.s_addr=SAddr;
addrClient.sin_port=portid;
if (connect(s_sock, (struct sockaddr *)&addrClient, sizeof (addrClient))==SOCKET_ERROR )
{
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
//CDialog::OnCancel();
return;
}
i=0;
while (i<5)
{
i++;
if (send (s_sock, sendbuf, sendlen, 0) == SOCKET_ERROR)
{
continue;
}
if ((nCnt=recv (s_sock, recbuf,256, 0)) == SOCKET_ERROR)
{
continue;
}
crc=0;
j=0;
while (j<nCnt-3)
{
crc+=(unsigned char)recbuf[j];
j++;
}
if (crc!=(unsigned char)recbuf[j]*256+(unsigned char)recbuf[j+1]) continue;
if (recbuf[1]==0x01) continue;
if (recbuf[1]==0x03)
{
closesocket(s_sock);
MessageBox(TEXT("转台失败!"));
return;
}
closesocket(s_sock);
MessageBox(TEXT("转台成功!"));
CDialog::OnOK();
return;
}
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
//CDialog::OnCancel();
}
BOOL CZhuantai::OnInitDialog()
{
CDialog::OnInitDialog();
HANDLE hFile;
unsigned long len;
hFile = CreateFile(CONFIGFILE, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile==INVALID_HANDLE_VALUE)
{
MessageBox(TEXT("打开配置文件失败,请点“配置”按钮重新配置"));
return FALSE;
}
ReadFile(hFile,&SAddr,4,&len,NULL);
ReadFile(hFile,&portid,2,&len,NULL);
ReadFile(hFile,&taiweilen,4,&len,NULL);
ReadFile(hFile,&caipulen,4,&len,NULL);
ReadFile(hFile,oprid,20,&len,NULL);
CloseHandle(hFile);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CZhuantai::OnChangeEditFrom()
{
UpdateData(TRUE);
if (m_from.GetLength()>=taiweilen) m_cto.SetFocus();
}
void CZhuantai::OnSetfocusEditFrom()
{
m_cfrom.SetSel(0,-1);
}
void CZhuantai::OnChangeEditTo()
{
UpdateData(TRUE);
if (m_to.GetLength()>=taiweilen) m_ok.SetFocus();
}
void CZhuantai::OnSetfocusEditTo()
{
m_cto.SetSel(0,-1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -