-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
112 lines (105 loc) · 3.93 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Tmainform *mainform;
//---------------------------------------------------------------------------
__fastcall Tmainform::Tmainform(TComponent* Owner)
: TForm(Owner)
{
std::vector<TMyIp> VC_MyIpPool;
GetMyIP(VC_MyIpPool);
auto cell = VC_MyIpPool.front();
UDPSvr->Bindings->Add();
UDPSvr->Bindings->Items[0]->IP = cell.IpAddr;
UDPSvr->Active = true;
Label1->Caption = UDPSvr->Bindings->Items[0]->IP;
}
//---------------------------------------------------------------------------
void __fastcall Tmainform::BitBtn1Click(TObject *Sender)
{
std::vector<TBytes> VC_TBytes;
RdPara(0,sizeof(TSysPara),VC_TBytes,TPkgIncrease::increase_1024);
for(const auto &abyte:VC_TBytes){
UDPSvr->Bindings->Items[0]->SendTo(LabeledEdit1->Text,2305,abyte);
}
}
//---------------------------------------------------------------------------
void __fastcall Tmainform::UDPSvrUDPRead(TIdUDPListenerThread *AThread, const TIdBytes AData,
TIdSocketHandle *ABinding)
{
if(ABinding->PeerIP == LabeledEdit1->Text) ShowMessage(L"收到网络回复");
const TCanTRDat* pCanTRDat = reinterpret_cast<const TCanTRDat*>(&AData[AData.Low]);
//解析CAN包头 不对装置地址进行校验
if(strcmp(pCanTRDat->Cmd,"CanRDat") == 0){
ShowMessage(L"连接成功");
return;
}
const TUDPSysPara* pUDPSysPara = reinterpret_cast<const TUDPSysPara*>(&AData[AData.Low]);
if(strcmp(pUDPSysPara->Cmd,"WriteOK!") == 0) ShowMessage(L"下发成功");
else {
const TSysPara* pSysPara = reinterpret_cast<const TSysPara*>(pUDPSysPara->Para);
//显示系统参数
LabeledEdit2->Text = pSysPara->DirectTR;
LabeledEdit3->Text = IPCharToStr(pSysPara->Hostip);
LabeledEdit5->Text = pSysPara->CanBaud;
LabeledEdit9->Text = MACCharToStr(pSysPara->Hostmac);
}
}
//---------------------------------------------------------------------------
void __fastcall Tmainform::BitBtn3Click(TObject *Sender)
{
std::vector<TBytes> VC_TBytes;
char rst = 'r';
WrPara(offsetof(TSysPara,FirstUse1),1,&rst,VC_TBytes);
for(const auto &abyte:VC_TBytes){
UDPSvr->Bindings->Items[0]->SendTo(LabeledEdit1->Text,2305,abyte);
}
}
//---------------------------------------------------------------------------
void __fastcall Tmainform::GCS28xx_Rd_PMeasStr(u8 Add,TBytes &AByte)
{
TCANPSOE CANPSOE{};
CANPSOE.TYP = 21;
CANPSOE.VSQ = 0x81;
CANPSOE.COT = 42;
CANPSOE.ADD = Add;
CANPSOE.FUN = 255;
CANPSOE.INF = 241;
TPCANITEM CANITEM{};
CANITEM.GROUPID = 6;
CANITEM.KOD = 10;
int len = sizeof(CANPSOE) + sizeof(CANITEM);
AByte.set_length(len);
memcpy(&AByte[0],&CANPSOE,sizeof(CANPSOE));
memcpy(&AByte[sizeof(CANPSOE)],&CANITEM,sizeof(CANITEM));
}
void __fastcall Tmainform::BitBtn4Click(TObject *Sender)
{
TBytes AByte; //待打包数据
TBytes UDPBytes; //UDP数据
GCS28xx_Rd_PMeasStr(LabeledEdit4->Text.ToInt(),AByte);
PackCan(LabeledEdit4->Text.ToInt(),AByte.Length,0,&AByte[0],UDPBytes);
UDPSvr->Bindings->Items[0]->SendTo(LabeledEdit1->Text,LabeledEdit7->Text.ToInt(),UDPBytes);
}
//---------------------------------------------------------------------------
void __fastcall Tmainform::BitBtn5Click(TObject *Sender)
{
u8 IP[4]{0};
StrToIPChar(AnsiString(LabeledEdit6->Text).c_str(),IP);
std::vector<TBytes> VC_TBytes;
WrPara(offsetof(TSysPara,Hostip),4,IP,VC_TBytes);
for(const auto &abyte:VC_TBytes){
UDPSvr->Bindings->Items[0]->SendTo(LabeledEdit1->Text,2305,abyte);
}
char Mac[6]{0};
StrToMac6(LabeledEdit8->Text,Mac);
WrPara(offsetof(TSysPara,Hostmac),6,Mac,VC_TBytes);
for(const auto &abyte:VC_TBytes){
UDPSvr->Bindings->Items[0]->SendTo(LabeledEdit1->Text,2305,abyte);
}
}
//---------------------------------------------------------------------------