FrmMelsecA1EAscii.cs 51 KB

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