FrmMelsecA1EAscii.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using Curtain.DataAccess;
  7. using Curtain.Framework.Utils;
  8. using Curtain.Framework.Windows;
  9. using Curtain.WCF.Proxy;
  10. using HslCommunication.Core.Net;
  11. using HslCommunication.Enthernet;
  12. using PLCCommunication.Proxy;
  13. namespace PCLCommunication
  14. {
  15. public partial class FrmMelsecA1EAscii : Form
  16. {
  17. private string FormText = "PLC通信-";
  18. public FrmMelsecA1EAscii()
  19. {
  20. InitializeComponent();
  21. dgvSetting.AutoGenerateColumns = false;
  22. }
  23. private void FrmMelsecA1EAscii_Shown(object sender, EventArgs e)
  24. {
  25. try
  26. {
  27. ConnSetting.Init();
  28. //if (!this.chkAutoPiece.Checked)
  29. //{
  30. // ConnSetting.MESInfo = null;
  31. //}
  32. bool autoStart = false;
  33. if (string.Compare(ConnSetting.PLCServiceSettings[0], "true", true) == 0)
  34. {
  35. autoStart = true;
  36. }
  37. this.txtServerPort.Text = ConnSetting.PLCServiceSettings[1];
  38. this.Text = FormText + ConnSetting.PLCServiceSettings[1];
  39. this.dgvSetting.DataSource = ConnSetting.T_PLC;
  40. this.lblUsercode.Text = ConnSetting.MESInfo.UserCode;
  41. if (autoStart)
  42. {
  43. Start(false);
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. // todo log
  49. LogOut.Error(null, ex);
  50. MessageBox.Show("系统异常");
  51. }
  52. }
  53. private void dgvSetting_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
  54. {
  55. //if (e.RowIndex < 0)
  56. //{
  57. // return;
  58. //}
  59. //string ename = this.dgvSetting.Rows[e.RowIndex].Cells["enable"].Value.ToString();
  60. //if (ename == "-1")
  61. //{
  62. // (this.dgvSetting.Rows[e.RowIndex].Cells["ss"] as DataGridViewButtonCell).
  63. //}
  64. }
  65. private void btnDelete_Click(object sender, EventArgs e)
  66. {
  67. if (this.dgvSetting.CurrentRow == null)
  68. {
  69. return;
  70. }
  71. try
  72. {
  73. DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
  74. string guid = item["GUID"].ToString();
  75. ConnSetting.DeletePLC(guid);
  76. item.Delete();
  77. ConnSetting.T_PLC.AcceptChanges();
  78. }
  79. catch (Exception ex)
  80. {
  81. // todo log
  82. LogOut.Error(null, ex);
  83. MessageBox.Show("系统异常");
  84. }
  85. }
  86. private void btnAdd_Click(object sender, EventArgs e)
  87. {
  88. }
  89. private void btnEdit_Click(object sender, EventArgs e)
  90. {
  91. if (this.dgvSetting.CurrentRow == null)
  92. {
  93. return;
  94. }
  95. try
  96. {
  97. DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
  98. FrmPLC plc = new FrmPLC();
  99. plc.Item = item.Row;
  100. if (plc.ShowDialog() == DialogResult.OK)
  101. {
  102. ConnSetting.EditPLC(item.Row);
  103. ConnSetting.T_PLC.AcceptChanges();
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. //ConnSetting.T_PLC.Reset();
  109. // todo log
  110. LogOut.Error(null, ex);
  111. MessageBox.Show("系统异常");
  112. }
  113. }
  114. private void dgvSetting_CellClick(object sender, DataGridViewCellEventArgs e)
  115. {
  116. if (e.RowIndex < 0)
  117. {
  118. return;
  119. }
  120. string colName = dgvSetting.Columns[e.ColumnIndex].Name;
  121. if (colName != "ss")
  122. {
  123. return;
  124. }
  125. try
  126. {
  127. string ename = this.dgvSetting.Rows[e.RowIndex].Cells["enable"].Value.ToString();
  128. if (ename == "-1")
  129. {
  130. return;
  131. }
  132. string ssname = this.dgvSetting.Rows[e.RowIndex].Cells["ss"].Value.ToString();
  133. string guid = this.dgvSetting.Rows[e.RowIndex].Cells["guid"].Value.ToString();
  134. if (ssname == "启用")
  135. {
  136. LogOut.Debug(null, "StartPLC:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
  137. ConnSetting.StartPLC(guid);
  138. DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
  139. item["Status"] = "1";
  140. item["StatusName"] = "已启动";
  141. item["ss"] = "停止";
  142. item.EndEdit();
  143. }
  144. else
  145. {
  146. ConnSetting.StopPLC(guid);
  147. LogOut.Debug(null, "StopPLC:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
  148. DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
  149. item["Status"] = "0";
  150. item["StatusName"] = "未启动";
  151. item["ss"] = "启用";
  152. item.EndEdit();
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. // todo log
  158. LogOut.Error(null, ex);
  159. MessageBox.Show("系统异常");
  160. }
  161. }
  162. private void FrmMelsecA1EAscii_FormClosing(object sender, FormClosingEventArgs e)
  163. {
  164. //DialogResult dr = MessageBox.Show("确认退出?\r\n退出后机械施釉臂将无法读取产品型号。", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  165. DialogResult dr = MessageBox.Show("确认退出?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
  166. if (dr == DialogResult.OK)
  167. {
  168. dr = MessageBox.Show("确认退出?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
  169. if (dr == DialogResult.OK)
  170. {
  171. ConnSetting.Dispose();
  172. return;
  173. }
  174. }
  175. e.Cancel = true;
  176. }
  177. private void btnTest_Click(object sender, EventArgs e)
  178. {
  179. try
  180. {
  181. FrmTest ft = new FrmTest();
  182. ft.Show();
  183. }
  184. catch (Exception ex)
  185. {
  186. // todo log
  187. LogOut.Error(null, ex);
  188. MessageBox.Show("系统异常");
  189. }
  190. }
  191. private void btnStart_Click(object sender, EventArgs e)
  192. {
  193. Start();
  194. }
  195. private NetSimplifyServer simplifyServer;
  196. private FrmLog _FrmLog = null;
  197. private void Start(bool showFrmLog=true)
  198. {
  199. if (simplifyServer != null)
  200. {
  201. simplifyServer.ServerClose();
  202. //simplifyServer.LogNet.Dispose();
  203. // 释放log文件
  204. System.Threading.Thread.Sleep(300);
  205. }
  206. try
  207. {
  208. simplifyServer = new NetSimplifyServer();
  209. simplifyServer.LogNet = new HslCommunication.LogNet.LogNetSingle(Application.StartupPath + @"\ServerLogs\log.txt");
  210. simplifyServer.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile;
  211. //simplifyServer.Token = new Guid(textBox3.Text);
  212. //simplifyServer.ReceiveStringEvent += SimplifyServer_ReceiveStringEvent;
  213. simplifyServer.ReceivedBytesEvent += SimplifyServer_ReceivedBytesEvent;
  214. //ConnSetting.AutoRun();
  215. simplifyServer.ServerStart(int.Parse(txtServerPort.Text.Trim()));
  216. ConnSetting.PLCServiceSettings[1] = txtServerPort.Text.Trim();
  217. this.Text = FormText + ConnSetting.PLCServiceSettings[1];
  218. ConnSetting.SavePLCServicePort();
  219. lbls.Text = "启动";
  220. if (showFrmLog)
  221. {
  222. if (_FrmLog != null)
  223. {
  224. _FrmLog.Close();
  225. _FrmLog.Dispose();
  226. }
  227. _FrmLog = new FrmLog();
  228. _FrmLog.Text = this.Text;
  229. _FrmLog.Show();
  230. }
  231. }
  232. catch (Exception ex)
  233. {
  234. MessageBox.Show("启动失败:" + ex.Message);
  235. }
  236. }
  237. // 1楼 5001
  238. //public const int PLC_Barcode_Flag = 330;
  239. //public const int PLC_Error_Flag = 331;
  240. //public const int PLC_GoodsCode_Length = 350;
  241. //public const int PLC_Barcode = 341;
  242. //public const int PLC_GoodsCode = 351;
  243. //// 2楼 5002
  244. //public const int PLC_Barcode_Flag2 = 510;
  245. //public const int PLC_Error_Flag2 = 511;
  246. //public const int PLC_GoodsCode_Length2 = 513;
  247. //public const int PLC_Barcode2 = 515;
  248. //public const int PLC_GoodsCode2 = 525;
  249. private void SimplifyServer_ReceivedBytesEvent(AppSession arg1, HslCommunication.NetHandle arg2, byte[] arg3)
  250. {
  251. StringBuilder lbl = new StringBuilder();
  252. MelsecA1EAscii m = null;
  253. bool hasDebugLog = true;
  254. bool result = false;
  255. int flag = 0;
  256. //int ff = (int)arg2;
  257. //string str = Encoding.ASCII.GetString(arg3);
  258. //MessageBox.Show(str, "c:" + ff.ToString());
  259. //return;
  260. lock (this)
  261. {
  262. try
  263. {
  264. //throw new NotImplementedException();
  265. flag = (int)arg2;
  266. #region 1 识别型号
  267. if (flag == 1)
  268. {
  269. string str = Encoding.ASCII.GetString(arg3);
  270. // 替换工号
  271. if (str.StartsWith("#"))
  272. {
  273. LogNet_BeforeSaveToFile("-----------------------------------------------");
  274. LogNet_BeforeSaveToFile("接收的工号为:" + str);
  275. lbl.AppendLine("接收的工号为:" + str);
  276. m = ConnSetting.PLC_ConnServer[flag.ToString()];
  277. try
  278. {
  279. m.Always = true;
  280. //m.ConnectSocket();
  281. }
  282. catch (Exception ex)
  283. {
  284. LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
  285. lbl.AppendLine("PLC连接失败 : " + ex.Message);
  286. return;
  287. }
  288. string usercode = str.Replace("#", "");
  289. if (string.IsNullOrWhiteSpace(usercode))
  290. {
  291. ConnSetting.SetPLCError(m, lbl, "接收的工号为空");
  292. simplifyServer.SendMessage(arg1, "0000");
  293. return;
  294. }
  295. string id = ConnSetting.CheckUsercode(usercode);
  296. usercode = ConnSetting.MESInfo.UserCode;
  297. //int l = usercode.Length;
  298. //LogNet_BeforeSaveToFile("写入 D350 当前工号长度 " + l);
  299. //lbl.AppendLine("写入 D350 当前工号长度 " + l);
  300. //// 写入 D350 当前工号长度
  301. ////result = m.SetData("D", PLC_GoodsCode_Length, 1, l.ToString("0000"));
  302. //result = m.SetData("D", 350, 1, l.ToString("X4"));
  303. //LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  304. //lbl.AppendLine(JsonUtil.FromObject(m));
  305. //if (!result)
  306. //{
  307. // LogNet_BeforeSaveToFile("PLC操作失败");
  308. // lbl.AppendLine("PLC操作失败");
  309. // return;
  310. //}
  311. LogNet_BeforeSaveToFile("写入 D351 当前工号 : " + usercode);
  312. lbl.AppendLine("写入 D351 当前工号 : " + usercode);
  313. // 写入 D351 当前工号
  314. byte[] byteCode = Encoding.ASCII.GetBytes(usercode);
  315. result = m.SetData("D", 351, 5, byteCode);
  316. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  317. lbl.AppendLine(JsonUtil.FromObject(m));
  318. if (!result)
  319. {
  320. LogNet_BeforeSaveToFile("PLC操作失败");
  321. lbl.AppendLine("PLC操作失败");
  322. return;
  323. }
  324. if (string.IsNullOrWhiteSpace(id))
  325. {
  326. ConnSetting.SetPLCError(m, lbl, "工号不存在或不在此工序");
  327. simplifyServer.SendMessage(arg1, "0000");
  328. return;
  329. }
  330. else
  331. {
  332. LogNet_BeforeSaveToFile("写入 D330 标识 1 ");
  333. lbl.AppendLine("写入 D330 标识 1 ");
  334. // 写入 D330 标识 1
  335. result = m.SetData("D", 330, 1, "0001");
  336. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  337. lbl.AppendLine(JsonUtil.FromObject(m));
  338. if (!result)
  339. {
  340. LogNet_BeforeSaveToFile("PLC操作失败");
  341. lbl.AppendLine("PLC操作失败");
  342. return;
  343. }
  344. }
  345. simplifyServer.SendMessage(arg1, "0100");
  346. return;
  347. }
  348. string barcode = str;
  349. LogNet_BeforeSaveToFile("-----------------------------------------------");
  350. LogNet_BeforeSaveToFile("接收的条码为:" + barcode);
  351. lbl.AppendLine("接收的条码为:" + barcode);
  352. m = ConnSetting.PLC_ConnServer[flag.ToString()];
  353. try
  354. {
  355. m.Always = true;
  356. //m.ConnectSocket();
  357. }
  358. catch (Exception ex)
  359. {
  360. LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
  361. lbl.AppendLine("PLC连接失败 : " + ex.Message);
  362. return;
  363. }
  364. if (string.IsNullOrEmpty(barcode) || "0" == barcode)
  365. {
  366. ConnSetting.SetPLCError(m, lbl, "接收的条码为空");
  367. simplifyServer.SendMessage(arg1, "0000");
  368. return;
  369. }
  370. LogNet_BeforeSaveToFile("写入 D341 条码 : " + barcode);
  371. lbl.AppendLine("写入 D341 条码 : " + barcode);
  372. // 写入 D341 条码
  373. byte[] bytebarcode = Encoding.ASCII.GetBytes(barcode);
  374. result = m.SetData("D", 341, 6, bytebarcode);
  375. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  376. lbl.AppendLine(JsonUtil.FromObject(m));
  377. if (!result)
  378. {
  379. LogNet_BeforeSaveToFile("PLC操作失败");
  380. lbl.AppendLine("PLC操作失败");
  381. return;
  382. }
  383. LogNet_BeforeSaveToFile("查询 产品编码 : ");
  384. lbl.Append("查询 产品编码 : ");
  385. // 查询 产品编码
  386. string code = ConnSetting.GetGoodscode(barcode);
  387. LogNet_BeforeSaveToFile(code);
  388. lbl.AppendLine(code);
  389. if (string.IsNullOrEmpty(code))
  390. {
  391. LogNet_BeforeSaveToFile("写入 D332 产品型号代码 0");
  392. lbl.AppendLine("写入 D332 产品型号代码 0");
  393. // 写入 D332 产品型号代码
  394. result = m.SetData("D", 332, 1, "0000");
  395. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  396. lbl.AppendLine(JsonUtil.FromObject(m));
  397. if (!result)
  398. {
  399. LogNet_BeforeSaveToFile("PLC操作失败");
  400. lbl.AppendLine("PLC操作失败");
  401. return;
  402. }
  403. /*
  404. LogNet_BeforeSaveToFile("写入 D351 产品型号");
  405. lbl.AppendLine("写入 D351 产品型号");
  406. result = m.SetData("D", PLC_GoodsCode, 10, "0000000000000000000000000000000000000000");
  407. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  408. lbl.AppendLine(JsonUtil.FromObject(m));
  409. if (!result)
  410. {
  411. LogNet_BeforeSaveToFile("PLC操作失败");
  412. lbl.AppendLine("PLC操作失败");
  413. return;
  414. }
  415. */
  416. ConnSetting.SetPLCError(m, lbl, "产品型号为空");
  417. simplifyServer.SendMessage(arg1, "0000");
  418. }
  419. else
  420. {
  421. //int l = code.Length;
  422. int l = int.Parse(code);
  423. LogNet_BeforeSaveToFile("写入 D332 产品型号代码 " + l);
  424. lbl.AppendLine("写入 D332 产品型号代码 " + l);
  425. // 写入 D332 产品型号代码
  426. //result = m.SetData("D", PLC_GoodsCode_Length, 1, l.ToString("0000"));
  427. result = m.SetData("D", 332, 1, l.ToString("X4"));
  428. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  429. lbl.AppendLine(JsonUtil.FromObject(m));
  430. if (!result)
  431. {
  432. LogNet_BeforeSaveToFile("PLC操作失败");
  433. lbl.AppendLine("PLC操作失败");
  434. return;
  435. }
  436. /*
  437. LogNet_BeforeSaveToFile("写入 D351 产品型号 : " + code);
  438. lbl.AppendLine("写入 D351 产品型号 : " + code);
  439. // 写入 D351 产品编码
  440. byte[] byteCode = Encoding.ASCII.GetBytes(code);
  441. result = m.SetData("D", PLC_GoodsCode, 10, byteCode);
  442. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  443. lbl.AppendLine(JsonUtil.FromObject(m));
  444. if (!result)
  445. {
  446. LogNet_BeforeSaveToFile("PLC操作失败");
  447. lbl.AppendLine("PLC操作失败");
  448. return;
  449. }
  450. */
  451. LogNet_BeforeSaveToFile("写入 D330 标识 1 ");
  452. lbl.AppendLine("写入 D330 标识 1 ");
  453. // 写入 D330 标识 1
  454. result = m.SetData("D", 330, 1, "0001");
  455. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  456. lbl.AppendLine(JsonUtil.FromObject(m));
  457. if (!result)
  458. {
  459. LogNet_BeforeSaveToFile("PLC操作失败");
  460. lbl.AppendLine("PLC操作失败");
  461. return;
  462. }
  463. simplifyServer.SendMessage(arg1, "0100");
  464. }
  465. }
  466. #endregion
  467. #region 2 自动计件
  468. if (flag == 2)
  469. {
  470. string str = Encoding.ASCII.GetString(arg3);
  471. // 替换工号
  472. if (str.StartsWith("#"))
  473. {
  474. LogNet_BeforeSaveToFile("-----------------------------------------------");
  475. LogNet_BeforeSaveToFile("接收的工号为:" + str);
  476. lbl.AppendLine("接收的工号为:" + str);
  477. m = ConnSetting.PLC_ConnServer[flag.ToString()];
  478. try
  479. {
  480. m.Always = true;
  481. //m.ConnectSocket();
  482. }
  483. catch (Exception ex)
  484. {
  485. LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
  486. lbl.AppendLine("PLC连接失败 : " + ex.Message);
  487. return;
  488. }
  489. string usercode = str.Replace("#", "");
  490. if (string.IsNullOrWhiteSpace(usercode))
  491. {
  492. ConnSetting.SetPLCError(m, lbl, "接收的工号为空");
  493. simplifyServer.SendMessage(arg1, "0000");
  494. return;
  495. }
  496. string id = ConnSetting.CheckUsercode(usercode);
  497. usercode = ConnSetting.MESInfo.UserCode;
  498. this.lblUsercode.Text = usercode;
  499. //int l = usercode.Length;
  500. //LogNet_BeforeSaveToFile("写入 D150 当前工号长度 " + l);
  501. //lbl.AppendLine("写入 D150 当前工号长度 " + l);
  502. //// 写入 D150 当前工号长度
  503. ////result = m.SetData("D", PLC_GoodsCode_Length, 1, l.ToString("0000"));
  504. //result = m.SetData("D", 150, 1, l.ToString("X4"));
  505. //LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  506. //lbl.AppendLine(JsonUtil.FromObject(m));
  507. //if (!result)
  508. //{
  509. // LogNet_BeforeSaveToFile("PLC操作失败");
  510. // lbl.AppendLine("PLC操作失败");
  511. // return;
  512. //}
  513. LogNet_BeforeSaveToFile("写入 D150 当前工号 : " + usercode);
  514. lbl.AppendLine("写入 D150 当前工号 : " + usercode);
  515. // 写入 D150 当前工号
  516. byte[] byteCode = Encoding.ASCII.GetBytes(usercode);
  517. result = m.SetData("D", 150, 5, byteCode);
  518. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  519. lbl.AppendLine(JsonUtil.FromObject(m));
  520. if (!result)
  521. {
  522. LogNet_BeforeSaveToFile("PLC操作失败");
  523. lbl.AppendLine("PLC操作失败");
  524. return;
  525. }
  526. if (string.IsNullOrWhiteSpace(id))
  527. {
  528. ConnSetting.SetPLCError2(m, lbl, "工号不存在或不在此工序");
  529. simplifyServer.SendMessage(arg1, "0000");
  530. return;
  531. }
  532. else
  533. {
  534. LogNet_BeforeSaveToFile("写入 D280 标识 1 ");
  535. lbl.AppendLine("写入 D280 标识 1 ");
  536. // 写入 D280 标识 1
  537. result = m.SetData("D", 280, 1, "0001");
  538. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  539. lbl.AppendLine(JsonUtil.FromObject(m));
  540. if (!result)
  541. {
  542. LogNet_BeforeSaveToFile("PLC操作失败");
  543. lbl.AppendLine("PLC操作失败");
  544. return;
  545. }
  546. }
  547. MelsecA1EAscii m1 = ConnSetting.PLC_ConnServer["1"];
  548. try
  549. {
  550. m1.Always = true;
  551. //m.ConnectSocket();LogNet_BeforeSaveToFile("写入 D351 当前工号 : " + usercode);
  552. lbl.AppendLine("写入 D351 当前工号 : " + usercode);
  553. // 写入 D351 当前工号
  554. byte[] byteCode1 = Encoding.ASCII.GetBytes(usercode);
  555. result = m1.SetData("D", 351, 5, byteCode1);
  556. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m1));
  557. lbl.AppendLine(JsonUtil.FromObject(m1));
  558. if (!result)
  559. {
  560. LogNet_BeforeSaveToFile("PLC操作失败");
  561. lbl.AppendLine("PLC操作失败");
  562. //return;
  563. }
  564. }
  565. catch (Exception ex)
  566. {
  567. LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
  568. lbl.AppendLine("PLC连接失败 : " + ex.Message);
  569. //return;
  570. }
  571. finally
  572. {
  573. m1?.CloseSocket();
  574. }
  575. simplifyServer.SendMessage(arg1, "0100");
  576. return;
  577. }
  578. string barcode = str;
  579. LogNet_BeforeSaveToFile("-----------------------------------------------");
  580. LogNet_BeforeSaveToFile("接收的条码为:" + barcode);
  581. lbl.AppendLine("接收的条码为:" + barcode);
  582. m = ConnSetting.PLC_ConnServer[flag.ToString()];
  583. try
  584. {
  585. m.Always = true;
  586. //m.ConnectSocket();
  587. }
  588. catch (Exception ex)
  589. {
  590. LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
  591. lbl.AppendLine("PLC连接失败 : " + ex.Message);
  592. return;
  593. }
  594. if (string.IsNullOrEmpty(barcode) || "0" == barcode)
  595. {
  596. ConnSetting.SetPLCError2(m, lbl, "接收的条码为空");
  597. simplifyServer.SendMessage(arg1, "0000");
  598. return;
  599. }
  600. /*
  601. LogNet_BeforeSaveToFile("写入 D515 条码 : " + barcode);
  602. lbl.AppendLine("写入 D515 条码 : " + barcode);
  603. // 写入 D515 条码
  604. byte[] bytebarcode = Encoding.ASCII.GetBytes(barcode);
  605. result = m.SetData("D", PLC_Barcode2, 6, bytebarcode);
  606. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  607. lbl.AppendLine(JsonUtil.FromObject(m));
  608. if (!result)
  609. {
  610. LogNet_BeforeSaveToFile("PLC操作失败");
  611. lbl.AppendLine("PLC操作失败");
  612. return;
  613. }
  614. */
  615. LogNet_BeforeSaveToFile("读取 喷釉房号 ");
  616. lbl.AppendLine("读取 喷釉房号");
  617. // 读取 D510 喷釉房
  618. string roomcode = null;
  619. string roomAddress = ConnSetting.PLC_GlazingRoom;
  620. if (string.IsNullOrWhiteSpace(roomAddress) || roomAddress.Length < 2)
  621. {
  622. LogNet_BeforeSaveToFile("PLC喷釉房地址错误[" + roomAddress + "]");
  623. lbl.AppendLine("PLC喷釉房地址错误[" + roomAddress + "]");
  624. roomcode = "E";
  625. }
  626. else
  627. {
  628. string t = roomAddress[0].ToString();
  629. if (int.TryParse(roomAddress.Substring(1), out int n))
  630. {
  631. roomcode = m.GetHexData(t, n, 1);
  632. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  633. lbl.AppendLine(JsonUtil.FromObject(m));
  634. if (string.IsNullOrWhiteSpace(roomcode) ||
  635. !roomcode.StartsWith("8100") ||
  636. roomcode.Length < 8)
  637. {
  638. LogNet_BeforeSaveToFile("PLC操作失败");
  639. lbl.AppendLine("PLC操作失败");
  640. //return;
  641. roomcode = "E";
  642. }
  643. else
  644. {
  645. roomcode = roomcode.Substring(4);
  646. if (int.TryParse(roomcode, out int rc))
  647. {
  648. roomcode = rc.ToString();
  649. }
  650. }
  651. }
  652. else
  653. {
  654. LogNet_BeforeSaveToFile("PLC喷釉房地址错误[" + roomAddress + "]");
  655. lbl.AppendLine("PLC喷釉房地址错误[" + roomAddress + "]");
  656. roomcode = "E";
  657. }
  658. }
  659. LogNet_BeforeSaveToFile(roomcode);
  660. lbl.AppendLine(roomcode);
  661. // 记录 喷釉房
  662. bool boolroom = ConnSetting.SetGlazingRoom(barcode, roomcode);
  663. roomcode = string.Format("喷釉房号[{0}]", roomcode);
  664. LogNet_BeforeSaveToFile(roomcode);
  665. lbl.AppendLine(roomcode);
  666. #region 防伪码
  667. if (ConnSetting.SE_CODE_FLAG)
  668. {
  669. // ==================================================================
  670. // 金马返回 启用防伪码标识
  671. // TODO get from db begin
  672. string se_code_flag = m.GetHexData("D", 502, 1);
  673. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  674. lbl.AppendLine(JsonUtil.FromObject(m));
  675. if (string.IsNullOrWhiteSpace(se_code_flag) ||
  676. !se_code_flag.StartsWith("8100") ||
  677. se_code_flag.Length < 8)
  678. {
  679. LogNet_BeforeSaveToFile("PLC操作失败");
  680. lbl.AppendLine("PLC操作失败");
  681. //return;
  682. se_code_flag = "E";
  683. }
  684. else
  685. {
  686. se_code_flag = se_code_flag.Substring(4);
  687. if (int.TryParse(se_code_flag, out int rc))
  688. {
  689. se_code_flag = rc.ToString();
  690. }
  691. }
  692. LogNet_BeforeSaveToFile($"读取 启用防伪码标识[{se_code_flag}] ");
  693. lbl.AppendLine($"读取 启用防伪码标识[{se_code_flag}] ");
  694. // end
  695. // ==================================================================
  696. if (se_code_flag == "1")
  697. {
  698. // 读取防伪码
  699. int lianti = 496;
  700. string se_code1 = m.GetHexData("D", lianti, 6);
  701. //byte[] ss = m.GetByteData("D", lianti, 6);
  702. //string se_code1 = Encoding.ASCII.GetString(ss);
  703. if (!string.IsNullOrWhiteSpace(se_code1) &&
  704. se_code1.StartsWith("8100"))
  705. {
  706. se_code1 = se_code1.Substring(4);
  707. se_code1 = MelsecA1EAscii.GetByHexAscii(se_code1);
  708. if (se_code1.StartsWith("0"))
  709. {
  710. se_code1 = null;
  711. }
  712. }
  713. else
  714. {
  715. se_code1 = null;
  716. }
  717. //int zhineng = 222;
  718. //string se_code2 = m.GetHexData("D", zhineng, 6);
  719. //if (!string.IsNullOrWhiteSpace(se_code2) &&
  720. // se_code2.StartsWith("8100"))
  721. //{
  722. // se_code2 = se_code2.Substring(4);
  723. // if (se_code2.StartsWith("0"))
  724. // { se_code2 = null; }
  725. //}
  726. //else
  727. //{ se_code2 = null; }
  728. string se_code = null;
  729. if (!string.IsNullOrWhiteSpace(se_code1))
  730. {
  731. se_code = se_code1;
  732. }
  733. //else if (!string.IsNullOrWhiteSpace(se_code2))
  734. //{ se_code = se_code2; }
  735. if (string.IsNullOrWhiteSpace(se_code))
  736. {
  737. ConnSetting.SetPLCError2(m, lbl, "读取防伪码失败");
  738. return;
  739. }
  740. LogNet_BeforeSaveToFile($"读取 防伪码[{se_code}] ");
  741. lbl.AppendLine($"读取 防伪码[{se_code}] ");
  742. if (!string.IsNullOrWhiteSpace(ConnSetting.INI_PATH_UserCode))
  743. {
  744. this.lblUsercode.Text = INIUtil.Read("iBOSS.MES", "UserCode", ConnSetting.INI_PATH_UserCode);
  745. ConnSetting.MESInfo.UserCode = this.lblUsercode.Text;
  746. }
  747. //string se_result = SmartDeviceProxy.Instance.Invoke<string>(
  748. // p => p.CheckSecurityCodeByBarcode(
  749. // ConnSetting.MESInfo.AccountCode,
  750. // ConnSetting.MESInfo.UserCode,
  751. // barcode,
  752. // se_code
  753. // ));
  754. string se_result = SmartDeviceProxy.Instance.Invoke<string>(
  755. p => p.UpdateSecurityCodeByBarcode(
  756. ConnSetting.MESInfo.AccountCode,
  757. ConnSetting.MESInfo.UserCode,
  758. barcode,
  759. se_code,
  760. ConnSetting.MESInfo.Procedures[0]
  761. ));
  762. // 返回状态(成功、失败)
  763. if (se_result != "OK")
  764. {
  765. ConnSetting.SetPLCError2(m, lbl, "防伪码:" + this.lblUsercode.Text + " " + se_result);
  766. simplifyServer.SendMessage(arg1, "0000");
  767. return;
  768. }
  769. LogNet_BeforeSaveToFile($"绑定 防伪码[{se_result}] ");
  770. lbl.AppendLine($"绑定 防伪码[{se_result}] ");
  771. }
  772. }
  773. #endregion
  774. // 自动工序计件
  775. string scode = "OK";
  776. if (ConnSetting.AutoPost)
  777. {
  778. if (!string.IsNullOrWhiteSpace(ConnSetting.INI_PATH_UserCode))
  779. {
  780. this.lblUsercode.Text = INIUtil.Read("iBOSS.MES", "UserCode", ConnSetting.INI_PATH_UserCode);
  781. ConnSetting.MESInfo.UserCode = this.lblUsercode.Text;
  782. }
  783. scode = ConnSetting.AddWorkPieceByStatus(barcode, this.Text, roomcode);
  784. LogNet_BeforeSaveToFile("工序计件:" + this.lblUsercode.Text + " " + scode);
  785. lbl.AppendLine("工序计件:" + this.lblUsercode.Text + " " + scode);
  786. }
  787. //if (string.IsNullOrEmpty(scode))
  788. if(scode != "OK")
  789. {
  790. ConnSetting.SetPLCError2(m, lbl, "工序计件:" + this.lblUsercode.Text + " " + scode);
  791. simplifyServer.SendMessage(arg1, "0000");
  792. }
  793. else
  794. {
  795. LogNet_BeforeSaveToFile("写入 D280 标识 1 ");
  796. lbl.AppendLine("写入 D280 标识 1 ");
  797. // 写入 D280 标识 1
  798. result = m.SetData("D", 280, 1, "0001");
  799. LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
  800. lbl.AppendLine(JsonUtil.FromObject(m));
  801. if (!result)
  802. {
  803. LogNet_BeforeSaveToFile("PLC操作失败");
  804. lbl.AppendLine("PLC操作失败");
  805. return;
  806. }
  807. simplifyServer.SendMessage(arg1, "0100");
  808. }
  809. }
  810. #endregion
  811. // TODO 正常读取不记录日志
  812. //hasLog = false;
  813. simplifyServer.SendMessage(arg1, "0200");
  814. }
  815. catch (Exception ex)
  816. {
  817. if (flag == 1)
  818. {
  819. ConnSetting.SetPLCError(m, lbl, "系统异常:" + ex.Message);
  820. }
  821. else if (flag == 2)
  822. {
  823. ConnSetting.SetPLCError2(m, lbl, "系统异常:" + ex.Message);
  824. }
  825. string j = lbl + "\r\n";
  826. if (m != null)
  827. {
  828. j += JsonUtil.FromObject(m);
  829. }
  830. LogOut.Error(null, ex, j);
  831. //throw ex;
  832. }
  833. finally
  834. {
  835. m?.CloseSocket();
  836. if (hasDebugLog)
  837. {
  838. //string j = lbl + "\r\n";
  839. //if (m != null)
  840. //{
  841. // j += JsonUtil.FromObject(m);
  842. //}
  843. //LogOut.Debug(null, j);
  844. LogOut.Debug(null, lbl.ToString());
  845. }
  846. }
  847. }
  848. }
  849. private void LogNet_BeforeSaveToFile(string message)
  850. {
  851. if (InvokeRequired)
  852. {
  853. BeginInvoke(new Action<string>(LogNet_BeforeSaveToFile), message);
  854. return;
  855. }
  856. if (_FrmLog != null && _FrmLog.Visible)
  857. {
  858. _FrmLog.LogText.AppendText(message + Environment.NewLine);
  859. }
  860. }
  861. private void LogNet_BeforeSaveToFile(object sender, HslCommunication.LogNet.HslEventArgs e)
  862. {
  863. if (InvokeRequired)
  864. {
  865. BeginInvoke(new Action<object, HslCommunication.LogNet.HslEventArgs>(LogNet_BeforeSaveToFile), sender, e);
  866. return;
  867. }
  868. if (_FrmLog != null && _FrmLog.Visible)
  869. {
  870. _FrmLog.LogText.AppendText(e.HslMessage.ToString() + Environment.NewLine);
  871. }
  872. }
  873. private void btnStop_Click(object sender, EventArgs e)
  874. {
  875. if (simplifyServer != null)
  876. {
  877. simplifyServer.ServerClose();
  878. //simplifyServer.LogNet.Dispose();
  879. simplifyServer = null;
  880. lbls.Text = "停止";
  881. }
  882. if (_FrmLog != null)
  883. {
  884. _FrmLog.Close();
  885. _FrmLog.Dispose();
  886. _FrmLog = null;
  887. }
  888. }
  889. private void btnLog_Click(object sender, EventArgs e)
  890. {
  891. if (simplifyServer == null)
  892. {
  893. return;
  894. }
  895. if (_FrmLog == null || !_FrmLog.Visible)
  896. {
  897. _FrmLog = new FrmLog();
  898. _FrmLog.Text = this.Text;
  899. }
  900. _FrmLog.Show();
  901. }
  902. private void btnM1Test_Click(object sender, EventArgs e)
  903. {
  904. new FrmServerTest().Show();
  905. }
  906. private void btnDBTest_Click(object sender, EventArgs e)
  907. {
  908. IDataAccess dataAccess = null;
  909. try
  910. {
  911. ConnSetting.SetOracleConnStr();
  912. dataAccess = DataAccessFactory.CreateDataAccess(ConnSetting._oracleString, DataBaseType.Oracle);
  913. dataAccess.ExecuteScalar<string>("select 'x' from dual");
  914. MessageBox.Show("连接成功");
  915. }
  916. catch (Exception ex)
  917. {
  918. // todo log
  919. LogOut.Error(null, ex, ConnSetting._oracleString);
  920. MessageBox.Show("连接失败");
  921. }
  922. finally
  923. {
  924. dataAccess?.Close();
  925. }
  926. }
  927. private void btnExePath_Click(object sender, EventArgs e)
  928. {
  929. System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory);
  930. }
  931. private void btnResetINI_Click(object sender, EventArgs e)
  932. {
  933. ConnSetting.SetOracleConnStr();
  934. }
  935. private void chkAutoPiece_CheckedChanged(object sender, EventArgs e)
  936. {
  937. if (this.chkAutoPiece.Checked)
  938. {
  939. ConnSetting.SetMESInfo();
  940. }
  941. else
  942. {
  943. ConnSetting.MESInfo = null;
  944. }
  945. }
  946. private void button4_Click(object sender, EventArgs e)
  947. {
  948. string pp = ApplicationInformation.GetAbsolutePath("DBSetting.ini");
  949. string INI_PATH_UserCode = INIUtil.Read("iBOSS.MES", "UserCodeINIPath", pp);
  950. ConnSetting.AutoPost = (INIUtil.Read("iBOSS.MES", "AutoPost", pp).ToLower() == "true");
  951. this.chkAutoPiece.Checked = ConnSetting.AutoPost;
  952. //ConnSetting.AddWorkPiece("1234", this.Text);
  953. //string scode = ConnSetting.AddWorkPieceByStatus("1234", this.Text, string.Format("喷釉房号[{0}]", "1"));
  954. if (!string.IsNullOrWhiteSpace(INI_PATH_UserCode))
  955. {
  956. this.lblUsercode.Text = INIUtil.Read("iBOSS.MES", "UserCode", INI_PATH_UserCode);
  957. if (this.chkAutoPiece.Checked)
  958. {
  959. ConnSetting.MESInfo.UserCode = this.lblUsercode.Text;
  960. }
  961. }
  962. else
  963. {
  964. this.lblUsercode.Text = "NO CODE " + ConnSetting.MESInfo?.UserCode;
  965. }
  966. }
  967. }
  968. }