关于PLC与上位机通讯(200)

  • 主题发起人 主题发起人 再苦也不说累
  • 开始时间 开始时间

再苦也不说累

Unregistered / Unconfirmed
GUEST, unregistred user!
是通过RS232来通讯的,也就是串口,PLC程序员给了我三段信息0 V0.1 写入0 V0.0 输出0 V0.2 写入请问如果用串口控件,我该发送什么数据另:他们用的是S7-200,串口该怎么设置? 9600 8 N 1吗?
 
各位大富翁帮帮忙 急啊!!!
 
[:(][:(]帮帮忙!
 
有协议吧,根据协议收发数据.
 
to lht123可以具体说下不?以前没太接触过
 
mscomm spcomm 控件接受 数据,有例子自己看,接受二进制数据流 自己分析,根据协议
 
上位机对PLC的通讯和控制,每个品牌几乎都有相应的SDK和控件,你直接使用就好。支持delphi的。当然,如果是遵循标准的通讯格式,你还可以SPCOMM这样的方式进行控制。
 
上位机与下位机通讯协议一般是保密的,不然谁都可以开发组态软件而不用厂家提供的了,西门子的倒是有一套
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ToolWin, ComCtrls, ImgList, StdCtrls, ExtCtrls, Buttons, Unit_dll, Menus, DateUtils,Winsock,SHELLAPI; type TForm1 = class(TForm) ToolBar1: TToolBar; tbStartService: TToolButton; tbStopService: TToolButton; ImageList1: TImageList; ImageList2: TImageList; tbSendData: TToolButton; tbClear: TToolButton; tbQuit: TToolButton; Panel1: TPanel; lvUserTable: TListView; mmDataWnd: TMemo; Timer1: TTimer; ToolButton1: TToolButton; tbctr: TToolButton; CheckBox1: TCheckBox; MainMenu1: TMainMenu; N1: TMenuItem; N2: TMenuItem; N3: TMenuItem; N4: TMenuItem; Timer2: TTimer; N5: TMenuItem; N6: TMenuItem; Hex1: TMenuItem; N7: TMenuItem; N8: TMenuItem; N9: TMenuItem; N10: TMenuItem; N11: TMenuItem; N12: TMenuItem; SaveDialog1: TSaveDialog; N13: TMenuItem; N14: TMenuItem; OpenDialog1: TOpenDialog; Panel2: TPanel; sbSendData: TSpeedButton; Edit1: TEdit; Edit2: TEdit; StatusBar1: TStatusBar; N15: TMenuItem; procedure tbQuitClick(Sender: TObject); procedure tbStartServiceClick(Sender: TObject); procedure tbStopServiceClick(Sender: TObject); procedure tbSendDataClick(Sender: TObject); procedure tbClearClick(Sender: TObject); procedure lvUserTableClick(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure Timer1Timer(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ToolButton1Click(Sender: TObject); procedure tbctrClick(Sender: TObject); procedure N2Click(Sender: TObject); procedure N4Click(Sender: TObject); procedure lvUserTableDblClick(Sender: TObject); procedure N6Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Hex1Click(Sender: TObject); procedure CheckBox1Click(Sender: TObject); procedure N13Click(Sender: TObject); procedure N14Click(Sender: TObject); procedure mmDataWndChange(Sender: TObject); procedure sbSendDataClick(Sender: TObject); procedure Timer2Timer(Sender: TObject); procedure N15Click(Sender: TObject); //procedure Timer2Timer(Sender: TObject); // procedure HEX1Click(Sender: TObject); private { Private declarations } bIfServiceStarted:boolean; function BytetoIP(ui: ModemInfoStruct): String; public procedure AddText(p:PChar); procedure PollUserTable; procedure ProcessData(dr:ModemDataStruct); procedure AddListItem(ui:ModemInfoStruct); function IntToIp(ip:Cardinal):String; function _DateTimeToUnix(const aValue:TDateTime):Int64; { Public declarations } end;var Form1: TForm1; svrport:integer; waittime:integer; SysAutoM:integer;implementationuses hsx_dialogs,Unit2,Unit3,Unit4,Unit5,Unit6;{$R *.DFM}function StrToHex(const s : string) : string;var iCount : integer; Value, CH, CL : Byte;begin SetLength(Result, 2 * Length(s)); for iCount:= 1 to Length(s) do begin Value:= Byte(S[iCount]); CH:= (Value shr 4) and $0F; CL:= Value and $0F; if CL < $0A then CL:= CL + $30 else CL:= CL + $37; if CH < $0A then CH:= CH + $30 else CH:= CH + $37; Result[iCount * 2 - 1]:= Char(CH); Result[iCount * 2]:= Char(CL); end;end;{procedure TForm1.ProcessMessage(var Msg:TMessage);var dr:ModemDataStruct; p:PChar;begin GetMem(p,512); if (DSGetNextData(dr,100)>=0) then begin if dr.m_data_len=0 then PollUserTable else ProcessData(dr); end; FreeMem(p,512);end; }function HexToInt(Hex : string) : Cardinal;const cHex = '0123456789ABCDEF';var mult,i,loop : integer;beginresult := 0;mult := 1;for loop := length(Hex) downto 1 dobegini := pos(Hex[loop],cHex)-1;if (i < 0) then i := 0;inc(result,(i*mult));mult := mult * 16;end;end;procedure TForm1.FormCreate(Sender: TObject);begin svrport:=5000; sbSendData.Enabled:=false; n8.Enabled:=true; n9.Enabled:=false; n10.Enabled:=false; n11.Enabled:=false; n12.Enabled:=false;end;procedure TForm1.tbQuitClick(Sender: TObject);begin Close;end;procedure TForm1.tbStartServiceClick(Sender: TObject);var p:PChar;iRet:integer;strResult:String;begin iRet:=DSStartService(svrport); if (iRet<>0) then begin strResult:='&Iuml;&Atilde;&Atilde;&Aring;&Eacute;&pound;&Egrave;&Ugrave;&iquest;&AElig;&frac14;&frac14;&Ecirc;&yacute;&frac34;&Yacute;&Ouml;&ETH;&ETH;&Auml;·&thorn;&Icirc;&ntilde;&AElig;&ocirc;&para;&macr;,&Otilde;ì&Igrave;&yacute;&para;&Euml;&iquest;&Uacute;&Icirc;&ordf;'+inttostr(svrport); Form1.StatusBar1.Panels[1].Text:='&AElig;&ocirc;&para;&macr;'; end else begin AddText('&Ecirc;&yacute;&frac34;&Yacute;&Ouml;&ETH;&ETH;&Auml;·&thorn;&Icirc;&ntilde;&AElig;&ocirc;&para;&macr;&Ecirc;§°&Uuml;&pound;&iexcl;'); Form1.StatusBar1.Panels[1].Text:='&AElig;&ocirc;&para;&macr;&Ecirc;§°&Uuml;'; end; If SysAutoM = 1 Then begin form1.Timer1.Enabled:=true; end; AddText(PChar(strResult)); bIfServiceStarted:=true; tbStartService.Enabled:=false; tbStopService.Enabled:=true; tbSendData.Enabled:=true; sbSendData.Enabled:=true; tbctr.Enabled:=true; n8.Enabled:=false; n9.Enabled:=true; n10.Enabled:=true; n11.Enabled:=true; n12.Enabled:=true;end;procedure TForm1.tbStopServiceClick(Sender: TObject);var p:PChar;strResult:String;begin GetMem(p,512); if (0=DSStopService) then begin DSGetLastError(p,512); AddText(p); end else begin Form1.StatusBar1.Panels[1].Text:='&Iacute;&pound;&Ouml;&sup1;'; AddText('&Iacute;&pound;&Ouml;&sup1;&Ecirc;&yacute;&frac34;&Yacute;&Ouml;&ETH;&ETH;&Auml;·&thorn;&Icirc;&ntilde;&sup3;&Eacute;&sup1;&brvbar;&pound;&iexcl;'); lvUserTable.Items.Clear ; end; bIfServiceStarted:=FALSE; FreeMem(p,512); tbStartService.Enabled:=true; tbStopService.Enabled:=false; tbSendData.Enabled:=false; sbSendData.Enabled:=false; tbctr.Enabled:=false; n8.Enabled:=true; n9.Enabled:=false; n10.Enabled:=false; n11.Enabled:=false; n12.Enabled:=false;end;procedure TForm1.AddText(p:PChar);begin if mmDataWnd.Lines.Count>100 then mmDataWnd.Lines.Clear; mmDataWnd.Lines.Add(p);end;procedure TForm1.tbClearClick(Sender: TObject);begin mmDataWnd.Lines.Clear;end;procedure TForm1.PollUserTable;var ui:ModemInfoStruct; i:Integer; iDtuAmount:Integer; t_update:Cardinal; p:PChar;begin lvUserTable.Items.Clear; GetMem(p,512); iDtuAmount:=DSGetModemCount; for i:=0 to iDtuAmount-1 do begin DSGetModemByPosition(i,ui); AddListItem(ui); end; FreeMem(p,512);end;{function HexStrToStr(const HexStr : string;const hexlen:integer) : string;const NumericBin : array[$30..$39] of Byte = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);CharBin : array[$41..$46] of Byte = (10, 11, 12, 13, 14, 15);var BH, BL : Byte; iCount : integer; P : ^Byte;begin SetLength(Result, Length(HexStr) div 2); P:= Pointer(Result); for iCount:= 0 to Length(HexStr) div 2 - 1 do begin BH:= Byte(HexStr[iCount * 2 + 1]); if BH >= $41 then BH:= CharBin[BH] else BH:= NumericBin[BH]; BL:= Byte(HexStr[iCount * 2 + 2]); if BL >= $41 then BL:= CharBin[BL] else BL:= NumericBin[BL]; BH:= BH shl 4 + BL and $0F; P^:= BH; Inc(P); end;end; }procedure TForm1.ProcessData(dr:ModemDataStruct);var i:Integer; str:String; strId:String; strDate:String; time:TDateTime; rut:integer;begin strId:=format('%8.8x',[dr.m_modemId]); time:=UnixToDateTime(dr.m_recv_time+28800); strDate:=DateToStr(Date)+' '+TimeToStr(time); str:='ID:'+strId+'---'+'Time:'+strDate+'---'+'Len:'+IntToStr(dr.m_data_len); for i:=1 to Length(str) do if str=#0 then str:=' '; mmdatawnd.Lines.Add(str); dr.m_data_buf[dr.m_data_len]:=#0; if Form1.CheckBox1.Checked =true then begin mmDataWnd.Lines.Add('Data:'+StrToHex(dr.m_data_buf)); end else begin mmDataWnd.Lines.Add('Data:'+dr.m_data_buf); end;end;procedure TForm1.AddListItem(ui:ModemInfoStruct);var list:TListItem;time:TDateTime;strDate:String;begin list:=lvUserTable.Items.Add; list.Caption:=format('%8.8x',[ui.m_modemId]); list.SubItems.Add(ui.m_phoneno); time:=UnixToDateTime(ui.m_conn_time+28800); strDate:=DateTimeToStr(time); list.SubItems.Add(strDate); time:=UnixToDateTime(ui.m_refresh_time+28800); strDate:=DateTimeToStr(time); list.SubItems.Add(strDate); //list.SubItems.Add(IntToIp(ui.m_dynip)); list.SubItems.Add(BytetoIP(ui));end;function TForm1.BytetoIP(ui:ModemInfoStruct):String;var ipstr:String;begin ipstr:=Inttostr(ord(ui.m_dynip[0]))+'.'+Inttostr(ord(ui.m_dynip[1]))+'.'+Inttostr(ord(ui.m_dynip[2]))+'.'+Inttostr(ord(ui.m_dynip[3]));{ ipstr:=Inttostr((ui.m_dynip[0]+256) Mod 256); ipstr:=ipstr+'.'+Inttostr((ui.m_dynip[1]+256) Mod 256); ipstr:=ipstr+'.'+Inttostr((ui.m_dynip[2]+256) Mod 256); ipstr:=ipstr+'.'+Inttostr((ui.m_dynip[3]+256) Mod 256); } BytetoIP:=ipstr;end;function TForm1.IntToIp(ip:Cardinal):String;var s:String;begin s:=''; s:=s+IntToStr(ip div (256*256*256)); s:=s+'.'; s:=s+IntToStr((ip div (256*256)) mod 256); s:=s+'.'; s:=s+IntToStr((ip div 256) mod 256); s:=s+'.'; s:=s+IntToStr(ip mod 256); IntToIp:=s;end;function TForm1._DateTimeToUnix(const AValue:TDateTime):Int64;begin Result := Round((AValue - 25569) * 3600*24);end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);begin if MessageDlg('&Egrave;·&para;¨&Ograve;&ordf;&Iacute;&Euml;&sup3;&ouml;&Acirc;&eth;&pound;&iquest;',mtConfirmation,[mbYes,mbNo],0)=mrNo then CanClose:=false;end;procedure TForm1.Timer1Timer(Sender: TObject);var dr:ModemDataStruct;iRet:integer;begin if (bIfServiceStarted=true) then begin PollUserTable; iRet:=DSGetNextData(dr,0); if (iRet=1) then if (dr.m_data_len<>0) then begin// if dr.m_data_len=0 then// PollUserTable// else ProcessData(dr); end; end;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);var p:PChar;strResult:String;begin GetMem(p,512); if (0=DSStopService) then begin DSGetLastError(p,512); AddText(p); end else begin AddText('&Iacute;&pound;&Ouml;&sup1;&Ecirc;&yacute;&frac34;&Yacute;&Ouml;&ETH;&ETH;&Auml;·&thorn;&Icirc;&ntilde;&sup3;&Eacute;&sup1;&brvbar;!'); end; bIfServiceStarted:=FALSE; FreeMem(p,512); tbStartService.Enabled:=true; tbStopService.Enabled:=false; tbSendData.Enabled:=false; tbctr.Enabled:=false;end;procedure TForm1.ToolButton1Click(Sender: TObject);begin PollUserTable;end;procedure TForm1.tbSendDataClick(Sender: TObject);begin//Try// application.createform(Tform2,form2); form2.ShowModal;//finally// FreeAndNil(Form2);end;procedure TForm1.tbctrClick(Sender: TObject);begin if length(form1.Edit1.Text)>0 then begin Form1.Edit2.Text := '***COMMIT CONFIG***'; form1.sbSendData.Click; form3.ShowModal; end else MessageDlg('&Ccedil;&euml;&Iuml;&Egrave;&Ocirc;&Uacute;&Aacute;&ETH;±í&Eacute;&Iuml;&Ntilde;&iexcl;&Ocirc;&ntilde;&Eacute;è±&cedil;&pound;¨&Ecirc;ó±ê×ó&frac14;ü&micro;&yen;&raquo;÷&Eacute;è±&cedil;ID&ordm;&Aring;&Acirc;&euml;&frac14;&acute;&iquest;&Eacute;&pound;&copy;',mtConfirmation,[mbYes],0);end;procedure TForm1.N2Click(Sender: TObject);begin aboutbox.ShowModal;end;procedure TForm1.N4Click(Sender: TObject);begin close;end;procedure TForm1.lvUserTableDblClick(Sender: TObject);begin tbSendData.Click;end;procedure TForm1.N6Click(Sender: TObject);begin form5:=tform5.Create(application); form5.ShowModal; form5.Free;end;procedure TForm1.Hex1Click(Sender: TObject);begin checkbox1.Checked:=hex1.Checked;end;procedure TForm1.CheckBox1Click(Sender: TObject);begin hex1.Checked:=checkbox1.Checked;end;procedure TForm1.N13Click(Sender: TObject);begin form1.OpenDialog1.FileName:='DataBakup.txt'; ShellExecute(Handle,'open', PAnsiChar(OpenDialog1.FileName), nil,nil,SW_SHOW);end;procedure TForm1.N14Click(Sender: TObject);begin DeleteFile('DataBakup.txt');end;procedure TForm1.mmDataWndChange(Sender: TObject);begin form1.SaveDialog1.FileName:='DataBakup.txt'; mmDataWnd.Lines.SaveToFile(SaveDialog1.FileName);end;procedure TForm1.sbSendDataClick(Sender: TObject);var modemId:Cardinal; fsendresult:integer;begin //&frac12;&laquo;edit1&micro;&Auml;×&Ouml;·&ucirc;&acute;&reg;×&ordf;&raquo;&raquo;&sup3;&Eacute;id&ordm;&Aring; modemId:=StrToInt('$'+Edit1.Text); fsendresult:=DSSendData(modemId,Length(Edit2.Text),PChar(Edit2.Text)); If fsendresult = 1 then begin Form1.addtext (pchar('&Iuml;ò ' + edit1.Text + ' ·&cent;&Euml;&Iacute;&Ecirc;&yacute;&frac34;&Yacute;&pound;&ordm;' + edit2.Text )); form1.AddText('&iquest;&Oslash;&Ouml;&AElig;&Atilde;ü&Aacute;&icirc;·&cent;&Euml;&Iacute;&sup3;&Eacute;&sup1;&brvbar;!'); form3.StatusBar1.Panels[0].Text:='&Iuml;ò'+form3.Edit1.Text+'·&cent;&Euml;&Iacute;&Atilde;ü&Aacute;&icirc;:'+form1.Edit2.Text+'&sup3;&Eacute;&sup1;&brvbar;&pound;&iexcl;'; end Else form1.AddText ('·&cent;&Euml;&Iacute;&Ecirc;§°&Uuml;!');end;procedure TForm1.lvUserTableClick(Sender: TObject);begin if lvUserTable.Selected <> nil then begin form1.Edit1.Text:=lvUserTable.Selected.Caption; form2.Edit1.Text:=lvUserTable.Selected.Caption; form3.Edit1.Text:=lvUserTable.Selected.Caption; end;end;procedure TForm1.Timer2Timer(Sender: TObject);begin Form1.StatusBar1.Panels[2].Text:= DateToStr(Date)+' '+TimeToStr(time);end;procedure TForm1.N15Click(Sender: TObject);begin form6.ShowModal;end;end.
 
GE 西门子 施耐德?哪种PLC MODBUS?PPI什么协议阿
 
S7-200 也有多种协议的 PPI还是MODBUS
 
呵呵,多谢各位,我用TdOPCServer解决了,分数大家分了吧。
 
多人接受答案了。
 

Similar threads

回复
0
查看
848
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部