Home > Back-end >  With idtcpclient and server communication will appear "connection closed gracefully", unde
With idtcpclient and server communication will appear "connection closed gracefully", unde

Time:09-29

With idtcpclient and server communication will appear "connection closed gracefully", the basic process is the client to the server sends data, after the server to perform the operation data to return,
Pray god help analysis, the source code is as follows:
 

Void __fastcall TfmTimerReadOne: : Timer2Timer (TObject * Sender)
{
Byte Recive_Datas [76].
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- the receiving application -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
If (IdTCPClient1 - & gt; Connected ()) {
while(! IdTCPClient1 - & gt; IOHandler - & gt; InputBufferIsEmpty ())
{
Byte Date;
The Date=IdTCPClient1 - & gt; IOHandler - & gt; ReadByte ();
If (Date!=0 XFF) {
Recive_Datas [I]=Date;//only accept 76
i++;
}
If ((Recive_Datas [0]==0 xaf) & amp; & (Recive_Datas [1]==0 XBF) & amp; &//determine whether to accept the DTU data returned head and tail
(Recive_Datas [2]==0 XCF) & amp; & (Recive_Datas [3]==0 XDF) & amp; &//if the judgment for DTU return data, it returns to the PC client
(Recive_Datas] [I - 1==0 XFD) & amp; & (Recive_Datas [I - 2)==0 XFC) & amp; &
(Recive_Datas [I - 3]==0 XFB) & amp; & (Recive_Datas [4] I -==0 xfa)) {
//-- -- -- -- -- -- parse the data -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
AnsiString Meter_ID_H;//the current billing address high
AnsiString Meter_ID_L;//the current billing address low
AnsiString Meter_ID;
AnsiString Now_Heat;//the current heat
AnsiString Now_Flow;//the instantaneous flow
AnsiString Cum_Flow;//the cumulative flow
AnsiString In_Temp;//water temperature
AnsiString Out_Temp;//water temperature
AnsiString Cum_WorkingTime;//total work time
AnsiString differentTemp.//temperature difference
Meter_ID_H=AddZero (BCD_ (Recive_Datas [19])) + AddZero (BCD_ (Recive_Datas [18])) + AddZero (BCD_ (Recive_Datas [17]));
Meter_ID_L=AddZero (BCD_ (Recive_Datas [16])) + AddZero (BCD_ (Recive_Datas [15])) + AddZero (BCD_ (Recive_Datas [14])) + AddZero (BCD_ (Recive_Datas [13]));
Meter_ID=Meter_ID_H + Meter_ID_L;
Now_Heat=IntToStr ((BCD_ (Recive_Datas [32])) + (BCD_ (Recive_Datas [33])) * 100 + (BCD_ (Recive_Datas [34])) * 10000) + ' '+ (BCD_ (Recive_Datas [31]));//the current heat
Now_Flow=IntToStr ((BCD_ (Recive_Datas [44])) * 100) + IntToStr ((BCD_ (Recive_Datas [43]))) + ' '+ IntToStr ((BCD_ (Recive_Datas [42])) + (BCD_ (Recive_Datas [41])/100));//the instantaneous flow
Cum_Flow=IntToStr ((BCD_ (Recive_Datas [49])) * 10000) + IntToStr ((BCD_ (Recive_Datas [48])) * 100) + IntToStr ((BCD_ (Recive_Datas [47]))) + ' '+ IntToStr ((BCD_ (Recive_Datas [46])));//the cumulative flow
In_Temp=IntToStr ((BCD_ (Recive_Datas [52])) * 100 + (BCD_ (Recive_Datas [51]))) + ' '+ IntToStr (BCD_ (Recive_Datas [50]));//water temperature
Out_Temp=IntToStr ((BCD_ (Recive_Datas [55])) * 100 + (BCD_ (Recive_Datas [54]))) + ' '+ IntToStr (BCD_ (Recive_Datas [53]));//water temperature
Cum_WorkingTime=IntToStr ((BCD_ (Recive_Datas [58])) * 10000 + (BCD_ (Recive_Datas [57])) * 100 + BCD_ (Recive_Datas [56]));//total work time
Float fdifferentT=StrToFloat (In_Temp) - StrToFloat (Out_Temp);
DifferentTemp=FormatFloat (" 0.00 ", fdifferentT);
//import and export temperature differenceAnsiString year=IntToStr (BCD_ (Recive_Datas [59]) * 100 + BCD_ (Recive_Datas [60]));//in
AnsiString month=IntToStr (BCD_ (Recive_Datas [61]));//
AnsiString day=IntToStr (BCD_ (Recive_Datas [62]));//day
AnsiString hour=IntToStr (BCD_ (Recive_Datas [63]));//
AnsiString minute=IntToStr (BCD_ (Recive_Datas [64]));//points
AnsiString second=IntToStr (BCD_ (Recive_Datas [65]));//s
AnsiString Read_Time="-" + year + month + "-" + + "" + hour day + +" : "+" : "+ minute second;
AnsiString BatteryState;
If (Recive_Datas [66]==0 x04) {//battery undervoltage
BatteryState="under voltage";
}
The else {
BatteryState="normal";
}
//-- -- -- -- -- -- -- -- -- parsing is complete -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
AnsiString stry.
AnsiString STRM;
AnsiString STRD;
AnsiString STRH;
AnsiString strmin;
AnsiString STRS;
AnsiString strsystime;
SYSTEMTIME * GT=new SYSTEMTIME;//the system time variable
GetLocalTime (GT);//to extract local time
Stry=AnsiString (GT - & GT; WYear);
STRM=AnsiString (GT - & GT; WMonth);
STRD=AnsiString (GT - & GT; WDay);
STRH=AnsiString (GT - & GT; WHour);
Strmin=AnsiString (GT - & GT; WMinute);
STRS=AnsiString (GT - & GT; WSecond);
Strsystime=STRM stry + "/" + + + "" +"/" + STRD STRH + + ":" + ":" + strmin STRS;//the system time in strsystime
AnsiString mysql;
TADOQuery * Query=new TADOQuery (this);
Query - & gt; The Connection=fmMain - & gt; ADOConnection1;
Query - & gt; SQL - & gt; The Clear ();
Query - & gt; SQL - & gt; Add (" select MeterNumber, LastRead from ylt. ResidentMeterRead where MeterNumber='" + Meter_ID + "' ");
Query - & gt; The Open ();
Float Heat=StrToFloat (Now_Heat) - StrToFloat (Query - & gt; FieldByName (" LastRead ") - & gt; AsString) are identical.
AnsiString HeatDiffence=FormatFloat (" 0.00 ", Heat);
If (CheckBox1 - & gt; Checked==true) {
Mysql="UPDATE ylt. ResidentMeterRead SET CurrentRead='" + Now_Heat +"', InstantaneousFlow='";
Mysql +=Now_Flow + "', CumulativeFlow='" + Cum_Flow +"', InletTemperature='" + In_Temp + "', EffluentTemperature='";
Mysql +=Out_Temp + "', ReadMeterMark='success', Heat='" + HeatDiffence +"', BatteryState='" + BatteryState + "', the Runtime='";
Mysql +=Cum_WorkingTime + "', ReadingDate='" + strsystime +"' ";
Mysql +="WHERE MeterNumber='" + Meter_ID +"' ";
}
The else {
Mysql="UPDATE ylt. ResidentMeterReadTemple SET CurrentRead='" + Now_Heat +"', InstantaneousFlow='";
Mysql +=Now_Flow + "', CumulativeFlow='" + Cum_Flow +"', InletTemperature='" + In_Temp + "', EffluentTemperature='";
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related