MainForm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. using Hik.Api;
  2. using Newtonsoft.Json.Linq;
  3. using S7.Net;
  4. using System.Threading.Channels;
  5. using System.Windows.Forms;
  6. namespace xicheji
  7. {
  8. public partial class MainForm : Form
  9. {
  10. private readonly Config config;
  11. private readonly List<WashRecord> washRecords = new();
  12. private DateTime lastReceiveTime = DateTime.MinValue;
  13. private byte[] currentDataBlock;
  14. private Plc? plc = null;
  15. private HikApi? hikApi = null;
  16. public MainForm()
  17. {
  18. InitializeComponent();
  19. config = ConfigHelper.GetConfig();
  20. }
  21. private void MainForm_Load(object sender, EventArgs e)
  22. {
  23. try
  24. {
  25. //测试数据
  26. washRecords.Add(new WashRecord()
  27. {
  28. CarNum = "京123456",
  29. StartTime = new DateTime(2024, 6, 1, 12, 00, 00),
  30. EndTime = new DateTime(2024, 6, 1, 12, 30, 00),
  31. WashTime = 15,
  32. DryTime = 15,
  33. TotalElectricity = 10,
  34. TotalWater = 2,
  35. WaterPressure = 6,
  36. });
  37. dataGridView1.DataSource = washRecords;
  38. ConnectPLC();
  39. //ConnectCamera();
  40. receivePLCTimer.Interval = config.PLCReceiveInterval * 1000;
  41. receivePLCTimer.Enabled = true;
  42. uploadTimer.Enabled = true;
  43. }
  44. catch (Exception ex)
  45. {
  46. MessageBox.Show(ex.Message + ex.StackTrace);
  47. }
  48. }
  49. private void btnConnectCamera_Click(object sender, EventArgs e)
  50. {
  51. try
  52. {
  53. ConnectCamera();
  54. }
  55. catch (Exception ex)
  56. {
  57. MessageBox.Show(ex.Message + ex.StackTrace);
  58. }
  59. }
  60. private void nowTimer_Tick(object sender, EventArgs e)
  61. {
  62. try
  63. {
  64. var now = DateTime.Now;
  65. lblTime.Text = $"{now.Month}月 {now.Day}日 {now.Hour}:{now:mm}:{now:ss}";
  66. }
  67. catch { }
  68. }
  69. private void receivePLCTimer_Tick(object sender, EventArgs e)
  70. {
  71. try
  72. {
  73. if (plc == null || !plc.IsConnected)
  74. {
  75. receivePLCTimer.Enabled = false;
  76. return;
  77. }
  78. ReadPLCData();
  79. }
  80. catch (Exception ex)
  81. {
  82. MessageBox.Show(ex.Message + ex.StackTrace);
  83. }
  84. }
  85. private void ReadPLCData()
  86. {
  87. currentDataBlock = plc!.ReadBytes(DataType.DataBlock, 1, 0, 100);
  88. var time = new DateTime(
  89. DateTime.Now.Year,
  90. ReadPLCInt("当前月"),
  91. ReadPLCInt("当前日"),
  92. ReadPLCInt("当前时"),
  93. ReadPLCInt("当前分"),
  94. ReadPLCInt("当前秒"));
  95. if (time <= lastReceiveTime)
  96. {
  97. //已经读过这个时间的了
  98. return;
  99. }
  100. lastReceiveTime = time;
  101. ReadError();
  102. ReadStatus();
  103. lblWashCount.Text = ReadPLCString("洗车次数");
  104. lblTotalWater.Text = ReadPLCString("累计用水量");
  105. lblTotalElectricity.Text = ReadPLCString("累计用电量");
  106. //.Text = ReadPLCStringValue("当前流量");
  107. lblCurrentElectricity.Text = ReadPLCString("当前功率");
  108. lblCurrentWaterP.Text = ReadPLCString("当前压力");
  109. }
  110. private void ReadStatus()
  111. {
  112. lblStatus.Text = string.Empty;
  113. var statuses = new List<string>() {
  114. "洗车中", "清洗中", "沥水中",
  115. "出车中", "风干中"};
  116. foreach (var status in statuses)
  117. {
  118. if (ReadPLCBool(status))
  119. {
  120. lblStatus.Text = status;
  121. break;
  122. }
  123. }
  124. }
  125. private void ReadError()
  126. {
  127. lblError.Text = string.Empty;
  128. var errors = new List<string>() {
  129. "急停", "冲洗泵故障", "反洗泵故障",
  130. "清淤泵故障", "伴热故障", "风刀故障" };
  131. foreach (var error in errors)
  132. {
  133. if (ReadPLCBool(error))
  134. {
  135. lblError.Text = error;
  136. break;
  137. }
  138. }
  139. }
  140. private string ReadPLCString(string name)
  141. {
  142. var variable = ConfigHelper.FindVariable(name);
  143. if (variable == null)
  144. return string.Empty;
  145. Parse(variable.Key, out _, out _, out var varType, out var address, out var bitNumber);
  146. if (varType == VarType.DWord)
  147. {
  148. var value = S7.Net.Types.DWord.FromByteArray(currentDataBlock.Take(new Range(address, address + 4)).ToArray());
  149. return $"{value} {variable.Desc}";
  150. }
  151. else if (varType == VarType.Word)
  152. {
  153. var value = S7.Net.Types.Word.FromByteArray(currentDataBlock.Take(new Range(address, address + 2)).ToArray());
  154. return $"{value} {variable.Desc}";
  155. }
  156. return string.Empty;
  157. }
  158. private int ReadPLCInt(string name)
  159. {
  160. var variable = ConfigHelper.FindVariable(name);
  161. if (variable == null)
  162. return 0;
  163. Parse(variable.Key, out var dataType, out var dbNumber, out var varType, out var address, out var bitNumber);
  164. if (varType == VarType.DWord)
  165. {
  166. var value = S7.Net.Types.DWord.FromByteArray(currentDataBlock.Take(new Range(address, address + 4)).ToArray());
  167. return (int)value;
  168. }
  169. else if (varType == VarType.Word)
  170. {
  171. var value = S7.Net.Types.Word.FromByteArray(currentDataBlock.Take(new Range(address, address + 2)).ToArray());
  172. return value;
  173. }
  174. return 0;
  175. }
  176. private bool ReadPLCBool(string name)
  177. {
  178. var variable = ConfigHelper.FindVariable(name);
  179. if (variable == null)
  180. return false;
  181. Parse(variable.Key, out _, out _, out _, out var address, out var bitNumber);
  182. var value = S7.Net.Types.Boolean.GetValue(currentDataBlock[address], bitNumber);
  183. return value;
  184. }
  185. private void uploadTimer_Tick(object sender, EventArgs e)
  186. {
  187. }
  188. private void btnSetting_Click(object sender, EventArgs e)
  189. {
  190. try
  191. {
  192. var settingForm = new SettingForm();
  193. if (settingForm.ShowDialog() == DialogResult.OK)
  194. {
  195. ConnectPLC();
  196. ConnectCamera();
  197. receivePLCTimer.Enabled = true;
  198. uploadTimer.Enabled = true;
  199. }
  200. }
  201. catch { }
  202. }
  203. private void ConnectPLC()
  204. {
  205. plc = new Plc(config.PLCType, config.PLCIP, int.Parse(config.PLCPort), 0, 1);
  206. plc.Open();
  207. if (!plc.IsConnected)
  208. {
  209. MessageBox.Show("PLC连接失败!");
  210. return;
  211. }
  212. }
  213. private void ConnectCamera()
  214. {
  215. try
  216. {
  217. hikApi = HikApi.Login(config.CameraIP, int.Parse(config.CameraPort),
  218. config.CameraUserName, config.CameraPassword) as HikApi;
  219. if (hikApi == null || !hikApi.Connected)
  220. {
  221. MessageBox.Show("摄像头连接失败!");
  222. return;
  223. }
  224. hikApi.PlaybackService.StartPlayBack(hikApi.DefaultIpChannel, pictureBox1.Handle);
  225. }
  226. catch (Exception ex)
  227. {
  228. MessageBox.Show(ex.Message + ex.StackTrace);
  229. }
  230. }
  231. public void UpdateCarInfo(DateTime now)
  232. {
  233. this.Invoke(() =>
  234. {
  235. this.Text = now.ToString();
  236. });
  237. }
  238. public static void Parse(string input, out DataType dataType, out int dbNumber, out VarType varType, out int address, out int bitNumber)
  239. {
  240. bitNumber = -1;
  241. dbNumber = 0;
  242. switch (input.Substring(0, 2))
  243. {
  244. case "DB":
  245. string[] strings = input.Split(new char[] { '.' });
  246. if (strings.Length < 2)
  247. throw new InvalidAddressException("To few periods for DB address");
  248. dataType = DataType.DataBlock;
  249. dbNumber = int.Parse(strings[0].Substring(2));
  250. address = int.Parse(strings[1].Substring(3));
  251. string dbType = strings[1].Substring(0, 3);
  252. switch (dbType)
  253. {
  254. case "DBB":
  255. varType = VarType.Byte;
  256. return;
  257. case "DBW":
  258. varType = VarType.Word;
  259. return;
  260. case "DBD":
  261. varType = VarType.DWord;
  262. return;
  263. case "DBX":
  264. bitNumber = int.Parse(strings[2]);
  265. if (bitNumber > 7)
  266. throw new InvalidAddressException("Bit can only be 0-7");
  267. varType = VarType.Bit;
  268. return;
  269. default:
  270. throw new InvalidAddressException();
  271. }
  272. case "IB":
  273. case "EB":
  274. // Input byte
  275. dataType = DataType.Input;
  276. dbNumber = 0;
  277. address = int.Parse(input.Substring(2));
  278. varType = VarType.Byte;
  279. return;
  280. case "IW":
  281. case "EW":
  282. // Input word
  283. dataType = DataType.Input;
  284. dbNumber = 0;
  285. address = int.Parse(input.Substring(2));
  286. varType = VarType.Word;
  287. return;
  288. case "ID":
  289. case "ED":
  290. // Input double-word
  291. dataType = DataType.Input;
  292. dbNumber = 0;
  293. address = int.Parse(input.Substring(2));
  294. varType = VarType.DWord;
  295. return;
  296. case "QB":
  297. case "AB":
  298. case "OB":
  299. // Output byte
  300. dataType = DataType.Output;
  301. dbNumber = 0;
  302. address = int.Parse(input.Substring(2));
  303. varType = VarType.Byte;
  304. return;
  305. case "QW":
  306. case "AW":
  307. case "OW":
  308. // Output word
  309. dataType = DataType.Output;
  310. dbNumber = 0;
  311. address = int.Parse(input.Substring(2));
  312. varType = VarType.Word;
  313. return;
  314. case "QD":
  315. case "AD":
  316. case "OD":
  317. // Output double-word
  318. dataType = DataType.Output;
  319. dbNumber = 0;
  320. address = int.Parse(input.Substring(2));
  321. varType = VarType.DWord;
  322. return;
  323. case "MB":
  324. // Memory byte
  325. dataType = DataType.Memory;
  326. dbNumber = 0;
  327. address = int.Parse(input.Substring(2));
  328. varType = VarType.Byte;
  329. return;
  330. case "MW":
  331. // Memory word
  332. dataType = DataType.Memory;
  333. dbNumber = 0;
  334. address = int.Parse(input.Substring(2));
  335. varType = VarType.Word;
  336. return;
  337. case "MD":
  338. // Memory double-word
  339. dataType = DataType.Memory;
  340. dbNumber = 0;
  341. address = int.Parse(input.Substring(2));
  342. varType = VarType.DWord;
  343. return;
  344. default:
  345. switch (input.Substring(0, 1))
  346. {
  347. case "E":
  348. case "I":
  349. // Input
  350. dataType = DataType.Input;
  351. varType = VarType.Bit;
  352. break;
  353. case "Q":
  354. case "A":
  355. case "O":
  356. // Output
  357. dataType = DataType.Output;
  358. varType = VarType.Bit;
  359. break;
  360. case "M":
  361. // Memory
  362. dataType = DataType.Memory;
  363. varType = VarType.Bit;
  364. break;
  365. case "T":
  366. // Timer
  367. dataType = DataType.Timer;
  368. dbNumber = 0;
  369. address = int.Parse(input.Substring(1));
  370. varType = VarType.Timer;
  371. return;
  372. case "Z":
  373. case "C":
  374. // Counter
  375. dataType = DataType.Counter;
  376. dbNumber = 0;
  377. address = int.Parse(input.Substring(1));
  378. varType = VarType.Counter;
  379. return;
  380. default:
  381. throw new InvalidAddressException(string.Format("{0} is not a valid address", input.Substring(0, 1)));
  382. }
  383. string txt2 = input.Substring(1);
  384. if (txt2.IndexOf(".") == -1)
  385. throw new InvalidAddressException("To few periods for DB address");
  386. address = int.Parse(txt2.Substring(0, txt2.IndexOf(".")));
  387. bitNumber = int.Parse(txt2.Substring(txt2.IndexOf(".") + 1));
  388. if (bitNumber > 7)
  389. throw new InvalidAddressException("Bit can only be 0-7");
  390. return;
  391. }
  392. }
  393. }
  394. }