mainclient.pas 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  1. unit mainclient;
  2. {$IFDEF FPC}
  3. {$MODE Delphi}
  4. {$ENDIF}
  5. interface
  6. uses
  7. {$IFNDEF FPC}
  8. Windows,
  9. {$ELSE}
  10. LCLIntf, LCLType, LMessages,
  11. {$ENDIF}
  12. SyncObjs, SysUtils, DateUtils, Variants, Classes, Graphics, Controls,
  13. Forms, Dialogs, StdCtrls, ComCtrls, Grids,
  14. ExtCtrls, Buttons, sc_info, cp_info,
  15. snap7;
  16. const
  17. amPolling = 0;
  18. amEvent = 1;
  19. amCallBack = 2;
  20. type
  21. { TFormClient }
  22. TFormClient = class(TForm)
  23. CBConnType: TComboBox;
  24. CBPing: TCheckBox;
  25. EdIp: TEdit;
  26. BtnConnect: TButton;
  27. EdLocTsapHI: TEdit;
  28. EdRemTsapHI: TEdit;
  29. EdLocTsapLO: TEdit;
  30. EdRemTsapLO: TEdit;
  31. EdRack: TEdit;
  32. EdSlot: TEdit;
  33. Label1: TLabel;
  34. BtnDisconnect: TButton;
  35. Label2: TLabel;
  36. Label3: TLabel;
  37. Label58: TLabel;
  38. Label59: TLabel;
  39. Label60: TLabel;
  40. Label61: TLabel;
  41. Label62: TLabel;
  42. Label63: TLabel;
  43. Label64: TLabel;
  44. Label65: TLabel;
  45. Label7: TLabel;
  46. EdPDUSize: TStaticText;
  47. PageControl: TPageControl;
  48. PCC: TPageControl;
  49. StatusBar: TStatusBar;
  50. TabSheet1: TTabSheet;
  51. Label4: TLabel;
  52. LblDBNum: TLabel;
  53. Label5: TLabel;
  54. Label6: TLabel;
  55. DataGrid: TStringGrid;
  56. CbArea: TComboBox;
  57. EdDBNum: TEdit;
  58. EdStart: TEdit;
  59. EdAmount: TEdit;
  60. BtnRead: TButton;
  61. BtnWrite: TButton;
  62. BtnAsyncRead: TButton;
  63. BtnAsyncWrite: TButton;
  64. TabSheet2: TTabSheet;
  65. TabSheet3: TTabSheet;
  66. Label9: TLabel;
  67. Label10: TLabel;
  68. Label11: TLabel;
  69. Label12: TLabel;
  70. Label13: TLabel;
  71. Label14: TLabel;
  72. Label15: TLabel;
  73. Label16: TLabel;
  74. Label17: TLabel;
  75. Label18: TLabel;
  76. ComboArea_1: TComboBox;
  77. EdDBNum_1: TEdit;
  78. EdStart_1: TEdit;
  79. EdAmount_1: TEdit;
  80. EdData_1: TEdit;
  81. ComboArea_2: TComboBox;
  82. EdDBNum_2: TEdit;
  83. EdStart_2: TEdit;
  84. EdAmount_2: TEdit;
  85. EdData_2: TEdit;
  86. ComboArea_3: TComboBox;
  87. EdDBNum_3: TEdit;
  88. EdStart_3: TEdit;
  89. EdAmount_3: TEdit;
  90. EdData_3: TEdit;
  91. ComboArea_4: TComboBox;
  92. EdDBNum_4: TEdit;
  93. EdStart_4: TEdit;
  94. EdAmount_4: TEdit;
  95. EdData_4: TEdit;
  96. ComboArea_5: TComboBox;
  97. EdDBNum_5: TEdit;
  98. EdStart_5: TEdit;
  99. EdAmount_5: TEdit;
  100. EdData_5: TEdit;
  101. MultiReadBtn: TButton;
  102. TabSheet4: TTabSheet;
  103. TabSheet8: TTabSheet;
  104. TabZSL: TTabSheet;
  105. TabClock: TTabSheet;
  106. TabSheet7: TTabSheet;
  107. TabSecurity: TTabSheet;
  108. TabControl: TTabSheet;
  109. RGMode: TRadioGroup;
  110. CbWLen: TComboBox;
  111. Label19: TLabel;
  112. LblArea: TLabel;
  113. MultiWriteBtn: TButton;
  114. Label20: TLabel;
  115. EdResult_1: TEdit;
  116. Label21: TLabel;
  117. EdResult_2: TEdit;
  118. Label22: TLabel;
  119. EdResult_3: TEdit;
  120. Label23: TLabel;
  121. EdResult_4: TEdit;
  122. Label24: TLabel;
  123. EdResult_5: TEdit;
  124. Label25: TLabel;
  125. GroupBox1: TGroupBox;
  126. Label26: TLabel;
  127. txtOB: TStaticText;
  128. Label28: TLabel;
  129. txtFB: TStaticText;
  130. Label29: TLabel;
  131. txtFC: TStaticText;
  132. Label30: TLabel;
  133. txtSFB: TStaticText;
  134. Label31: TLabel;
  135. txtSFC: TStaticText;
  136. Label32: TLabel;
  137. txtDB: TStaticText;
  138. Label27: TLabel;
  139. Label33: TLabel;
  140. txtSDB: TStaticText;
  141. BtnBlockList: TButton;
  142. GroupBox2: TGroupBox;
  143. cbBlock: TComboBox;
  144. EdBlkNum: TEdit;
  145. MemoBlk: TMemo;
  146. BlkInfoBtn: TButton;
  147. GroupBox3: TGroupBox;
  148. CbBot: TComboBox;
  149. BoTBtn: TButton;
  150. ReadSZLBtn: TButton;
  151. MemoSZL: TMemo;
  152. EdID: TEdit;
  153. Label34: TLabel;
  154. Label35: TLabel;
  155. EdIndex: TEdit;
  156. AsReadSZLBtn: TButton;
  157. lblSZLdump: TLabel;
  158. TimClock: TTimer;
  159. GrPGDateTime: TGroupBox;
  160. ChkGetDateTime: TCheckBox;
  161. grAGDateTime: TGroupBox;
  162. Button7: TButton;
  163. Label37: TLabel;
  164. EdDBNumGet: TEdit;
  165. LblDBDump: TLabel;
  166. MemoDB: TMemo;
  167. DBGetBtn: TButton;
  168. AsDBGetBtn: TButton;
  169. TabSheet6: TTabSheet;
  170. GroupBox5: TGroupBox;
  171. Label44: TLabel;
  172. EdPdu: TEdit;
  173. Label45: TLabel;
  174. EdConnections: TEdit;
  175. Label46: TLabel;
  176. EdMpiRate: TEdit;
  177. Label47: TLabel;
  178. EdBusRate: TEdit;
  179. GroupBox6: TGroupBox;
  180. Label41: TLabel;
  181. EdModuleTypeName: TEdit;
  182. Label42: TLabel;
  183. EdSerialNumber: TEdit;
  184. Label43: TLabel;
  185. EdCopyright: TEdit;
  186. GroupBox7: TGroupBox;
  187. Label40: TLabel;
  188. edOrderCode: TEdit;
  189. ListBot: TListBox;
  190. LblDblClick: TLabel;
  191. lbSZL: TListBox;
  192. lblSZLCount: TLabel;
  193. lblSZL: TLabel;
  194. Label49: TLabel;
  195. TimStatus: TTimer;
  196. Button12: TButton;
  197. Button13: TButton;
  198. TabSheet5: TTabSheet;
  199. cbBlkType: TComboBox;
  200. EdNum: TEdit;
  201. Label48: TLabel;
  202. Label50: TLabel;
  203. lblUpld: TLabel;
  204. MemoUpload: TMemo;
  205. UpBtn: TButton;
  206. AsUpBtn: TButton;
  207. ChkFull: TCheckBox;
  208. MemoBlkInfo: TMemo;
  209. lblNewNumber: TLabel;
  210. EdNewNumber: TEdit;
  211. DnBtn: TButton;
  212. AsDnBtn: TButton;
  213. BlkSaveBtn: TButton;
  214. SaveDialog: TSaveDialog;
  215. Button4: TButton;
  216. OpenDialog: TOpenDialog;
  217. Button14: TButton;
  218. GroupBox4: TGroupBox;
  219. lblStatus: TLabel;
  220. Button9: TButton;
  221. Button10: TButton;
  222. Button11: TButton;
  223. ChkStatusRefresh: TCheckBox;
  224. BtnStatus: TButton;
  225. EdVersion: TEdit;
  226. Label36: TLabel;
  227. Shape1: TShape;
  228. Label51: TLabel;
  229. EdASName: TEdit;
  230. Label52: TLabel;
  231. EdModuleName: TEdit;
  232. Button1: TButton;
  233. ChkSecurity: TCheckBox;
  234. GroupBox8: TGroupBox;
  235. EdPassword: TEdit;
  236. Button5: TButton;
  237. Button8: TButton;
  238. TimSecurity: TTimer;
  239. Panel1: TPanel;
  240. RG_sch_schal: TRadioGroup;
  241. RG_sch_par: TRadioGroup;
  242. RG_sch_rel: TRadioGroup;
  243. RG_bart_sch: TRadioGroup;
  244. RG_anl_sch: TRadioGroup;
  245. AsBotBtn: TButton;
  246. GroupBox9: TGroupBox;
  247. Label8: TLabel;
  248. EdTimeout: TEdit;
  249. Button3: TButton;
  250. Button15: TButton;
  251. Button16: TButton;
  252. Button17: TButton;
  253. ChEd_1: TEdit;
  254. Label53: TLabel;
  255. ChEd_2: TEdit;
  256. ChEd_3: TEdit;
  257. ChEd_4: TEdit;
  258. ChEd_5: TEdit;
  259. Label54: TLabel;
  260. Label55: TLabel;
  261. GroupBox10: TGroupBox;
  262. Label56: TLabel;
  263. Label57: TLabel;
  264. EdDBFill: TEdit;
  265. EdFill: TEdit;
  266. FillBtn: TButton;
  267. AsFillBtn: TButton;
  268. Label38: TLabel;
  269. Label39: TLabel;
  270. edPGDate: TEdit;
  271. edPGTime: TEdit;
  272. edAGDate: TEdit;
  273. edAGTime: TEdit;
  274. procedure Button2Click(Sender: TObject);
  275. procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  276. procedure FormCreate(Sender: TObject);
  277. procedure FormDestroy(Sender: TObject);
  278. procedure BtnConnectClick(Sender: TObject);
  279. procedure BtnDisconnectClick(Sender: TObject);
  280. procedure CbAreaChange(Sender: TObject);
  281. procedure DataGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  282. Rect: TRect; State: TGridDrawState);
  283. procedure EdRackKeyPress(Sender: TObject; var Key: Char);
  284. procedure DataGridExit(Sender: TObject);
  285. procedure DataGridKeyPress(Sender: TObject; var Key: Char);
  286. procedure BtnReadClick(Sender: TObject);
  287. procedure BtnWriteClick(Sender: TObject);
  288. procedure BtnAsyncReadClick(Sender: TObject);
  289. procedure Label63Click(Sender: TObject);
  290. procedure Label64Click(Sender: TObject);
  291. procedure MultiReadBtnClick(Sender: TObject);
  292. procedure RGModeClick(Sender: TObject);
  293. procedure BtnAsyncWriteClick(Sender: TObject);
  294. procedure MultiWriteBtnClick(Sender: TObject);
  295. procedure BtnBlockListClick(Sender: TObject);
  296. procedure BlkInfoBtnClick(Sender: TObject);
  297. procedure ReadSZLBtnClick(Sender: TObject);
  298. procedure EdIDKeyPress(Sender: TObject; var Key: Char);
  299. procedure AsReadSZLBtnClick(Sender: TObject);
  300. procedure PageControlChange(Sender: TObject);
  301. procedure TimClockTimer(Sender: TObject);
  302. procedure ChkGetDateTimeClick(Sender: TObject);
  303. procedure Button7Click(Sender: TObject);
  304. procedure DBGetBtnClick(Sender: TObject);
  305. procedure AsDBGetBtnClick(Sender: TObject);
  306. procedure BoTBtnClick(Sender: TObject);
  307. procedure ListBotDblClick(Sender: TObject);
  308. procedure CbBotCloseUp(Sender: TObject);
  309. procedure lbSZLDblClick(Sender: TObject);
  310. procedure Button9Click(Sender: TObject);
  311. procedure Button10Click(Sender: TObject);
  312. procedure Button11Click(Sender: TObject);
  313. procedure txtOBDblClick(Sender: TObject);
  314. procedure TimStatusTimer(Sender: TObject);
  315. procedure Button12Click(Sender: TObject);
  316. procedure Button13Click(Sender: TObject);
  317. procedure UpBtnClick(Sender: TObject);
  318. procedure AsUpBtnClick(Sender: TObject);
  319. procedure ChkFullClick(Sender: TObject);
  320. procedure DnBtnClick(Sender: TObject);
  321. procedure AsDnBtnClick(Sender: TObject);
  322. procedure BlkSaveBtnClick(Sender: TObject);
  323. procedure Button4Click(Sender: TObject);
  324. procedure Button5Click(Sender: TObject);
  325. procedure Button8Click(Sender: TObject);
  326. procedure Button14Click(Sender: TObject);
  327. procedure BtnStatusClick(Sender: TObject);
  328. procedure ChkStatusRefreshClick(Sender: TObject);
  329. procedure Button1Click(Sender: TObject);
  330. procedure TimSecurityTimer(Sender: TObject);
  331. procedure ChkSecurityClick(Sender: TObject);
  332. procedure AsBotBtnClick(Sender: TObject);
  333. procedure Button3Click(Sender: TObject);
  334. procedure Button15Click(Sender: TObject);
  335. procedure Button16Click(Sender: TObject);
  336. procedure Button17Click(Sender: TObject);
  337. procedure FillBtnClick(Sender: TObject);
  338. procedure AsFillBtnClick(Sender: TObject);
  339. procedure MultiVarReadBtnClick(Sender: TObject);
  340. procedure MultiVarWriteBtnClick(Sender: TObject);
  341. private
  342. { Private declarations }
  343. Client : TS7Client;
  344. FConnected: boolean;
  345. FLastError: integer;
  346. FLastOP: string;
  347. Buffer : TS7Buffer;
  348. BlkBuffer : TS7Buffer;
  349. BlkBufSize : integer;
  350. DataItems : TS7DataItems;
  351. BlocksList : TS7BlocksList;
  352. BlockInfo : TS7BlockInfo;
  353. AsMode : integer;
  354. AsOpResult : integer;
  355. function WordSize(Amount, WordLength : integer) : integer;
  356. procedure CheckArea;
  357. procedure SetFConnected(const Value: boolean);
  358. procedure SetFLastError(const Value: integer);
  359. procedure ValidateGrid;
  360. procedure ClientConnect;
  361. procedure ClientDisconnect;
  362. procedure FillBlockInfo(Memo : TMemo; Info : PS7BlockInfo);
  363. procedure DataToGrid(Amount : integer);
  364. procedure GridToData(Amount : integer);
  365. procedure DumpData(P : PS7Buffer; Memo : TMemo; Count : integer);
  366. procedure Read(Async : boolean);
  367. procedure Write(Async : boolean);
  368. procedure DBFill(ASync : boolean);
  369. procedure MultiRead;
  370. procedure MultiWrite;
  371. procedure DBGet(Async : boolean);
  372. procedure ListBlocks;
  373. procedure GetBlockInfo;
  374. procedure ListBlocksOfType(Async : boolean);
  375. procedure Upload(Full, Async : boolean);
  376. procedure GetSysInfo;
  377. procedure ReadSZL(Async : boolean);
  378. procedure ReadSZLList(Async : boolean);
  379. procedure SetFLastOP(const Value: string);
  380. procedure Elapse; overload;
  381. procedure Elapse(TotTime : cardinal); overload;
  382. procedure WaitCompletion(Const Timeout : integer = 1500);
  383. procedure ClearPages;
  384. procedure ClearSystemInfo;
  385. procedure ClearMultiReadWrite;
  386. procedure ClearDirectory;
  387. procedure ClearSZL;
  388. procedure ClearDBGet;
  389. procedure ClearUpDownload;
  390. procedure ClearProtection;
  391. procedure GetStatus;
  392. procedure GetProtection(const DoShowInfo : boolean = true);
  393. procedure SetPassword;
  394. procedure ClearPassword;
  395. procedure CopyRamToRom(Async : boolean);
  396. procedure Compress(Async : boolean);
  397. procedure FillBlkBuffer(p : pointer; Size : integer);
  398. procedure ClearBlkBuffer;
  399. procedure SaveToFile(Const FileName : string; P : pointer; Size : integer);
  400. procedure DeleteBlock;
  401. function LoadFromFile(Const FileName : string; P : pointer; var Size : integer) : boolean;
  402. function CliError(Error : integer) : string;
  403. function CliTime : cardinal;
  404. function CliPDULength : integer;
  405. public
  406. EvJob : TEvent;
  407. JobDone : boolean;
  408. { Public declarations }
  409. property Connected : boolean read FConnected write SetFConnected;
  410. property LastOP : string read FLastOP write SetFLastOP;
  411. property LastError : integer read FLastError write SetFLastError;
  412. end;
  413. var
  414. FormClient: TFormClient;
  415. implementation
  416. {$R *.lfm}
  417. // This procedure is called by client when AsyncMode = amCallBack
  418. procedure ClientCompletion(usrPtr : pointer; opCode, opResult : integer);
  419. {$IFDEF MSWINDOWS}stdcall;{$ELSE}cdecl;{$ENDIF}
  420. begin
  421. // in this demo we have nothing to do : set an event
  422. TFormClient(usrPtr).AsOpResult:=opResult;
  423. end;
  424. const
  425. AreaOf : array[0..5] of byte = (
  426. S7AreaDB, S7AreaPE, S7AreaPA, S7AreaMK, S7AreaTM, S7AreaCT
  427. );
  428. WLenOf : array[0..14] of integer = (
  429. S7WLBit,
  430. S7WLByte,
  431. S7WLChar,
  432. S7WLWord,
  433. S7WLInt,
  434. S7WLDWord,
  435. S7WLDInt,
  436. S7WLReal,
  437. S7WLDate,
  438. S7WLTOD,
  439. S7WLTime,
  440. S7WLS5Time,
  441. S7WLDT,
  442. S7WLCounter,
  443. S7WLTimer
  444. );
  445. SizeByte : array[0..14] of integer = (
  446. 1, 1, 1, 2, 2, 4, 4, 4, 2, 4, 4, 2, 8, 2, 2
  447. );
  448. BlockOf : array[0..6] of integer = (
  449. Block_OB, Block_FB, Block_FC, Block_DB, Block_SFB, Block_SFC, Block_SDB
  450. );
  451. function LangOf(Lang : integer) : string;
  452. begin
  453. case Lang of
  454. BlockLangAWL : Result:='AWL';
  455. BlockLangKOP : Result:='KOP';
  456. BlockLangFUP : Result:='FUP';
  457. BlockLangSCL : Result:='SCL';
  458. BlockLangDB : Result:='DB';
  459. BlockLangGRAPH : Result:='GRAPH';
  460. else
  461. Result:='Unknown';
  462. end;
  463. end;
  464. function SubBlkOf(SubBlk : integer) : string;
  465. begin
  466. case SubBlk of
  467. SubBlk_OB : Result:='OB';
  468. SubBlk_DB : Result:='DB';
  469. SubBlk_SDB : Result:='SDB';
  470. SubBlk_FC : Result:='FC';
  471. SubBlk_SFC : Result:='SFC';
  472. SubBlk_FB : Result:='FB';
  473. SubBlk_SFB : Result:='SFB';
  474. else
  475. Result:='Unknown';
  476. end;
  477. end;
  478. procedure TFormClient.BtnConnectClick(Sender: TObject);
  479. begin
  480. ClientConnect;
  481. end;
  482. procedure TFormClient.BtnDisconnectClick(Sender: TObject);
  483. begin
  484. ClientDisconnect;
  485. end;
  486. procedure TFormClient.BtnReadClick(Sender: TObject);
  487. begin
  488. Read(false);
  489. end;
  490. procedure TFormClient.BtnWriteClick(Sender: TObject);
  491. begin
  492. Write(false);
  493. end;
  494. procedure TFormClient.AsReadSZLBtnClick(Sender: TObject);
  495. begin
  496. ReadSZL(true);
  497. end;
  498. procedure TFormClient.Button10Click(Sender: TObject);
  499. begin
  500. Client.PlcHotStart;
  501. end;
  502. procedure TFormClient.Button11Click(Sender: TObject);
  503. begin
  504. Client.PlcColdStart;
  505. end;
  506. procedure TFormClient.Button12Click(Sender: TObject);
  507. begin
  508. ReadSZLList(false);
  509. end;
  510. procedure TFormClient.Button13Click(Sender: TObject);
  511. begin
  512. GetSysInfo;
  513. end;
  514. procedure TFormClient.Button14Click(Sender: TObject);
  515. begin
  516. DeleteBlock;
  517. end;
  518. procedure TFormClient.Button15Click(Sender: TObject);
  519. begin
  520. CopyRamToRom(true);
  521. end;
  522. procedure TFormClient.Button16Click(Sender: TObject);
  523. begin
  524. Compress(false);
  525. end;
  526. procedure TFormClient.Button17Click(Sender: TObject);
  527. begin
  528. Compress(true);
  529. end;
  530. procedure TFormClient.Button1Click(Sender: TObject);
  531. begin
  532. GetProtection;
  533. end;
  534. procedure TFormClient.BtnStatusClick(Sender: TObject);
  535. begin
  536. GetStatus;
  537. end;
  538. procedure TFormClient.ClearPages;
  539. begin
  540. ClearSystemInfo;
  541. ClearMultiReadWrite;
  542. ClearDirectory;
  543. ClearSZL;
  544. ClearDBGet;
  545. ClearUpDownload;
  546. ClearProtection;
  547. end;
  548. procedure TFormClient.ClearPassword;
  549. begin
  550. LastOp:='Clear Session password';
  551. LastError:=Client.ClearSessionPassword;
  552. Elapse;
  553. end;
  554. procedure TFormClient.ClearProtection;
  555. begin
  556. RG_sch_schal.ItemIndex:=0;
  557. RG_sch_par.ItemIndex:=0;
  558. RG_sch_rel.ItemIndex:=0;
  559. RG_bart_sch.ItemIndex:=0;
  560. RG_anl_sch.ItemIndex:=0;
  561. end;
  562. procedure TFormClient.UpBtnClick(Sender: TObject);
  563. begin
  564. Upload(ChkFull.Checked,false);
  565. end;
  566. procedure TFormClient.AsUpBtnClick(Sender: TObject);
  567. begin
  568. Upload(ChkFull.Checked,true);
  569. end;
  570. procedure TFormClient.BtnBlockListClick(Sender: TObject);
  571. begin
  572. ListBlocks;
  573. end;
  574. procedure TFormClient.Button3Click(Sender: TObject);
  575. begin
  576. CopyRamToRom(false)
  577. end;
  578. procedure TFormClient.Button4Click(Sender: TObject);
  579. Var
  580. Size : integer;
  581. begin
  582. if OpenDialog.Execute then
  583. begin
  584. if LoadFromFile(OpenDialog.FileName,@BlkBuffer,Size) then
  585. begin
  586. FillBlkBuffer(@BlkBuffer,Size);
  587. Client.GetPgBlockInfo(@BlkBuffer,@BlockInfo,Size);
  588. FillBlockInfo(MemoBlkInfo,@BlockInfo);
  589. DumpData(@BlkBuffer,MemoUpload,Size);
  590. lblUpld.Caption:='Block Dump : '+IntToStr(Size)+' byte'
  591. end;
  592. end;
  593. end;
  594. procedure TFormClient.Button5Click(Sender: TObject);
  595. begin
  596. SetPassword;
  597. end;
  598. procedure TFormClient.BlkSaveBtnClick(Sender: TObject);
  599. begin
  600. if SaveDialog.Execute then
  601. SaveToFile(SaveDialog.FileName,@BlkBuffer,BlkBufSize);
  602. end;
  603. procedure TFormClient.Button7Click(Sender: TObject);
  604. Var
  605. DT : TDateTime;
  606. AGDate : TDateTime;
  607. AGTime : TDateTime;
  608. begin
  609. TimClock.Enabled:=false;
  610. LastOp:='Set PLC Date and Time';
  611. if not ChkGetDateTime.Checked then
  612. begin
  613. if TryStrToDate(edAGDate.Text,AGDate) and TryStrToTime(edAGTime.Text,AGTime) then
  614. begin
  615. DT:=AGDate+AGTime;
  616. LastError:=Client.SetPlcDateTime(DT);
  617. end
  618. else
  619. MessageDlg('Date and/or Time format error',mtError,[mbOk],0);
  620. end
  621. else
  622. LastError:=Client.SetPlcSystemDateTime;
  623. Elapse;
  624. ChkGetDateTime.Checked:=true;
  625. TimClock.Enabled:=true;
  626. end;
  627. procedure TFormClient.Button8Click(Sender: TObject);
  628. begin
  629. ClearPassword;
  630. end;
  631. procedure TFormClient.ReadSZLBtnClick(Sender: TObject);
  632. begin
  633. ReadSZL(false);
  634. end;
  635. procedure TFormClient.ReadSZLList(Async: boolean);
  636. Var
  637. SZLList : TS7SZLList;
  638. Count : integer;
  639. c: Integer;
  640. begin
  641. LastOp:='Read SZL List';
  642. lblSZL.Visible:=false;
  643. lbSZL.Items.Clear;
  644. Count:=SizeOf(SZLList);
  645. LastError:=Client.ReadSZLList(@SZLList,Count);
  646. if LastError=0 then
  647. begin
  648. for c := 0 to Count - 1 do
  649. lbSZL.Items.Add('$'+IntToHex(SZLList.List[c],4));
  650. lblSZL.Visible:=lbSZL.Items.Count>0;
  651. end;
  652. lblSZLCount.Caption:='List of All SZL IDs : '+inttostr(Count);
  653. Elapse;
  654. end;
  655. procedure TFormClient.Button9Click(Sender: TObject);
  656. begin
  657. Client.PlcStop;
  658. end;
  659. procedure TFormClient.AsBotBtnClick(Sender: TObject);
  660. begin
  661. ListBlocksOfType(true);
  662. end;
  663. procedure TFormClient.AsDBGetBtnClick(Sender: TObject);
  664. begin
  665. DBGet(true);
  666. end;
  667. procedure TFormClient.AsDnBtnClick(Sender: TObject);
  668. Var
  669. BlockNum : integer;
  670. begin
  671. LastOp:='Async Download';
  672. BlockNum:=StrToIntDef(EdNewNumber.Text,0);EdNewNumber.Text:=IntToStr(BlockNum);
  673. LastError:=Client.AsDownload(BlockNum,@BlkBuffer,BlkBufSize);
  674. if LastError=0 then
  675. WaitCompletion;
  676. Elapse;
  677. end;
  678. procedure TFormClient.AsFillBtnClick(Sender: TObject);
  679. begin
  680. DBFill(true);
  681. end;
  682. procedure TFormClient.GetBlockInfo;
  683. Var
  684. BlockType : integer;
  685. BlockNum : integer;
  686. begin
  687. BlockType:=BlockOf[cbBlock.ItemIndex];
  688. BlockNum:=StrToIntDef(EdBlkNum.Text,0);
  689. fillchar(BlockInfo,SizeOf(TS7BlockInfo),#0);
  690. MemoBlk.Lines.Clear;
  691. LastOP:='Block Info';
  692. LastError:=Client.GetAgBlockInfo(BlockType,BlockNum,@BlockInfo);
  693. if LastError=0 then
  694. begin
  695. if LastError=0 then
  696. FillBlockInfo(MemoBlk,@BlockInfo);
  697. end;
  698. Elapse;
  699. end;
  700. procedure TFormClient.GetProtection(const DoShowInfo : boolean = true);
  701. Var
  702. Info : TS7Protection;
  703. procedure SetRGValue(RG : TRadioGroup; Value : word);
  704. begin
  705. if Value>RG.Items.Count-1 then
  706. RG.ItemIndex:=0
  707. else
  708. RG.ItemIndex:=Value;
  709. end;
  710. begin
  711. if DoShowInfo then
  712. LastOp:='Get Protection Info';
  713. LastError:=Client.GetProtection(@Info);
  714. if LastError=0 then
  715. begin
  716. SetRGValue(RG_sch_schal,Info.sch_schal);
  717. SetRGValue(RG_sch_par,Info.sch_par);
  718. SetRGValue(RG_sch_rel,Info.sch_rel);
  719. SetRGValue(RG_bart_sch,Info.bart_sch);
  720. SetRGValue(RG_anl_sch,Info.anl_sch);
  721. end;
  722. if DoShowInfo then
  723. Elapse;
  724. end;
  725. procedure TFormClient.GetStatus;
  726. Var
  727. Status : integer;
  728. procedure Run;
  729. begin
  730. lblStatus.Font.Color:=clGreen;
  731. lblStatus.Caption:='RUN';
  732. end;
  733. procedure Stop;
  734. begin
  735. lblStatus.Font.Color:=clRed;
  736. lblStatus.Caption:='STOP';
  737. end;
  738. procedure Unknown;
  739. begin
  740. lblStatus.Font.Color:=clGray;
  741. lblStatus.Caption:='Unknown';
  742. end;
  743. begin
  744. LastOp:='Get Plc Status';
  745. LastError:=Client.GetPlcStatus(Status);
  746. if LastError=0 then
  747. begin
  748. case Status of
  749. S7CpuStatusUnknown : Unknown;
  750. S7CpuStatusRun : Run;
  751. S7CpuStatusStop : Stop;
  752. end;
  753. end
  754. else
  755. Unknown;
  756. Elapse;
  757. end;
  758. procedure TFormClient.GetSysInfo;
  759. Var
  760. OrderCode : TS7OrderCode;
  761. CpuInfo : TS7CpuInfo;
  762. CpInfo : TS7CpInfo;
  763. TotTime : Cardinal;
  764. begin
  765. LastOp:='Get System Info';
  766. ClearSystemInfo;
  767. TotTime:=0;
  768. LastError:=Client.GetOrderCode(@OrderCode);
  769. if LastError=0 then
  770. begin
  771. EdOrderCode.Text:=String(OrderCode.Code);
  772. EdVersion.Text:='V '+IntToStr(OrderCode.V1)+'.'+
  773. IntToStr(OrderCode.V2)+'.'+
  774. IntToStr(OrderCode.V3);
  775. end
  776. else begin
  777. EdOrderCode.Text:='NO INFO AVAILABLE';
  778. EdVersion.Text:='';
  779. end;
  780. Inc(TotTime,CliTime);
  781. LastError:=Client.GetCpuInfo(@CpuInfo);
  782. if LastError=0 then
  783. begin
  784. EdModuleTypeName.Text :=String(CpuInfo.ModuleTypeName);
  785. EdSerialNumber.Text :=String(CpuInfo.SerialNumber);
  786. EdCopyright.Text :=String(CpuInfo.Copyright);
  787. EdASName.Text :=String(CpuInfo.ASName);
  788. EdModuleName.Text :=String(CpuInfo.ModuleName);
  789. end;
  790. Inc(TotTime,CliTime);
  791. LastError:=Client.GetCPInfo(@CpInfo);
  792. if LastError=0 then
  793. begin
  794. EdPdu.Text:=IntToStr(CpInfo.MaxPduLengt);
  795. EdConnections.Text:=IntToStr(CpInfo.MaxConnections);
  796. EdMpiRate.Text:=IntToStr(CpInfo.MaxMpiRate);
  797. EdBusRate.Text:=IntToStr(CpInfo.MaxBusRate);
  798. end;
  799. Inc(TotTime,CliTime);
  800. Elapse(TotTime);
  801. end;
  802. procedure TFormClient.BlkInfoBtnClick(Sender: TObject);
  803. begin
  804. GetBlockInfo;
  805. end;
  806. procedure TFormClient.BoTBtnClick(Sender: TObject);
  807. begin
  808. ListBlocksOfType(false);
  809. end;
  810. procedure TFormClient.BtnAsyncReadClick(Sender: TObject);
  811. begin
  812. Read(true);
  813. end;
  814. procedure TFormClient.Label63Click(Sender: TObject);
  815. begin
  816. SmartConnectInfo.ShowModal;
  817. end;
  818. procedure TFormClient.Label64Click(Sender: TObject);
  819. begin
  820. ParamsConnectInfo.ShowModal;
  821. end;
  822. procedure TFormClient.BtnAsyncWriteClick(Sender: TObject);
  823. begin
  824. Write(true);
  825. end;
  826. procedure TFormClient.ClearBlkBuffer;
  827. begin
  828. fillchar(BlkBuffer,SizeOf(BlkBuffer),#0);
  829. BlkBufSize:=0;
  830. DnBtn.Enabled:=false;
  831. AsDnBtn.Enabled:=false;
  832. BlkSaveBtn.Enabled:=false;
  833. EdNewNumber.Enabled:=false;
  834. end;
  835. procedure TFormClient.ClearDBGet;
  836. begin
  837. EdDBNumGet.Text:='1';
  838. LblDBDump.Caption:='DB Dump : 0 bytes';
  839. MemoDB.Lines.Clear;
  840. end;
  841. procedure TFormClient.ClearDirectory;
  842. begin
  843. txtOB.Caption:='0';
  844. txtFB.Caption:='0';
  845. txtFC.Caption:='0';
  846. txtDB.Caption:='0';
  847. txtSFB.Caption:='0';
  848. txtSFC.Caption:='0';
  849. txtSDB.Caption:='0';
  850. cbBot.ItemIndex:=0;
  851. cbBlock.ItemIndex:=0;
  852. EdBlkNum.Text:='1';
  853. ListBot.Items.Clear;
  854. MemoBlk.Lines.Clear;
  855. end;
  856. procedure TFormClient.ClearMultiReadWrite;
  857. begin
  858. fillchar(DataItems,SizeOf(TS7DataItems),#0);
  859. EdData_1.Text:='';EDResult_1.Text:='';
  860. EdData_2.Text:='';EDResult_2.Text:='';
  861. EdData_3.Text:='';EDResult_3.Text:='';
  862. EdData_4.Text:='';EDResult_4.Text:='';
  863. EdData_5.Text:='';EDResult_5.Text:='';
  864. end;
  865. procedure TFormClient.ClearSystemInfo;
  866. begin
  867. EdOrderCode.Text :='INFO NOT AVAILABLE';
  868. EdVersion.Text :='';
  869. EdModuleTypeName.Text :='INFO NOT AVAILABLE';
  870. EdSerialNumber.Text :='INFO NOT AVAILABLE';
  871. EdCopyright.Text :='INFO NOT AVAILABLE';
  872. EdModuleName.Text :='INFO NOT AVAILABLE';
  873. EdASName.Text :='INFO NOT AVAILABLE';
  874. EdPdu.Text :='INFO NOT AVAILABLE';
  875. EdConnections.Text :='INFO NOT AVAILABLE';
  876. EdMpiRate.Text :='INFO NOT AVAILABLE';
  877. EdBusRate.Text :='INFO NOT AVAILABLE';
  878. end;
  879. procedure TFormClient.ClearSZL;
  880. begin
  881. lbSZL.Items.Clear;
  882. MemoSZL.Lines.Clear;
  883. EdID.Text:='$0011';
  884. EdIndex.Text:='$0000';
  885. end;
  886. procedure TFormClient.ClearUpDownload;
  887. begin
  888. cbBlkType.ItemIndex:=0;
  889. EdNum.Text:='1';
  890. lblUpld.Caption:='Block Dump : 0 byte';
  891. MemoUpload.Lines.Clear;
  892. MemoBlkInfo.Lines.Clear;
  893. EdNewNumber.Text:='1';
  894. end;
  895. procedure TFormClient.ClientConnect;
  896. Var
  897. Rack, Slot : integer;
  898. ConnType : word;
  899. RemoteAddress : AnsiString;
  900. LocalTsapHI : integer;
  901. LocalTsapLO : integer;
  902. RemoteTsapHI : integer;
  903. RemoteTsapLO : integer;
  904. LocalTsap : word;
  905. RemoteTsap : word;
  906. PingTime : integer;
  907. function GetChar(ED : TEdit) : integer;
  908. Var
  909. B : byte;
  910. begin
  911. B:=StrToIntDef('$'+Ed.Text,0);
  912. Ed.Text:=IntToHex(B,2);
  913. Result:=B;
  914. end;
  915. begin
  916. LastOP:='Connection';
  917. RemoteAddress:=AnsiString(EdIp.Text);
  918. if not CBPing.Checked then
  919. begin
  920. PingTime:=0;
  921. LastError:=Client.SetParam(p_i32_PingTimeout,@PingTime);
  922. if LastError<>0 then
  923. exit;
  924. end;
  925. if PCC.PageIndex=0 then
  926. begin
  927. ConnType:=CBConnType.ItemIndex+1;
  928. Rack:=StrToIntDef(EdRack.Text,0);
  929. Slot:=StrToIntDef(EdSlot.Text,0);
  930. Client.SetConnectionType(ConnType);
  931. LastError:=Client.ConnectTo(RemoteAddress,Rack,Slot);
  932. end
  933. else begin
  934. LocalTsapHI :=GetChar(EdLocTsapHI);
  935. LocalTsapLO :=GetChar(EdLocTsapLO);
  936. RemoteTsapHI :=GetChar(EdRemTsapHI);
  937. RemoteTsapLO :=GetChar(EdRemTsapLO);
  938. LocalTsap :=LocalTsapHI shl 8 + LocalTsapLO;
  939. RemoteTsap :=RemoteTsapHI shl 8 + RemoteTsapLO;
  940. Client.SetConnectionParams(RemoteAddress, LocalTSAP, RemoteTSAP);
  941. LastError :=Client.Connect;
  942. end;
  943. Elapse;
  944. Connected:=LastError=0;
  945. if Connected then
  946. EdPduSize.Caption:=' '+IntToStr(CliPDULength);
  947. end;
  948. procedure TFormClient.ClientDisconnect;
  949. begin
  950. LastOP:='Disconnection';
  951. Client.Disconnect;
  952. Elapse;
  953. LastError:=0;
  954. Connected:=false;
  955. EdPduSize.Caption:=' 0';
  956. end;
  957. function TFormClient.CliPDULength: integer;
  958. begin
  959. Result:=Client.PDULength;
  960. end;
  961. function TFormClient.CliTime: cardinal;
  962. begin
  963. Result:=Client.Time;
  964. end;
  965. procedure TFormClient.Compress(Async: boolean);
  966. Var
  967. Timeout : integer;
  968. begin
  969. if ChkStatusRefresh.Checked then
  970. begin
  971. ShowMessage('First switch off the Status cyclic refresh');
  972. exit;
  973. end;
  974. Timeout:=StrToIntDef(EdTimeout.Text,0);EdTimeout.Text:=IntToStr(Timeout);
  975. if Timeout<1 then
  976. begin
  977. MessageDlg('Invalid Timeout value', mtError,[mbOk],0);
  978. exit;
  979. end;
  980. if ASync then
  981. LastOp:='Async Compress'
  982. else
  983. LastOp:='Compress';
  984. if ASync then
  985. LastError:=Client.AsCompress(Timeout)
  986. else
  987. LastError:=Client.Compress(Timeout);
  988. if ASync then
  989. WaitCompletion(Timeout);
  990. Elapse;
  991. end;
  992. procedure TFormClient.CopyRamToRom(Async: boolean);
  993. Var
  994. Timeout : integer;
  995. begin
  996. if ChkStatusRefresh.Checked then
  997. begin
  998. ShowMessage('First switch off the Status cyclic refresh');
  999. exit;
  1000. end;
  1001. ShowMessage('Remember that this function works only if the CPU is in STOP');
  1002. Timeout:=StrToIntDef(EdTimeout.Text,0);EdTimeout.Text:=IntToStr(Timeout);
  1003. if Timeout<1 then
  1004. begin
  1005. MessageDlg('Invalid Timeout value', mtError,[mbOk],0);
  1006. exit;
  1007. end;
  1008. if ASync then
  1009. LastOp:='Async Copy Ram to Rom'
  1010. else
  1011. LastOp:='Copy Ram to Rom';
  1012. if ASync then
  1013. LastError:=Client.AsCopyRamToRom(Timeout)
  1014. else
  1015. LastError:=Client.CopyRamToRom(Timeout);
  1016. if ASync then
  1017. WaitCompletion(Timeout);
  1018. Elapse;
  1019. end;
  1020. procedure TFormClient.CbAreaChange(Sender: TObject);
  1021. Var
  1022. Cb : TComboBox;
  1023. begin
  1024. Cb:=TComboBox(Sender);
  1025. if Cb=CbArea then
  1026. begin
  1027. LblDBNum.Visible:=Cb.ItemIndex=0;
  1028. EdDBNum.Visible :=Cb.ItemIndex=0;
  1029. CheckArea;
  1030. end;
  1031. if Cb=CbWLen then
  1032. CheckArea;
  1033. if Cb=ComboArea_1 then
  1034. EdDBNum_1.Visible:=Cb.ItemIndex=0;
  1035. if Cb=ComboArea_2 then
  1036. EdDBNum_2.Visible:=Cb.ItemIndex=0;
  1037. if Cb=ComboArea_3 then
  1038. EdDBNum_3.Visible:=Cb.ItemIndex=0;
  1039. if Cb=ComboArea_4 then
  1040. EdDBNum_4.Visible:=Cb.ItemIndex=0;
  1041. if Cb=ComboArea_5 then
  1042. EdDBNum_5.Visible:=Cb.ItemIndex=0;
  1043. end;
  1044. procedure TFormClient.CbBotCloseUp(Sender: TObject);
  1045. begin
  1046. ListBot.Items.Clear;
  1047. LblDblClick.Visible:=false;
  1048. end;
  1049. procedure TFormClient.CheckArea;
  1050. begin
  1051. LblArea.Visible:=((CbArea.ItemIndex=4) and (cbWLen.ItemIndex<>14)) or
  1052. ((CbArea.ItemIndex=5) and (cbWLen.ItemIndex<>13)) or
  1053. ((CbArea.ItemIndex<>4) and (cbWLen.ItemIndex=14)) or
  1054. ((CbArea.ItemIndex<>5) and (cbWLen.ItemIndex=13));
  1055. end;
  1056. procedure TFormClient.ChkFullClick(Sender: TObject);
  1057. begin
  1058. DnBtn.Visible :=ChkFull.Checked;
  1059. AsDnBtn.Visible :=ChkFull.Checked;
  1060. EdNewNumber.Visible :=ChkFull.Checked;
  1061. lblNewNumber.Visible:=ChkFull.Checked;
  1062. end;
  1063. procedure TFormClient.ChkGetDateTimeClick(Sender: TObject);
  1064. begin
  1065. if ChkGetDateTime.Checked then
  1066. begin
  1067. edAGDate.Color:=clWindow;
  1068. edAGTime.Color:=clWindow;
  1069. grAGDateTime.Enabled:=false;
  1070. end
  1071. else begin
  1072. edAGDate.Color:=clYellow;
  1073. edAGTime.Color:=clYellow;
  1074. grAGDateTime.Enabled:=true;
  1075. end;
  1076. end;
  1077. procedure TFormClient.ChkSecurityClick(Sender: TObject);
  1078. begin
  1079. TimSecurity.Enabled:=ChkSecurity.Checked;
  1080. end;
  1081. procedure TFormClient.ChkStatusRefreshClick(Sender: TObject);
  1082. begin
  1083. BtnStatus.Enabled:=not ChkStatusRefresh.Checked;
  1084. end;
  1085. procedure TFormClient.DataGridDrawCell(Sender: TObject; ACol, ARow: Integer;
  1086. Rect: TRect; State: TGridDrawState);
  1087. Var
  1088. aRect : TRect;
  1089. aText : string;
  1090. Style : TTextStyle;
  1091. begin
  1092. with Sender as TStringGrid do
  1093. begin
  1094. ARect:=Rect;
  1095. AText:=Cells[ACol,ARow];
  1096. if (ACol=0) or (ARow=0) then
  1097. Canvas.Brush.Color:=clbtnface
  1098. else
  1099. Canvas.Brush.Color:=clWhite;
  1100. Canvas.FillRect(Rect);
  1101. Style.Alignment:=taCenter;
  1102. Style.Clipping:=true;
  1103. Style.ExpandTabs:=false;
  1104. Style.Layout:=tlCenter;
  1105. Style.ShowPrefix:=false;
  1106. Style.Wordbreak:=false;
  1107. Style.SystemFont:=false;
  1108. Style.RightToLeft:=false;
  1109. Canvas.TextRect(ARect, 0,0, AText,Style);
  1110. if gdfocused in State then
  1111. begin
  1112. Canvas.Brush.Color:=clRed;
  1113. Canvas.FrameRect(ARect);
  1114. end;
  1115. end;
  1116. end;
  1117. procedure TFormClient.DataGridExit(Sender: TObject);
  1118. begin
  1119. ValidateGrid;
  1120. end;
  1121. procedure TFormClient.DataGridKeyPress(Sender: TObject; var Key: Char);
  1122. begin
  1123. if Key=#13 then
  1124. ValidateGrid;
  1125. end;
  1126. procedure TFormClient.DataToGrid(Amount: integer);
  1127. Var
  1128. x, c, r : integer;
  1129. begin
  1130. with DataGrid do
  1131. begin
  1132. c:=1;r:=1;
  1133. for x := 0 to Amount - 1 do
  1134. begin
  1135. Cells[c,r]:='$'+IntToHex(Buffer[x],2);
  1136. inc(c);
  1137. if c=ColCount then
  1138. begin
  1139. c:=1;
  1140. inc(r);
  1141. end;
  1142. end;
  1143. Row:=1;
  1144. Col:=1;
  1145. SetFocus;
  1146. end;
  1147. end;
  1148. procedure TFormClient.DBFill(ASync: boolean);
  1149. Var
  1150. B : byte;
  1151. DBNum : integer;
  1152. begin
  1153. if ASync then
  1154. LastOp:='Async DB Fill'
  1155. else
  1156. LastOp:='DB Fill';
  1157. B:=StrToIntDef(EdFill.Text,0);
  1158. EdFill.Text:='$'+IntToHex(B,2);
  1159. DBNum:=StrToIntDef(EdDBFill.Text,0);
  1160. EdDBFill.Text:=IntToStr(DBNum);
  1161. if ASync then
  1162. LastError:=Client.AsDBFill(DBNum,B)
  1163. else
  1164. LastError:=Client.DBFill(DBNum,B);
  1165. if LastError=0 then
  1166. begin
  1167. if Async then
  1168. WaitCompletion;
  1169. end;
  1170. Elapse;
  1171. end;
  1172. procedure TFormClient.DBGet(Async: boolean);
  1173. Var
  1174. DBNum : integer;
  1175. Size : integer;
  1176. begin
  1177. if ASync then
  1178. LastOP:='Async DB Get'
  1179. else
  1180. LastOP:='DB Get';
  1181. MemoDB.Lines.Clear;
  1182. LblDBDump.Caption:='DB Dump : 0 bytes';
  1183. DBNum:=StrToIntDef(EdDBNumGet.Text,0);EdDBNumGet.Text:=IntToStr(DBNum);
  1184. Size:=SizeOf(Buffer);
  1185. if Async then
  1186. LastError:=Client.AsDBGet(DBNum,@Buffer,Size)
  1187. else
  1188. LastError:=Client.DBGet(DBNum,@Buffer,Size);
  1189. if LastError=0 then
  1190. begin
  1191. if Async then
  1192. WaitCompletion;
  1193. if LastError=0 then
  1194. begin
  1195. LblDBDump.Caption:='DB Dump : '+IntToStr(Size)+' bytes';
  1196. DumpData(@Buffer,MemoDB,Size);
  1197. end;
  1198. Elapse;
  1199. end
  1200. else
  1201. Elapse;
  1202. end;
  1203. procedure TFormClient.DBGetBtnClick(Sender: TObject);
  1204. begin
  1205. DBGet(false);
  1206. end;
  1207. procedure TFormClient.DeleteBlock;
  1208. Var
  1209. BlockType, BlockNumber : integer;
  1210. begin
  1211. if MessageDlg('Are you sure ?',mtWarning,[mbYes,mbNo],0)<>mrYes then
  1212. exit;
  1213. ClearBlkBuffer;
  1214. LastOp :='Delete Block';
  1215. MemoUpload.Lines.Clear;
  1216. MemoBlkInfo.Lines.Clear;
  1217. BlockType:=BlockOf[cbBlkType.ItemIndex];
  1218. BlockNumber:=StrToIntDef(EdNum.Text,0);EdNum.Text:=IntToStr(BlockNumber);
  1219. LastError:=Client.Delete(BlockType,BlockNumber);
  1220. Elapse;
  1221. end;
  1222. procedure TFormClient.DnBtnClick(Sender: TObject);
  1223. Var
  1224. BlockNum : integer;
  1225. begin
  1226. LastOp:='Download';
  1227. BlockNum:=StrToIntDef(EdNewNumber.Text,0);EdNewNumber.Text:=IntToStr(BlockNum);
  1228. LastError:=Client.Download(BlockNum,@BlkBuffer,BlkBufSize);
  1229. Elapse;
  1230. end;
  1231. procedure TFormClient.DumpData(P : PS7Buffer; Memo: TMemo; Count: integer);
  1232. Var
  1233. SHex, SChr : string;
  1234. Ch : AnsiChar;
  1235. c, cnt : integer;
  1236. begin
  1237. Memo.Lines.Clear;
  1238. Memo.Lines.BeginUpdate;
  1239. SHex:='';SChr:='';cnt:=0;
  1240. try
  1241. for c := 0 to Count - 1 do
  1242. begin
  1243. SHex:=SHex+IntToHex(P^[c],2)+' ';
  1244. Ch:=AnsiChar(P^[c]);
  1245. if not (Ch in ['a'..'z','A'..'Z','0'..'9','_','$','-',#32]) then
  1246. Ch:='.';
  1247. SChr:=SChr+String(Ch);
  1248. inc(cnt);
  1249. if cnt=16 then
  1250. begin
  1251. Memo.Lines.Add(SHex+' '+SChr);
  1252. SHex:='';SChr:='';
  1253. cnt:=0;
  1254. end;
  1255. end;
  1256. // Dump remainder
  1257. if cnt>0 then
  1258. begin
  1259. while Length(SHex)<48 do
  1260. SHex:=SHex+' ';
  1261. Memo.Lines.Add(SHex+' '+SChr);
  1262. end;
  1263. finally
  1264. Memo.Lines.EndUpdate;
  1265. end;
  1266. end;
  1267. procedure TFormClient.EdIDKeyPress(Sender: TObject; var Key: Char);
  1268. begin
  1269. if not (Key in [#8,'0'..'9','$','A','a','B','b','C','c','D','d','E','e','F','f']) then
  1270. Key:=#0;
  1271. end;
  1272. procedure TFormClient.EdRackKeyPress(Sender: TObject; var Key: Char);
  1273. begin
  1274. if not (Key in [#8,'0'..'9']) then
  1275. Key:=#0;
  1276. end;
  1277. procedure TFormClient.Elapse(TotTime: cardinal);
  1278. begin
  1279. StatusBar.Panels[1].Text:=IntToStr(TotTime)+' ms';
  1280. end;
  1281. procedure TFormClient.Elapse;
  1282. begin
  1283. Elapse(CliTime);
  1284. end;
  1285. function TFormClient.CliError(Error: integer): string;
  1286. begin
  1287. Result:=CliErrorText(Error);
  1288. end;
  1289. procedure TFormClient.FillBlkBuffer(p: pointer; Size: integer);
  1290. begin
  1291. move(P^,BlkBuffer,Size);
  1292. BlkBufSize:=Size;
  1293. DnBtn.Enabled:=true;
  1294. AsDnBtn.Enabled:=true;
  1295. EdNewNumber.Enabled:=true;
  1296. BlkSaveBtn.Enabled:=true;
  1297. end;
  1298. procedure TFormClient.FillBlockInfo(Memo: TMemo; Info: PS7BlockInfo);
  1299. function ByteToBin(B : Byte) : string;
  1300. Const
  1301. Mask : array[1..8] of byte = ($80,$40,$20,$10,$08,$04,$02,$01);
  1302. var
  1303. c: Integer;
  1304. begin
  1305. Result:='00000000';
  1306. for c := 8 downto 1 do
  1307. if (B and Mask[c])<>0 then
  1308. Result[c]:='1';
  1309. end;
  1310. begin
  1311. with Memo.Lines do
  1312. begin
  1313. Clear;
  1314. BeginUpdate;
  1315. Add('Block Type : '+SubBlkOf(Info^.BlkType));
  1316. Add('Block Number : '+IntToStr(Info^.BlkNumber));
  1317. Add('Block Lang : '+LangOf(Info^.BlkLang));
  1318. Add('Block Flags : '+ByteToBin(Info^.BlkFlags));
  1319. Add('MC7 Size : '+IntToStr(Info^.MC7Size));
  1320. Add('Load Size : '+IntToStr(Info^.LoadSize));
  1321. Add('Local Data : '+IntToStr(Info^.LocalData));
  1322. Add('SBB Length : '+IntToStr(Info^.SBBLength));
  1323. Add('CheckSum : '+'$'+IntToHex(Info^.CheckSum,4));
  1324. Add('Version : '+IntToHex((Info^.Version and $F0) shr 4,1)+'.'+IntToHex((Info^.Version and $0F),1));
  1325. Add('Code Date : '+Info^.CodeDate);
  1326. Add('Intf.Date : '+Info^.IntfDate);
  1327. Add('Author : '+Info^.Author);
  1328. Add('Family : '+Info^.Family);
  1329. Add('Header : '+Info^.Header);
  1330. EndUpdate;
  1331. end;
  1332. end;
  1333. procedure TFormClient.FillBtnClick(Sender: TObject);
  1334. begin
  1335. DBFill(False);
  1336. end;
  1337. procedure TFormClient.FormCreate(Sender: TObject);
  1338. var
  1339. c: Integer;
  1340. ThePlatform : string;
  1341. Wide : string;
  1342. begin
  1343. // Infamous trick to get the platform size
  1344. // Maybe it could not work ever, but we need only a form caption....
  1345. case SizeOf(NativeUint) of
  1346. 4 : Wide := ' [32 bit]';
  1347. 8 : Wide := ' [64 bit]';
  1348. else Wide := ' [?? bit]';
  1349. end;
  1350. {$IFDEF MSWINDOWS}
  1351. ThePlatform:='Windows platform';
  1352. {$ELSE}
  1353. ThePlatform:='Unix platform';
  1354. CBPing.Visible:=false;
  1355. {$ENDIF}
  1356. Caption:='Snap7 Client Demo - '+ThePlatform+Wide+
  1357. {$IFDEF FPC}
  1358. ' [Lazarus]';
  1359. {$ELSE}
  1360. ' [Delphi/RAD studio]';
  1361. {$ENDIF}
  1362. EvJob:=TEvent.Create(nil,false,false,'');
  1363. Client := TS7Client.Create;
  1364. RGMode.ItemIndex:=0;
  1365. Connected:=false;
  1366. ClearBlkBuffer;
  1367. // Init Grid
  1368. with DataGrid do
  1369. begin
  1370. DefaultColWidth:=32;
  1371. ColWidths[0]:=48;
  1372. DefaultRowHeight:=18;
  1373. ColCount:=17;
  1374. RowCount:=4097;
  1375. for c := 1 to ColCount - 1 do
  1376. Cells[c,0]:=inttohex(c-1,2);
  1377. for c := 1 to RowCount - 1 do
  1378. Cells[0,c]:=inttohex((c-1)*16,4);
  1379. end;
  1380. ValidateGrid;
  1381. WindowState:=wsNormal;
  1382. end;
  1383. procedure TFormClient.Button2Click(Sender: TObject);
  1384. begin
  1385. end;
  1386. procedure TFormClient.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  1387. begin
  1388. Client.Free;
  1389. EvJob.Free;
  1390. end;
  1391. procedure TFormClient.FormDestroy(Sender: TObject);
  1392. begin
  1393. end;
  1394. procedure TFormClient.GridToData(Amount: integer);
  1395. Var
  1396. c, r, x : integer;
  1397. begin
  1398. ValidateGrid;
  1399. with DataGrid do
  1400. begin
  1401. c:=1;r:=1;
  1402. for x := 0 to Amount- 1 do
  1403. begin
  1404. Buffer[x]:=StrToIntDef(Cells[c,r],0);
  1405. inc(c);
  1406. if c=ColCount then
  1407. begin
  1408. c:=1;
  1409. inc(r);
  1410. end;
  1411. end;
  1412. end;
  1413. end;
  1414. procedure TFormClient.lbSZLDblClick(Sender: TObject);
  1415. begin
  1416. if (lbSZL.Items.Count>0) and (lbSZL.ItemIndex>=0) then
  1417. begin
  1418. EdID.Text:=lbSZL.Items[lbSZL.ItemIndex];
  1419. ReadSZL(false);
  1420. end;
  1421. end;
  1422. procedure TFormClient.ListBlocks;
  1423. procedure UpdateCount;
  1424. begin
  1425. with BlocksList do
  1426. begin
  1427. txtOB.Caption :=IntToStr(OBCount);
  1428. txtFB.Caption :=IntToStr(FBCount);
  1429. txtFC.Caption :=IntToStr(FCCount);
  1430. txtSFB.Caption:=IntToStr(SFBCount);
  1431. txtSFC.Caption:=IntToStr(SFCCount);
  1432. txtDB.Caption :=IntToStr(DBCount);
  1433. txtSDB.Caption:=IntToStr(SDBCount);
  1434. end;
  1435. end;
  1436. begin
  1437. LastOP:='Blocks List';
  1438. FillChar(BlocksList,SizeOf(BlocksList),#0);
  1439. UpdateCount;
  1440. LastError:=Client.ListBlocks(@BlocksList);
  1441. if LastError=0 then
  1442. begin
  1443. Elapse;
  1444. if LastError=0 then
  1445. UpdateCount;
  1446. end
  1447. else
  1448. Elapse;
  1449. end;
  1450. procedure TFormClient.ListBlocksOfType(Async: boolean);
  1451. Var
  1452. List : TS7BlocksOfType;
  1453. Count: integer;
  1454. BlockType : integer;
  1455. c: Integer;
  1456. begin
  1457. if Async then
  1458. LastOp:='Async List Blocks of type'
  1459. else
  1460. LastOp:='List Blocks of type';
  1461. BlockType:=BlockOf[CbBot.ItemIndex];
  1462. ListBot.Clear;
  1463. Count:=SizeOf(List) div 2;
  1464. if Async then
  1465. LastError:=Client.AsListBlocksOfType(BlockType,@List,Count)
  1466. else
  1467. LastError:=Client.ListBlocksOfType(BlockType,@List,Count);
  1468. if LastError=0 then
  1469. begin
  1470. if Async then
  1471. WaitCompletion;
  1472. if LastError=0 then
  1473. begin
  1474. ListBot.Items.BeginUpdate;
  1475. try
  1476. for c := 0 to Count - 1 do
  1477. ListBot.Items.Add(IntToStr(List[c]));
  1478. finally
  1479. ListBot.Items.EndUpdate;
  1480. end;
  1481. end;
  1482. end;
  1483. Elapse;
  1484. end;
  1485. procedure TFormClient.ListBotDblClick(Sender: TObject);
  1486. begin
  1487. if (ListBot.Items.Count>0) and (ListBot.ItemIndex>=0) then
  1488. begin
  1489. EdBlkNum.Text:=ListBot.Items[ListBot.ItemIndex];
  1490. CbBlock.ItemIndex:=CbBot.ItemIndex;
  1491. GetBlockInfo;
  1492. end;
  1493. end;
  1494. function TFormClient.LoadFromFile(const FileName: string; P: pointer;
  1495. var Size: integer) : boolean;
  1496. Var
  1497. F : file of byte;
  1498. FSize : integer;
  1499. Read : integer;
  1500. procedure Error;
  1501. begin
  1502. MessageDlg('An error occurred loading '+FileName,mtError,[mbOk],0);
  1503. end;
  1504. begin
  1505. AssignFile(F,FileName);
  1506. {$I-}
  1507. Reset(F);
  1508. {$I+}
  1509. Result:=IoResult=0;
  1510. if not Result then
  1511. begin
  1512. Error;
  1513. exit;
  1514. end;
  1515. {$I-}
  1516. FSize:=FileSize(F);
  1517. BlockRead(F,P^,FSize,Read);
  1518. CloseFile(F);
  1519. {$I+}
  1520. Result:=(IoResult=0) and (Read=FSize);
  1521. if not Result then
  1522. Error
  1523. else
  1524. Size:=FSize;
  1525. end;
  1526. procedure TFormClient.MultiRead;
  1527. procedure GetValues(CbArea : TComboBox; EdDB,EDStart,EDSize : TEdit; var PlcArea,DBNum,Start,Size : integer);
  1528. begin
  1529. DBNum:=StrToIntDef(EdDB.Text,0);
  1530. EdDB.Text:=IntToStr(DBNum);
  1531. Start:=StrToIntDef(EDStart.Text,0);
  1532. EDStart.Text:=IntToStr(Start);
  1533. Size:=StrToIntDef(EdSize.Text,0);
  1534. EdSize.Text:=IntToStr(Size);
  1535. PlcArea:=AreaOf[CbArea.ItemIndex];
  1536. end;
  1537. function HexString(ptr : pbyte; size : integer) : string;
  1538. var
  1539. c: Integer;
  1540. P : PS7Buffer;
  1541. begin
  1542. Result:='';
  1543. P:=PS7Buffer(Ptr);
  1544. for c := 0 to Size - 1 do
  1545. Result:=Result+'$'+IntToHex(P^[c],2)+' ';
  1546. end;
  1547. var
  1548. c: Integer;
  1549. begin
  1550. LastOP:='Read MultiVars';
  1551. ClearMultiReadWrite;
  1552. // Items
  1553. GetValues(ComboArea_1,EdDBNum_1,EdStart_1,EdAmount_1,DataItems[0].Area,DataItems[0].DBNumber,DataItems[0].Start,DataItems[0].Amount);
  1554. GetValues(ComboArea_2,EdDBNum_2,EdStart_2,EdAmount_2,DataItems[1].Area,DataItems[1].DBNumber,DataItems[1].Start,DataItems[1].Amount);
  1555. GetValues(ComboArea_3,EdDBNum_3,EdStart_3,EdAmount_3,DataItems[2].Area,DataItems[2].DBNumber,DataItems[2].Start,DataItems[2].Amount);
  1556. GetValues(ComboArea_4,EdDBNum_4,EdStart_4,EdAmount_4,DataItems[3].Area,DataItems[3].DBNumber,DataItems[3].Start,DataItems[3].Amount);
  1557. GetValues(ComboArea_5,EdDBNum_5,EdStart_5,EdAmount_5,DataItems[4].Area,DataItems[4].DBNumber,DataItems[4].Start,DataItems[4].Amount);
  1558. if (DataItems[0].Amount=0) or
  1559. (DataItems[1].Amount=0) or
  1560. (DataItems[2].Amount=0) or
  1561. (DataItems[3].Amount=0) or
  1562. (DataItems[4].Amount=0) then
  1563. begin
  1564. MessageDlg('Size 0 not allowed',mtError,[mbOk],0);
  1565. exit;
  1566. end;
  1567. // Note: for this demo we assume Wordlen=byte unless Area is Timer or counter.
  1568. // In real application see the documentation
  1569. for c := 0 to 4 do
  1570. if DataItems[c].Area=S7AreaCT then DataItems[c].WordLen:=S7WLCounter else
  1571. if DataItems[c].Area=S7AreaTM then DataItems[c].WordLen:=S7WLTimer else
  1572. DataItems[c].WordLen:=S7WLByte;
  1573. // Calcs the size needed
  1574. for c := 0 to 4 do
  1575. GetMem(DataItems[c].pdata,WordSize(DataItems[c].Amount,DataItems[c].WordLen));
  1576. LastError:=Client.ReadMultiVars(@DataItems,5);
  1577. if LastError=0 then
  1578. begin
  1579. Elapse;
  1580. if LastError=0 then
  1581. begin
  1582. if DataItems[0].Result=0 then
  1583. begin
  1584. EdData_1.Text:=HexString(DataItems[0].pdata,WordSize(DataItems[0].Amount,DataItems[0].WordLen));
  1585. EdResult_1.Text:='OK';
  1586. end
  1587. else
  1588. EdResult_1.Text:=CliError(DataItems[0].Result);
  1589. if DataItems[1].Result=0 then
  1590. begin
  1591. EdData_2.Text:=HexString(DataItems[1].pdata,WordSize(DataItems[1].Amount,DataItems[1].WordLen));
  1592. EdResult_2.Text:='OK';
  1593. end
  1594. else
  1595. EdResult_2.Text:=CliError(DataItems[1].Result);
  1596. if DataItems[2].Result=0 then
  1597. begin
  1598. EdData_3.Text:=HexString(DataItems[2].pdata,WordSize(DataItems[2].Amount,DataItems[2].WordLen));
  1599. EdResult_3.Text:='OK';
  1600. end
  1601. else
  1602. EdResult_3.Text:=CliError(DataItems[2].Result);
  1603. if DataItems[3].Result=0 then
  1604. begin
  1605. EdData_4.Text:=HexString(DataItems[3].pdata,WordSize(DataItems[3].Amount,DataItems[3].WordLen));
  1606. EdResult_4.Text:='OK';
  1607. end
  1608. else
  1609. EdResult_4.Text:=CliError(DataItems[3].Result);
  1610. if DataItems[4].Result=0 then
  1611. begin
  1612. EdData_5.Text:=HexString(DataItems[4].pdata,WordSize(DataItems[4].Amount,DataItems[4].WordLen));
  1613. EdResult_5.Text:='OK';
  1614. end
  1615. else
  1616. EdResult_5.Text:=CliError(DataItems[4].Result);
  1617. end;
  1618. end
  1619. else
  1620. Elapse;
  1621. for c := 0 to 4 do
  1622. FreeMem(DataItems[c].pdata,WordSize(DataItems[c].Amount,DataItems[c].WordLen));
  1623. end;
  1624. procedure TFormClient.MultiReadBtnClick(Sender: TObject);
  1625. begin
  1626. MultiRead;
  1627. end;
  1628. procedure TFormClient.MultiVarReadBtnClick(Sender: TObject);
  1629. begin
  1630. end;
  1631. procedure TFormClient.MultiVarWriteBtnClick(Sender: TObject);
  1632. begin
  1633. end;
  1634. procedure TFormClient.MultiWrite;
  1635. procedure GetValues(CbArea : TComboBox; EdDB,EDStart,EDSize : TEdit; var PlcArea,DBNum,Start,Size : integer);
  1636. begin
  1637. DBNum:=StrToIntDef(EdDB.Text,0);
  1638. EdDB.Text:=IntToStr(DBNum);
  1639. Start:=StrToIntDef(EDStart.Text,0);
  1640. EDStart.Text:=IntToStr(Start);
  1641. Size:=StrToIntDef(EdSize.Text,0);
  1642. EdSize.Text:=IntToStr(Size);
  1643. PlcArea:=AreaOf[CbArea.ItemIndex];
  1644. end;
  1645. procedure EditToBuffer(ChEd: TEdit; p: Pbyte);
  1646. var
  1647. c: Integer;
  1648. pb : PS7Buffer;
  1649. B : byte;
  1650. begin
  1651. B:=StrToIntDef(ChEd.Text,0);
  1652. ChEd.Text:='$'+IntToHex(B,2);
  1653. pb:=PS7Buffer(p);
  1654. for c := 0 to 15 do
  1655. pb^[c]:=B;
  1656. end;
  1657. var
  1658. c: Integer;
  1659. begin
  1660. LastOP:='Write MultiVars';
  1661. fillchar(DataItems,SizeOf(TS7DataItems),#0);
  1662. // Items
  1663. GetValues(ComboArea_1,EdDBNum_1,EdStart_1,EdAmount_1,DataItems[0].Area,DataItems[0].DBNumber,DataItems[0].Start,DataItems[0].Amount);
  1664. GetValues(ComboArea_2,EdDBNum_2,EdStart_2,EdAmount_2,DataItems[1].Area,DataItems[1].DBNumber,DataItems[1].Start,DataItems[1].Amount);
  1665. GetValues(ComboArea_3,EdDBNum_3,EdStart_3,EdAmount_3,DataItems[2].Area,DataItems[2].DBNumber,DataItems[2].Start,DataItems[2].Amount);
  1666. GetValues(ComboArea_4,EdDBNum_4,EdStart_4,EdAmount_4,DataItems[3].Area,DataItems[3].DBNumber,DataItems[3].Start,DataItems[3].Amount);
  1667. GetValues(ComboArea_5,EdDBNum_5,EdStart_5,EdAmount_5,DataItems[4].Area,DataItems[4].DBNumber,DataItems[4].Start,DataItems[4].Amount);
  1668. if (DataItems[0].Amount=0) or
  1669. (DataItems[1].Amount=0) or
  1670. (DataItems[2].Amount=0) or
  1671. (DataItems[3].Amount=0) or
  1672. (DataItems[4].Amount=0) then
  1673. begin
  1674. MessageDlg('Size 0 not allowed',mtError,[mbOk],0);
  1675. exit;
  1676. end;
  1677. // Note: for this demo we assume Wordlen=byte unless Area is Timer or counter.
  1678. // In real application see the documentation
  1679. for c := 0 to 4 do
  1680. if DataItems[c].Area=S7AreaCT then DataItems[c].WordLen:=S7WLCounter else
  1681. if DataItems[c].Area=S7AreaTM then DataItems[c].WordLen:=S7WLTimer else
  1682. DataItems[c].WordLen:=S7WLByte;
  1683. // for simplicity we allocate 1k per item
  1684. for c := 0 to 4 do
  1685. begin
  1686. GetMem(DataItems[c].pdata,1024);
  1687. fillchar(DataItems[c].pdata^,1024,#0);
  1688. end;
  1689. EditToBuffer(ChEd_1,DataItems[0].pdata);
  1690. EditToBuffer(ChEd_2,DataItems[1].pdata);
  1691. EditToBuffer(ChEd_3,DataItems[2].pdata);
  1692. EditToBuffer(ChEd_4,DataItems[3].pdata);
  1693. EditToBuffer(ChEd_5,DataItems[4].pdata);
  1694. LastError:=Client.WriteMultiVars(@DataItems,5);
  1695. if LastError=0 then
  1696. begin
  1697. Elapse;
  1698. if LastError=0 then
  1699. begin
  1700. if DataItems[0].Result=0 then
  1701. EdResult_1.Text:='OK'
  1702. else
  1703. EdResult_1.Text:=CliError(DataItems[0].Result);
  1704. if DataItems[1].Result=0 then
  1705. EdResult_2.Text:='OK'
  1706. else
  1707. EdResult_2.Text:=CliError(DataItems[1].Result);
  1708. if DataItems[2].Result=0 then
  1709. EdResult_3.Text:='OK'
  1710. else
  1711. EdResult_3.Text:=CliError(DataItems[2].Result);
  1712. if DataItems[3].Result=0 then
  1713. EdResult_4.Text:='OK'
  1714. else
  1715. EdResult_4.Text:=CliError(DataItems[3].Result);
  1716. if DataItems[4].Result=0 then
  1717. EdResult_5.Text:='OK'
  1718. else
  1719. EdResult_5.Text:=CliError(DataItems[4].Result);
  1720. end;
  1721. end
  1722. else
  1723. Elapse;
  1724. for c := 0 to 4 do
  1725. FreeMem(DataItems[c].pdata,1024);
  1726. end;
  1727. procedure TFormClient.MultiWriteBtnClick(Sender: TObject);
  1728. begin
  1729. MultiWrite;
  1730. end;
  1731. procedure TFormClient.PageControlChange(Sender: TObject);
  1732. begin
  1733. TimClock.Enabled :=PageControl.ActivePage=TabClock;
  1734. TimStatus.Enabled:=PageControl.ActivePage=TabControl;
  1735. TimSecurity.Enabled:=Pagecontrol.ActivePage=TabSecurity;
  1736. if Pagecontrol.ActivePage=TabSecurity then
  1737. GetProtection;
  1738. if PageControl.ActivePage=TabZSL then
  1739. ReadSZLList(false);
  1740. end;
  1741. procedure TFormClient.Read(Async: boolean);
  1742. Var
  1743. Area : integer;
  1744. DBNum : integer;
  1745. Start : integer;
  1746. Amount : integer;
  1747. WLen : integer;
  1748. begin
  1749. if ASync then
  1750. LastOP:='Async Read Data'
  1751. else
  1752. LastOP:='Read Data';
  1753. Area :=AreaOf[CbArea.ItemIndex];
  1754. DBNum :=StrToIntDef(EdDbNum.Text,0); EdDbNum.Text:=IntToStr(DBNum);
  1755. Start :=StrToIntDef(EdStart.Text,0); EdStart.Text:=IntToStr(Start);
  1756. Amount:=StrToIntDef(EdAmount.Text,0); EdAmount.Text:=IntToStr(Amount);
  1757. WLen :=WLenOf[cbWLen.ItemIndex];
  1758. if Async then
  1759. LastError:=Client.AsReadArea(Area,DBNum,Start,Amount,WLen,@Buffer)
  1760. else
  1761. LastError:=Client.ReadArea(Area,DBNum,Start,Amount,WLen,@Buffer);
  1762. if LastError=0 then
  1763. begin
  1764. if Async then
  1765. WaitCompletion;
  1766. Elapse;
  1767. if LastError=0 then
  1768. DataToGrid(WordSize(Amount,WLen));
  1769. end
  1770. else
  1771. Elapse;
  1772. end;
  1773. procedure TFormClient.ReadSZL(Async: boolean);
  1774. Var
  1775. ID, Index : integer;
  1776. SZL : TS7SZL;
  1777. Size : integer;
  1778. begin
  1779. if ASync then
  1780. LastOP:='Async Read SZL'
  1781. else
  1782. LastOP:='Read SZL';
  1783. MemoSZL.Lines.Clear;
  1784. lblSZLdump.Caption:='SZL Dump : 0 bytes';
  1785. ID:=StrToIntDef(EdID.Text,0);EdID.Text:='$'+IntToHex(ID,4);
  1786. Index:=StrToIntDef(EdIndex.Text,0);EdIndex.Text:='$'+IntToHex(Index,4);
  1787. Size:=SizeOf(SZL);
  1788. if ASync then
  1789. LastError:=Client.AsReadSZL(ID,Index,@SZL, Size)
  1790. else
  1791. LastError:=Client.ReadSZL(ID,Index,@SZL, Size);
  1792. if LastError=0 then
  1793. begin
  1794. if ASync then
  1795. WaitCompletion;
  1796. Elapse;
  1797. if (LastError=0) then
  1798. begin
  1799. DumpData(@SZL,MemoSZL,Size);
  1800. lblSZLdump.Caption:='SZL Dump : '+inttostr(Size)+' bytes';
  1801. end;
  1802. end
  1803. else
  1804. Elapse;
  1805. end;
  1806. procedure TFormClient.RGModeClick(Sender: TObject);
  1807. begin
  1808. AsMode:=RGMode.ItemIndex; // 0 : amPolling
  1809. // 1 : amEvent
  1810. // 2 : amCallBack
  1811. if AsMode =2 then
  1812. Client.SetAsCallback(@ClientCompletion,Self)
  1813. else
  1814. Client.SetAsCallback(nil, nil);
  1815. end;
  1816. procedure TFormClient.SaveToFile(const FileName: string; P: pointer;
  1817. Size: integer);
  1818. Var
  1819. F : File of byte;
  1820. begin
  1821. AssignFile(F, FileName);
  1822. {$I-}
  1823. Rewrite(F);
  1824. BlockWrite(F,P^,Size);
  1825. CloseFile(F);
  1826. {$I+}
  1827. if IoResult<>0 then
  1828. MessageDlg('An error occurred saving '+FileName,mtError,[mbok],0);
  1829. end;
  1830. procedure TFormClient.SetFConnected(const Value: boolean);
  1831. begin
  1832. FConnected := Value;
  1833. if FConnected then
  1834. begin
  1835. BtnConnect.Enabled:=false;
  1836. BtnDisconnect.Enabled:=true;
  1837. PageControl.Enabled:=true;
  1838. PCC.Enabled:=false;
  1839. EdIp.Enabled:=false;
  1840. EdRack.Enabled:=false;
  1841. EdSlot.Enabled:=false;
  1842. if PCC.ActivePageIndex=0 then
  1843. GetSysInfo;
  1844. end
  1845. else begin
  1846. ClearPages;
  1847. BtnConnect.Enabled:=true;
  1848. BtnDisconnect.Enabled:=false;
  1849. PageControl.Enabled:=false;
  1850. PageControl.ActivePageIndex:=0;
  1851. PCC.Enabled:=true;
  1852. EdIp.Enabled:=true;
  1853. edRack.Enabled:=true;
  1854. edSlot.Enabled:=true;
  1855. end;
  1856. end;
  1857. procedure TFormClient.SetFLastError(const Value: integer);
  1858. begin
  1859. FLastError := Value;
  1860. if FLastError=0 then
  1861. StatusBar.Panels[2].Text:='OK'
  1862. else
  1863. StatusBar.Panels[2].Text:=CliError(FLastError);
  1864. end;
  1865. procedure TFormClient.SetFLastOP(const Value: string);
  1866. begin
  1867. FLastOP := Value;
  1868. StatusBar.Panels[0].Text:=FLastOP;
  1869. end;
  1870. procedure TFormClient.SetPassword;
  1871. begin
  1872. LastOp:='Set Session password';
  1873. LastError:=Client.SetSessionPassword(AnsiString(EdPassword.Text));
  1874. Elapse;
  1875. end;
  1876. procedure TFormClient.TimClockTimer(Sender: TObject);
  1877. Var
  1878. DT : TDateTime;
  1879. Begin
  1880. if ChkGetDateTime.Checked then
  1881. begin
  1882. LastOp:='Read PLC Date and Time';
  1883. LastError:=Client.GetPlcDateTime(DT);
  1884. if LastError=0 then
  1885. begin
  1886. edAGDate.Text:=DateToStr(DT);
  1887. edAGTime.Text:=TimeToStr(DT);
  1888. end;
  1889. Elapse;
  1890. end;
  1891. edPGDate.Text:=DateToStr(Now);
  1892. edPGTime.Text:=TimeToStr(Now);
  1893. end;
  1894. procedure TFormClient.TimSecurityTimer(Sender: TObject);
  1895. begin
  1896. if ChkSecurity.Checked then
  1897. GetProtection;
  1898. end;
  1899. procedure TFormClient.TimStatusTimer(Sender: TObject);
  1900. begin
  1901. if ChkStatusRefresh.Checked then
  1902. GetStatus;
  1903. end;
  1904. procedure TFormClient.txtOBDblClick(Sender: TObject);
  1905. Var
  1906. ST : TStaticText;
  1907. begin
  1908. ST:=TStaticText(Sender);
  1909. if StrToIntDef(Trim(ST.Caption),0)=0 then
  1910. exit;
  1911. if ST=txtOB then
  1912. CbBot.ItemIndex:=0;
  1913. if ST=txtFB then
  1914. CbBot.ItemIndex:=1;
  1915. if ST=txtFC then
  1916. CbBot.ItemIndex:=2;
  1917. if ST=txtDB then
  1918. CbBot.ItemIndex:=3;
  1919. if ST=txtSFB then
  1920. CbBot.ItemIndex:=4;
  1921. if ST=txtSFC then
  1922. CbBot.ItemIndex:=5;
  1923. if ST=txtSDB then
  1924. CbBot.ItemIndex:=6;
  1925. ListBlocksOfType(false);
  1926. end;
  1927. procedure TFormClient.Upload(Full, Async: boolean);
  1928. Var
  1929. BlockType, BlockNumber : integer;
  1930. BlockSize : integer;
  1931. begin
  1932. ClearBlkBuffer;
  1933. if Async then
  1934. LastOp :='Async Block Upload'
  1935. else
  1936. LastOp :='Block Upload';
  1937. MemoUpload.Lines.Clear;
  1938. MemoBlkInfo.Lines.Clear;
  1939. BlockType:=BlockOf[cbBlkType.ItemIndex];
  1940. BlockNumber:=StrToIntDef(EdNum.Text,0);EdNum.Text:=IntToStr(BlockNumber);
  1941. BlockSize:=SizeOf(Buffer);
  1942. if Full then
  1943. begin
  1944. if Async then
  1945. LastError:=Client.AsFullUpload(BlockType,BlockNumber,@Buffer,BlockSize)
  1946. else
  1947. LastError:=Client.FullUpload(BlockType,BlockNumber,@Buffer,BlockSize);
  1948. end
  1949. else begin
  1950. if Async then
  1951. LastError:=Client.AsUpload(BlockType,BlockNumber,@Buffer,BlockSize)
  1952. else
  1953. LastError:=Client.Upload(BlockType,BlockNumber,@Buffer,BlockSize);
  1954. end;
  1955. if LastError=0 then
  1956. begin
  1957. if Async then
  1958. WaitCompletion;
  1959. if LastError=0 then
  1960. begin
  1961. DumpData(@Buffer,MemoUpload,BlockSize);
  1962. if Full then
  1963. begin
  1964. Client.GetPgBlockInfo(@Buffer,@BlockInfo,BlockSize);
  1965. FillBlockInfo(MemoBlkInfo,@BlockInfo);
  1966. FillBlkBuffer(@Buffer,BlockSize);
  1967. end
  1968. else
  1969. MemoBlkInfo.Lines.Add('INFO NOT AVAILABLE');
  1970. end;
  1971. end;
  1972. Elapse;
  1973. if LastError=0 then
  1974. lblUpld.Caption:='Block Dump : '+IntToStr(BlockSize)+' byte'
  1975. else
  1976. lblUpld.Caption:='Block Dump : 0 byte';
  1977. end;
  1978. procedure TFormClient.ValidateGrid;
  1979. Var
  1980. r,c : integer;
  1981. function ValidateHexCell(S : string) : string;
  1982. Var
  1983. V : integer;
  1984. begin
  1985. if S='' then
  1986. S:='0';
  1987. V:=StrToIntDef(S,0);
  1988. if V<0 then V:=0;
  1989. if V>255 then V:=255;
  1990. Result:='$'+IntToHex(V,2);
  1991. end;
  1992. begin
  1993. With DataGrid do
  1994. for r:=1 to RowCount - 1 do
  1995. for c := 1 to ColCount - 1 do
  1996. Cells[c,r]:=ValidateHexCell(Cells[c,r])
  1997. end;
  1998. // Call this function when is expect data and size
  1999. procedure TFormClient.WaitCompletion(Const Timeout : integer = 1500);
  2000. Var
  2001. Result : integer;
  2002. begin
  2003. Application.ProcessMessages;
  2004. case AsMode of
  2005. amPolling,
  2006. amCallBack:
  2007. repeat
  2008. Application.ProcessMessages;
  2009. until Client.CheckAsCompletion(Result);
  2010. amEvent : Result:=Client.WaitAsCompletion(Timeout);
  2011. (*
  2012. amCallBack : begin
  2013. // in our callback we setted evJob
  2014. if evJob.WaitFor(Timeout)=wrSignaled then
  2015. Result:=AsOpResult
  2016. else
  2017. Result:=errCliJobTimeout;
  2018. end;
  2019. *)
  2020. end;
  2021. LastError:=Result;
  2022. end;
  2023. // Call this function when don't expect data and size
  2024. function TFormClient.WordSize(Amount, WordLength: integer): integer;
  2025. begin
  2026. case WordLength of
  2027. S7WLBit : Result := Amount * 1; // S7 sends 1 byte per bit
  2028. S7WLByte : Result := Amount * 1;
  2029. S7WLWord : Result := Amount * 2;
  2030. S7WLDword : Result := Amount * 4;
  2031. S7WLReal : Result := Amount * 4;
  2032. S7WLCounter : Result := Amount * 2;
  2033. S7WLTimer : Result := Amount * 2;
  2034. else
  2035. Result:=0;
  2036. end;
  2037. end;
  2038. procedure TFormClient.Write(Async: boolean);
  2039. Var
  2040. Area : integer;
  2041. DBNum : integer;
  2042. Start : integer;
  2043. Amount : integer;
  2044. WLen : integer;
  2045. begin
  2046. if ASync then
  2047. LastOP:='Async Write Data'
  2048. else
  2049. LastOP:='Write Data';
  2050. Area :=AreaOf[CbArea.ItemIndex];
  2051. DBNum :=StrToIntDef(EdDbNum.Text,0);
  2052. Start :=StrToIntDef(EdStart.Text,0);
  2053. Amount:=StrToIntDef(EdAmount.Text,0);
  2054. WLen :=WLenOf[cbWLen.ItemIndex];
  2055. GridToData(Amount*SizeByte[cbWLen.ItemIndex]);
  2056. if Async then
  2057. LastError:=Client.AsWriteArea(Area,DBNum,Start,Amount,WLen,@Buffer)
  2058. else
  2059. LastError:=Client.WriteArea(Area,DBNum,Start,Amount,WLen,@Buffer);
  2060. if LastError=0 then
  2061. begin
  2062. if Async then
  2063. WaitCompletion;
  2064. Elapse;
  2065. end
  2066. else
  2067. Elapse;
  2068. end;
  2069. end.