📄 weihu.cpp
字号:
sprintf(sendbuf,"%c%s%c",0x18,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",0x1a,0x02,0x1b);
crc=0;
sendlen=strlen(sendbuf);
i=0;
while (i<sendlen)
{
crc+=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)
{
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;//inet_addr("192.168.0.119");
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[nCnt-2]=0x0d;
recbuf[nCnt-1]=0x0a;
recbuf[nCnt]=0;
WriteFile(hFile,recbuf,strlen(recbuf),&len,NULL);
sprintf(sendbuf,"%c%c%c",0x1c,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();
}
CloseHandle(hFile);
MessageBox(L"下载完毕");
}
void CWeihu::OnButtonBeizhu()
{
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_BEIZHU, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (!hFile)
{
MessageBox(TEXT("打开文件失败"));
return;
}
sprintf(sendbuf,"%c%s%c",0x1d,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",0x1f,0x02,0x1b);
crc=0;
sendlen=strlen(sendbuf);
i=0;
while (i<sendlen)
{
crc+=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)
{
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]) continue;
break;
}
if (i>=5)
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
}
recbuf[nCnt-2]=0x0d;
recbuf[nCnt-1]=0x0a;
recbuf[nCnt]=0;
WriteFile(hFile,recbuf+1,strlen(recbuf+1),&len,NULL);
sprintf(sendbuf,"%c%c%c",0x21,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();
}
CloseHandle(hFile);
MessageBox(L"下载完毕");
}
void CWeihu::OnButtonCaipu()
{
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;
int taiweilen,caipulen;
u_long SAddr;
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_CAIPU, GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (!hFile)
{
CloseHandle(hFile);
MessageBox(TEXT("打开文件失败"));
return;
}
sprintf(sendbuf,"%c%s%c",0x22,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",0x24,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)
{
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)
{
MessageBox(TEXT("send error"));
continue;
}
//swprintf(wcCode,L"*%d*",k);
//MessageBox(wcCode);
if ((nCnt=recv (s_sock, recbuf, 256, 0)) == SOCKET_ERROR)
{
MessageBox(TEXT("recv error"));
continue;
}
//MessageBox(TEXT("5"));
crc=0;
j=0;
while (j<nCnt-3)
{
crc+=(unsigned char)recbuf[j];
j++;
}
//MessageBox(TEXT("6"));
if (crc==(unsigned char)recbuf[j]*256+(unsigned char)recbuf[j+1]) break;
MessageBox(TEXT("check error"));
}
if (i>=5)
{
CloseHandle(hFile);
closesocket(s_sock);
MessageBox(TEXT("通讯失败,请检查设备"));
}
recbuf[nCnt-2]=0x0d;
recbuf[nCnt-1]=0x0a;
recbuf[nCnt]=0;
WriteFile(hFile,recbuf+1,strlen(recbuf+1),&len,NULL);
sprintf(sendbuf,"%c%c%c",0x26,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();
}
CloseHandle(hFile);
MessageBox(L"下载完毕");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -