procedure TForm1.ConnectHost; var re:integer; begin skt:=socket(AF_INET,SOCK_STREAM,0); if skt=INVALID_SOCKET then begin msg('create socket failed!'); exit; end else msg('create socket ok');
ZeroMemory(@addr,sizeof(addr)); addr.sin_family:=AF_INET; addr.sin_addr.S_addr:=inet_addr(pchar(edtHostIP.text)); addr.sin_port:=htons(strtoint(edtHostPort.Text)); re:=connect(skt,addr,sizeof(addr)); if re<>0 then begin msg('connect host '+edtHostIp.Text+' failed!'); exit; end else msg('connect host '+edtHostIp.Text+' ok'); end;
procedure TForm1.FormCreate(Sender: TObject); var WSAData:TWSAData; begin lstTags.Items.LoadFromFile('lics.txt');
if (WSAStartup(makeword(1,0),WSAData)<>0) then begin msg('Initialize WinSock failed!'); exit; end else msg('Initialize winsock ok'); end;
procedure TForm1.msg(myStr: string); begin lblmsg.Caption:=myStr; end;
procedure TForm1.FormDestroy(Sender: TObject); begin WSACleanUp; end;
procedure TForm1.GetData(myStr: string); var BufSend,BufRecv:pchar; re:integer; begin getMem(BufSend,1024); ZeroMemory(BufSend,1024); myStr:=myStr+#10; StrPCopy(BufSend,myStr); re:=send(skt,BufSend^,length(BufSend),0); if re=SOCKET_ERROR then msg('send error');
procedure TForm1.btnSendClick(Sender: TObject); var i,iPos:integer; s:string; s1,s2,s3:string; begin msg('receive remote data ......'); connectHost; lstRecv.Clear; lstSTag.Clear; lstTagValue.Clear; for i:=0 to lstTags.Count-1 do begin application.ProcessMessages; s:=lstTags.Items[i]; // crac_bat11.cpv s1:=copy(s,6,length(s)-4); // bat11.cpv iPos:=pos('.',s); s2:='G01 FG 01 '+s1; // G01 FG 01 BAT11.CPV s3:=copy(s,1,iPos-1); // CRAC_BAT11 lstSTag.Items.Add(s3); try GetData(s2); except msg('error reading remote date... you should retry!'); end; end; msg('End at :'+datetimetostr(now())); closesocket(skt); SaveToDB; msg('Save to infoplus end now :'+datetimetostr(now())); end;
procedure TForm1.Timer1Timer(Sender: TObject); begin btnSendClick(sender); end;
//±£´æÊý¾Ýµ½Êý¾Ý¿âÖÐ procedure TForm1.SaveToDB; var i:integer; sTag,SVal,sSQL:string; begin try conn.Open(); msg('connect database server OK'); except msg('connect database server failed'); exit; end;
// lstSQL.Clear; for i:=0 to lstSTag.Count-1 do begin sTag:=lstSTag.Items[i]; sVal:=lstTagValue.Items[i]; if (sVal<>'62') AND (sVal<>'61') then begin sSQL:='update ip_analogdef set ip_input_value='+sVal+' where name=''+sTag+''; //sSQL:='update ip_analogdef set ip_input_value='+sVal+' where name=''+sTag+''; try cmd.CommandText:=sSQL; cmd.Execute; except msg('error:update failed!'); conn.Close; end; end; // lstSQL.Items.Add(sSQL); end; conn.Close; end;