Form1.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using PLCS7;
  2. using S7.Net;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace PLCConnector
  14. {
  15. public partial class Form1 : Form
  16. {
  17. PLCLinker pl;
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. string ip = "";
  22. short rack = 0;
  23. short slot = 0;
  24. string[] blockIds = new string[3];
  25. try
  26. {
  27. ip = ConfigurationManager.AppSettings.Get("plcIpAddress");
  28. rack = Int16.Parse(ConfigurationManager.AppSettings.Get("plcRack"));
  29. slot = Int16.Parse(ConfigurationManager.AppSettings.Get("plcSlot"));
  30. blockIds = (ConfigurationManager.AppSettings.Get("plcDatablockId")).Split(',');
  31. pl = new PLCLinker(CpuType.S71500, ip, rack, slot, int.Parse(blockIds[0]), int.Parse(blockIds[1]), int.Parse(blockIds[2]), 6, 200);
  32. }
  33. catch (Exception e) { MessageBox.Show("配置文件异常"); }
  34. }
  35. public static void parkingSpaceDBTest(AbstractPLCLinker pl, Random rnd)
  36. {
  37. if (!pl.isConnected) { pl.PLCConnect(); }
  38. else
  39. {
  40. //读车位表信息
  41. List<object> psList = pl.ReadFromPLC(PLCDataType.parkingSpace, 1);
  42. psList.AddRange(pl.ReadFromPLC(PLCDataType.parkingSpace, 2));
  43. foreach (var ps in psList)
  44. {
  45. Console.WriteLine(((ParkingSpaceStru)ps).ToString());
  46. }
  47. //写入车位状态信息
  48. ParkingSpaceStru pss = new ParkingSpaceStru
  49. {
  50. parkingSpace = (short)rnd.Next(1, 3),
  51. spaceStatus = (short)rnd.Next(0, 3)
  52. };
  53. pl.WriteToPLC(pss, PLCDataType.central);
  54. Console.WriteLine("写入状态值");
  55. }
  56. }
  57. public static void terminalDBTest(AbstractPLCLinker pl, Random rnd)
  58. {
  59. if (!pl.isConnected) { pl.PLCConnect(); }
  60. else
  61. {
  62. //读终端块数据
  63. List<object> tList = pl.ReadFromPLC(PLCDataType.terminal, 1);
  64. foreach (var term in tList)
  65. {
  66. Console.WriteLine(((TerminalStru)term).ToString());
  67. }
  68. //模拟中控写数据
  69. TerminalStru tsCentral = new TerminalStru
  70. {
  71. terminalID = 1,
  72. paymentStatus = (short)rnd.Next(100, 999),
  73. licVerification = -1,
  74. parkingFee = (short)rnd.Next(100, 999),
  75. userType = (short)rnd.Next(100, 999)
  76. };
  77. //模拟终端写数据
  78. TerminalStru tsTerminal = new TerminalStru
  79. {
  80. terminalID = 1,
  81. terminalStatus = (short)rnd.Next(100, 999),
  82. btnStatus = (short)rnd.Next(100, 999),
  83. licenseCodeA = (short)rnd.Next(100, 999),
  84. licenseCodeB = -1,
  85. licenseCodeC = (short)rnd.Next(100, 999),
  86. receiptNum = (short)rnd.Next(100, 999)
  87. };
  88. //pl.WriteToPLC(tsCentral, PLCDataType.central);
  89. pl.WriteToPLC(tsTerminal, PLCDataType.terminal);
  90. }
  91. }
  92. private void button1_Click(object sender, EventArgs e)
  93. {
  94. PLCDataType type = PLCDataType.parkingSpace;
  95. int offset = 0;
  96. int value = 0;
  97. try
  98. {
  99. offset = Int32.Parse(textBox2.Text);
  100. value = Int32.Parse(textBox3.Text);
  101. }
  102. catch { MessageBox.Show("异常"); return; }
  103. switch (textBox4.Text)
  104. {
  105. case "中控":
  106. type = PLCDataType.central;
  107. break;
  108. case "终端1":
  109. type = PLCDataType.terminal;
  110. break;
  111. case "终端2":
  112. type = PLCDataType.terminal;
  113. offset += 42;
  114. break;
  115. case "终端3":
  116. type = PLCDataType.terminal;
  117. offset += 42*2;
  118. break;
  119. case "车位1":
  120. type = PLCDataType.parkingSpace;
  121. break;
  122. }
  123. if (value < 32768)
  124. {
  125. pl.WriteAccordingToOffset(type, offset, (short)value);
  126. }
  127. else
  128. {
  129. pl.WriteAccordingToOffset(type, offset, value);
  130. }
  131. }
  132. private void button2_Click(object sender, EventArgs e)
  133. {
  134. PLCDataType type = PLCDataType.parkingSpace;
  135. int id = -1;
  136. switch ((string)textBox5.Text)
  137. {
  138. case "中控":
  139. type = PLCDataType.central;id = 0;
  140. break;
  141. case "终端1":
  142. type = PLCDataType.terminal; id = 1;
  143. break;
  144. case "终端2":
  145. type = PLCDataType.terminal; id = 2;
  146. break;
  147. case "终端3":
  148. type = PLCDataType.terminal; id = 3;
  149. break;
  150. case "车位1":
  151. type = PLCDataType.parkingSpace; id = 4;
  152. break;
  153. }
  154. string result = "";
  155. try
  156. {
  157. if (id == 4)
  158. {
  159. List<object> list = pl.ReadFromPLC(type, 1);
  160. result = ((ParkingSpaceStru)list[0]).ToString();
  161. }
  162. else if (id > 0)
  163. {
  164. List<object> list = pl.ReadFromPLC(type, id);
  165. result = ((TerminalStru)list[0]).ToString();
  166. }
  167. else if (id == 0)
  168. {
  169. List<object> list = pl.ReadFromPLC(type, id);
  170. result = ((MainBlockStru)list[0]).ToString();
  171. }
  172. }
  173. catch { Console.WriteLine("异常"); return; }
  174. textBox1.Text = result.Replace(",","\r\n");
  175. }
  176. private void button3_Click(object sender, EventArgs e)
  177. {
  178. PLCDataType type = PLCDataType.parkingSpace;
  179. int id = -1;
  180. switch ((string)textBox6.Text)
  181. {
  182. case "中控":
  183. type = PLCDataType.central; id = 0;
  184. break;
  185. case "终端1":
  186. type = PLCDataType.terminal; id = 1;
  187. break;
  188. case "终端2":
  189. type = PLCDataType.terminal; id = 2;
  190. break;
  191. case "终端3":
  192. type = PLCDataType.terminal; id = 3;
  193. break;
  194. case "车位1":
  195. type = PLCDataType.parkingSpace; id = 4;
  196. break;
  197. }
  198. string result = "";
  199. try
  200. {
  201. if (id == 4)
  202. {
  203. List<object> list = pl.ReadFromPLC(type, 1);
  204. result = ((ParkingSpaceStru)list[0]).ToString();
  205. }
  206. else if (id > 0)
  207. {
  208. List<object> list = pl.ReadFromPLC(type, id);
  209. result = ((TerminalStru)list[0]).ToString();
  210. }
  211. else if (id == 0)
  212. {
  213. List<object> list = pl.ReadFromPLC(type, id);
  214. result = ((MainBlockStru)list[0]).ToString();
  215. }
  216. }
  217. catch { Console.WriteLine("异常"); return; }
  218. textBox7.Text = result.Replace(",", "\r\n");
  219. }
  220. private void button4_Click(object sender, EventArgs e)
  221. {
  222. PLCDataType type = PLCDataType.parkingSpace;
  223. int id = -1;
  224. switch ((string)textBox8.Text)
  225. {
  226. case "中控":
  227. type = PLCDataType.central; id = 0;
  228. break;
  229. case "终端1":
  230. type = PLCDataType.terminal; id = 1;
  231. break;
  232. case "终端2":
  233. type = PLCDataType.terminal; id = 2;
  234. break;
  235. case "终端3":
  236. type = PLCDataType.terminal; id = 3;
  237. break;
  238. case "车位1":
  239. type = PLCDataType.parkingSpace; id = 4;
  240. break;
  241. }
  242. string result = "";
  243. try
  244. {
  245. if (id == 4)
  246. {
  247. List<object> list = pl.ReadFromPLC(type, 1);
  248. result = ((ParkingSpaceStru)list[0]).ToString();
  249. }
  250. else if (id > 0)
  251. {
  252. List<object> list = pl.ReadFromPLC(type, id);
  253. result = ((TerminalStru)list[0]).ToString();
  254. }
  255. else if (id == 0)
  256. {
  257. List<object> list = pl.ReadFromPLC(type, id);
  258. result = ((MainBlockStru)list[0]).ToString();
  259. }
  260. }
  261. catch { Console.WriteLine("异常"); return; }
  262. textBox9.Text = result.Replace(",", "\r\n");
  263. }
  264. private void btn_refresh_Click(object sender, EventArgs e)
  265. {
  266. Timer timer1 = new Timer();
  267. timer1.Interval = 1000;
  268. timer1.Tick += new EventHandler(button2_Click);
  269. timer1.Start();
  270. Timer timer2 = new Timer();
  271. timer2.Interval = 1000;
  272. timer2.Tick += new EventHandler(button3_Click);
  273. timer2.Start();
  274. Timer timer3 = new Timer();
  275. timer3.Interval = 1000;
  276. timer3.Tick += new EventHandler(button4_Click);
  277. timer3.Start();
  278. }
  279. private void btn_clear_Click(object sender, EventArgs e)
  280. {
  281. TerminalStru tsFromCentral = new TerminalStru
  282. {
  283. terminalID = 1,
  284. parkingFee = (short)32767,
  285. paymentStatus = (short)0,
  286. licVerification =(short)0,
  287. userType = (short)0,
  288. };
  289. TerminalStru tsFromTerminal = new TerminalStru
  290. {
  291. terminalID = 1,
  292. btnStatus = -1,
  293. cmd = (short)0,
  294. receiptNum = (short)0,
  295. };
  296. pl.WriteToPLC(tsFromCentral, PLCDataType.central);
  297. pl.WriteToPLC(tsFromTerminal, PLCDataType.terminal);
  298. }
  299. /// <summary>
  300. /// 凭证号写入车位
  301. /// </summary>
  302. /// <param name="sender"></param>
  303. /// <param name="e"></param>
  304. private void button5_Click(object sender, EventArgs e)
  305. {
  306. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  307. int receipt = ((TerminalStru)list[0]).receiptNum;
  308. pl.WriteAccordingToOffset(PLCDataType.parkingSpace, 14, receipt);
  309. }
  310. /// <summary>
  311. /// 启动地感
  312. /// </summary>
  313. /// <param name="sender"></param>
  314. /// <param name="e"></param>
  315. private void button7_Click(object sender, EventArgs e)
  316. {
  317. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  318. short ground = ((TerminalStru)list[0]).groundStatus;
  319. if (ground == (short)1)
  320. {
  321. pl.WriteAccordingToOffset(PLCDataType.terminal, 36, (short)0);
  322. }
  323. else
  324. {
  325. pl.WriteAccordingToOffset(PLCDataType.terminal, 36, (short)1);
  326. }
  327. }
  328. /// <summary>
  329. /// 启动号牌机
  330. /// </summary>
  331. /// <param name="sender"></param>
  332. /// <param name="e"></param>
  333. private void button8_Click(object sender, EventArgs e)
  334. {
  335. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)1);
  336. }
  337. /// <summary>
  338. /// 清空中控db
  339. /// </summary>
  340. /// <param name="sender"></param>
  341. /// <param name="e"></param>
  342. private void button9_Click(object sender, EventArgs e)
  343. {
  344. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  345. short complete = ((MainBlockStru)list[0]).processCompleted;
  346. if (complete == (short)0)
  347. {
  348. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  349. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)1);
  350. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  351. }
  352. else
  353. {
  354. pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0);
  355. pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)0);
  356. pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0);
  357. }
  358. }
  359. /// <summary>
  360. /// 切换中控停取模式
  361. /// </summary>
  362. /// <param name="sender"></param>
  363. /// <param name="e"></param>
  364. private void button10_Click(object sender, EventArgs e)
  365. {
  366. List<object> list = pl.ReadFromPLC(PLCDataType.central, 0);
  367. short park = ((MainBlockStru)list[0]).parkingRunning;
  368. if(park == (short)0)
  369. {
  370. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)1);
  371. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)0);
  372. }
  373. else
  374. {
  375. pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)0);
  376. pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)1);
  377. }
  378. }
  379. /// <summary>
  380. /// 切换终端停取模式
  381. /// </summary>
  382. /// <param name="sender"></param>
  383. /// <param name="e"></param>
  384. private void button11_Click(object sender, EventArgs e)
  385. {
  386. List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
  387. short termStatus = ((TerminalStru)list[0]).terminalStatus;
  388. if (termStatus == (short)1)
  389. {
  390. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)2);
  391. }
  392. else
  393. {
  394. pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)1);
  395. }
  396. }
  397. }
  398. }