📄 weihu.cpp
字号:
// Weihu.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "Weihu.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWeihu dialog
CWeihu::CWeihu(CWnd* pParent /*=NULL*/)
: CDialog(CWeihu::IDD, pParent)
{
//{{AFX_DATA_INIT(CWeihu)
//}}AFX_DATA_INIT
}
void CWeihu::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWeihu)
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWeihu, CDialog)
//{{AFX_MSG_MAP(CWeihu)
ON_BN_CLICKED(IDC_BUTTON_TAIWEI, OnButtonTaiwei)
ON_BN_CLICKED(IDC_BUTTON_QINGDAN, OnButtonQingdan)
ON_BN_CLICKED(IDC_BUTTON_KOUWEI, OnButtonKouwei)
ON_BN_CLICKED(IDC_BUTTON_BEIZHU, OnButtonBeizhu)
ON_BN_CLICKED(IDC_BUTTON_CAIPU, OnButtonCaipu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWeihu message handlers
void CWeihu::OnButtonTaiwei()
{
SOCKET s_sock;
struct sockaddr_in addrClient;
int nCnt,i,j,k,num,sendlen;
char sendbuf[257],recbuf[257];
unsigned short crc;
u_short portid;
u_long SAddr;
int taiweilen,caipulen;
char oprid[21];
char BianHao[21],TanWeiMing[31],ZhuangTai[5],LouCeng[11],LiuShuiHao[11],ShiJian[21];
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;
}
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);
hFile = CreateFile(DB_TAIWEI, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (!hFile)
{
CloseHandle(hFile);
MessageBox(TEXT("打开文件失败"));
return;
}
sprintf(sendbuf,"%c%s%c",0x13,oprid,0x1b);
// sprintf(sendbuf,"%c%s%c",0x13,"001",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)
{
CloseHandle(hFile);
MessageBox(TEXT("通讯失败,请检查设备"));
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 )
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
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]==0x02) break;
if (recbuf[1]==0x03)
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("下传失败!"));
return;
}
}
if (i>=5)
{
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
}
num=(unsigned char)recbuf[3]*256+(unsigned char)recbuf[4];
m_progress.SetRange(0,num);
m_progress.SetPos(0);
m_progress.SetStep(1);
sprintf(sendbuf,"%c%c%c",0x15,0x02,0x1b);
crc=0;
sendlen=strlen(sendbuf);
i=0;
while (i<sendlen)
{
crc+=(unsigned char)sendbuf[i];
i++;
}
k=1;
sendbuf[i]=crc/256;
sendbuf[i+1]=crc%256;
sendbuf[i+2]=0;
sendlen+=3;
if (!num) m_progress.StepIt();
while (k<=num)
{
i=0;
while (i<5)
{
/////
closesocket(s_sock);
s_sock = socket (AF_INET, SOCK_STREAM, 0);
if (s_sock == INVALID_SOCKET)
{
CloseHandle(hFile);
MessageBox(TEXT("通讯失败,请检查设备1"));
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 )
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备2"));
return;
}
//////
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;
break;
}
if (i>=5)
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
}
j=1;
i=0;
while (recbuf[j]!=0x1b)
{
BianHao[i]=recbuf[j];
i++;
j++;
}
BianHao[i]=0;
i=0;
j++;
while (recbuf[j]!=0x1b)
{
TanWeiMing[i]=recbuf[j];
i++;
j++;
}
TanWeiMing[i]=0;
i=0;
j++;
while (recbuf[j]!=0x1b)
{
ZhuangTai[i]=recbuf[j];
i++;
j++;
}
ZhuangTai[i]=0;
i=0;
j++;
while (recbuf[j]!=0x1b)
{
LouCeng[i]=recbuf[j];
i++;
j++;
}
LouCeng[i]=0;
i=0;
j++;
while (recbuf[j]!=0x1b)
{
LiuShuiHao[i]=recbuf[j];
i++;
j++;
}
while (i<10)
{
LiuShuiHao[i]=(char)0xff;
i++;
}
LiuShuiHao[i]=0;
i=0;
j++;
while (recbuf[j]!=0x1b)
{
ShiJian[i]=recbuf[j];
i++;
j++;
}
while (i<20)
{
ShiJian[i]=(char)0xff;
i++;
}
ShiJian[i]=0;
sprintf(recbuf,"%s%c%s%c%s%c%s%c%s%c%s%c%c%c",BianHao,0x1b,TanWeiMing,0x1b,ZhuangTai,0x1b,LouCeng,0x1b,LiuShuiHao,0x1b,ShiJian,0x1b,0x0d,0x0a);
WriteFile(hFile,recbuf,strlen(recbuf),&len,NULL);
sprintf(sendbuf,"%c%c%c",0x17,0x02,0x1b);
crc=0;
sendlen=strlen(sendbuf);
i=0;
while (i<sendlen)
{
crc+=(unsigned char)sendbuf[i];
i++;
}
sendbuf[i]=k/256;
sendbuf[i+1]=k%256;
sendbuf[i+2]=0x1b;
crc+=(unsigned char)sendbuf[i]+(unsigned char)sendbuf[i+1]+(unsigned char)sendbuf[i+2];
sendbuf[i+3]=crc/256;
sendbuf[i+4]=crc%256;
sendbuf[i+5]=0;
sendlen+=6;
k++;
m_progress.StepIt();
}
MessageBox(L"下载完毕");
CloseHandle(hFile);
}
void CWeihu::OnButtonQingdan()
{
SOCKET s_sock;
struct sockaddr_in addrClient;
int nCnt,i,j,k,num,sendlen;
char sendbuf[257],recbuf[257],taiweibianhao[11];
unsigned short crc;
u_short portid;
u_long SAddr;
int taiweilen,caipulen;
char oprid[21];
CString strCode;
//m_title = _T("清单");
//UpdateData(FALSE);
if (m_taiweibianhao.GetLength()==0)
{
MessageBox(TEXT("请返回上一级界面,选择台位后再下载!"));
return;
}
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;
}
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);
hFile = CreateFile(m_taiweibianhao, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (!hFile)
{
MessageBox(TEXT("打开文件失败"));
return;
}
WideCharToMultiByte(CP_ACP,0,m_taiweibianhao,-1,taiweibianhao,255,NULL,NULL);
sprintf(sendbuf,"%c%s%c%s%c",0x27,taiweibianhao,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)
{
CloseHandle(hFile);
MessageBox(TEXT("通讯失败,请检查设备"));
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 )
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
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]==0x02) break;
if (recbuf[1]==0x03)
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("下传失败!"));
return;
}
}
if (i>=5)
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
}
num=(unsigned char)recbuf[3]*256+(unsigned char)recbuf[4];
m_progress.SetRange(0,num);
m_progress.SetPos(0);
m_progress.SetStep(1);
sprintf(sendbuf,"%c%c%c%s%c",0x29,0x02,0x1b,taiweibianhao,0x1b);
crc=0;
sendlen=strlen(sendbuf);
i=0;
while (i<sendlen)
{
crc+=(unsigned char)sendbuf[i];
i++;
}
k=1;
sendbuf[i]=crc/256;
sendbuf[i+1]=crc%256;
sendbuf[i+2]=0;
sendlen+=3;
if (num==0)
{
m_progress.SetRange(0,100);
m_progress.SetPos(100);
}
while (k<=num)
{
i=0;
while (i<5)
{
i++;
closesocket(s_sock);
s_sock = socket (AF_INET, SOCK_STREAM, 0);
if (s_sock == INVALID_SOCKET)
{
CloseHandle(hFile);
MessageBox(TEXT("通讯失败,请检查设备"));
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 )
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
return;
}
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]) break;
}
if (i>=5)
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
}
recbuf[0]='n';
recbuf[nCnt-3]='*';
recbuf[nCnt-2]=0x1b;
recbuf[nCnt-1]=0x0d;
recbuf[nCnt]=0x0a;
recbuf[nCnt+1]=0;
WriteFile(hFile,recbuf,strlen(recbuf),&len,NULL);
sprintf(sendbuf,"%c%c%c%s%c",0x2b,0x02,0x1b,taiweibianhao,0x1b);
crc=0;
sendlen=strlen(sendbuf);
i=0;
while (i<sendlen)
{
crc+=(unsigned char)sendbuf[i];
i++;
}
sendbuf[i]=k/256;
sendbuf[i+1]=k%256;
sendbuf[i+2]=0x1b;
crc+=(unsigned char)sendbuf[i]+(unsigned char)sendbuf[i+1]+(unsigned char)sendbuf[i+2];
sendbuf[i+3]=crc/256;
sendbuf[i+4]=crc%256;
sendbuf[i+5]=0;
sendlen+=6;
k++;
m_progress.StepIt();
}
MessageBox(L"下载完毕");
CloseHandle(hFile);
//m_taiweibianhao=L"";
}
void CWeihu::OnButtonKouwei()
{
SOCKET s_sock;
struct sockaddr_in addrClient;
int nCnt,i,j,k,num,sendlen;
char sendbuf[257],recbuf[257];
unsigned short crc;
u_short portid;
u_long SAddr;
int taiweilen,caipulen;
char oprid[21];
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;
}
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);
hFile = CreateFile(DB_KOUWEI, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (!hFile)
{
CloseHandle(hFile);
MessageBox(TEXT("打开文件失败"));
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -