| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.ServiceModel.Channels;
- using System.Text;
- using System.Windows.Forms;
- using Curtain.DataAccess;
- using Curtain.Framework.Utils;
- using Curtain.Framework.Windows;
- using Curtain.WCF.Proxy;
- using HslCommunication.Core.Net;
- using HslCommunication.Enthernet;
- using PLCCommunication.Proxy;
- namespace PCLCommunication
- {
- public partial class FrmMelsecA1EAscii : Form
- {
- private string FormText = "PLC通信-";
- public FrmMelsecA1EAscii()
- {
- InitializeComponent();
- dgvSetting.AutoGenerateColumns = false;
- }
- private void FrmMelsecA1EAscii_Shown(object sender, EventArgs e)
- {
- try
- {
- ConnSetting.Init();
- //if (!this.chkAutoPiece.Checked)
- //{
- // ConnSetting.MESInfo = null;
- //}
- bool autoStart = false;
- if (string.Compare(ConnSetting.PLCServiceSettings[0], "true", true) == 0)
- {
- autoStart = true;
- }
- this.txtServerPort.Text = ConnSetting.PLCServiceSettings[1];
- this.Text = FormText + ConnSetting.PLCServiceSettings[1];
- this.dgvSetting.DataSource = ConnSetting.T_PLC;
- this.lblUsercode.Text = ConnSetting.MESInfo.UserCode;
- if (autoStart)
- {
- Start(false);
- }
- //SimplifyServer_ReceivedBytesEvent(new AppSession(), (HslCommunication.NetHandle)2,null);
- }
- catch (Exception ex)
- {
- // todo log
- LogOut.Error(null, ex);
- MessageBox.Show("系统异常");
- }
- }
- private void dgvSetting_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
- {
- //if (e.RowIndex < 0)
- //{
- // return;
- //}
- //string ename = this.dgvSetting.Rows[e.RowIndex].Cells["enable"].Value.ToString();
- //if (ename == "-1")
- //{
- // (this.dgvSetting.Rows[e.RowIndex].Cells["ss"] as DataGridViewButtonCell).
- //}
- }
- private void btnDelete_Click(object sender, EventArgs e)
- {
- if (this.dgvSetting.CurrentRow == null)
- {
- return;
- }
- try
- {
- DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
- string guid = item["GUID"].ToString();
- ConnSetting.DeletePLC(guid);
- item.Delete();
- ConnSetting.T_PLC.AcceptChanges();
- }
- catch (Exception ex)
- {
- // todo log
- LogOut.Error(null, ex);
- MessageBox.Show("系统异常");
- }
- }
- private void btnAdd_Click(object sender, EventArgs e)
- {
- }
- private void btnEdit_Click(object sender, EventArgs e)
- {
- if (this.dgvSetting.CurrentRow == null)
- {
- return;
- }
- try
- {
- DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
- FrmPLC plc = new FrmPLC();
- plc.Item = item.Row;
- if (plc.ShowDialog() == DialogResult.OK)
- {
- ConnSetting.EditPLC(item.Row);
- ConnSetting.T_PLC.AcceptChanges();
- }
- }
- catch (Exception ex)
- {
- //ConnSetting.T_PLC.Reset();
- // todo log
- LogOut.Error(null, ex);
- MessageBox.Show("系统异常");
- }
- }
- private void dgvSetting_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- if (e.RowIndex < 0)
- {
- return;
- }
- string colName = dgvSetting.Columns[e.ColumnIndex].Name;
- if (colName != "ss")
- {
- return;
- }
- try
- {
- string ename = this.dgvSetting.Rows[e.RowIndex].Cells["enable"].Value.ToString();
- if (ename == "-1")
- {
- return;
- }
- string ssname = this.dgvSetting.Rows[e.RowIndex].Cells["ss"].Value.ToString();
- string guid = this.dgvSetting.Rows[e.RowIndex].Cells["guid"].Value.ToString();
- if (ssname == "启用")
- {
- LogOut.Debug(null, "StartPLC:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
- ConnSetting.StartPLC(guid);
- DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
- item["Status"] = "1";
- item["StatusName"] = "已启动";
- item["ss"] = "停止";
- item.EndEdit();
- }
- else
- {
- ConnSetting.StopPLC(guid);
- LogOut.Debug(null, "StopPLC:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
- DataRowView item = (this.dgvSetting.CurrentRow.DataBoundItem as DataRowView);
- item["Status"] = "0";
- item["StatusName"] = "未启动";
- item["ss"] = "启用";
- item.EndEdit();
- }
- }
- catch (Exception ex)
- {
- // todo log
- LogOut.Error(null, ex);
- MessageBox.Show("系统异常");
- }
- }
- private void FrmMelsecA1EAscii_FormClosing(object sender, FormClosingEventArgs e)
- {
- //DialogResult dr = MessageBox.Show("确认退出?\r\n退出后机械施釉臂将无法读取产品型号。", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
- DialogResult dr = MessageBox.Show("确认退出?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
- if (dr == DialogResult.OK)
- {
- dr = MessageBox.Show("确认退出?", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
- if (dr == DialogResult.OK)
- {
- ConnSetting.Dispose();
- return;
- }
- }
- e.Cancel = true;
- }
- private void btnTest_Click(object sender, EventArgs e)
- {
- try
- {
- FrmTest ft = new FrmTest();
- ft.Show();
- }
- catch (Exception ex)
- {
- // todo log
- LogOut.Error(null, ex);
- MessageBox.Show("系统异常");
- }
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- Start();
- }
- private NetSimplifyServer simplifyServer;
- private FrmLog _FrmLog = null;
- private void Start(bool showFrmLog=true)
- {
- if (simplifyServer != null)
- {
- simplifyServer.ServerClose();
- //simplifyServer.LogNet.Dispose();
- // 释放log文件
- System.Threading.Thread.Sleep(300);
- }
- try
- {
- simplifyServer = new NetSimplifyServer();
- simplifyServer.LogNet = new HslCommunication.LogNet.LogNetSingle(Application.StartupPath + @"\ServerLogs\log.txt");
- simplifyServer.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile;
- //simplifyServer.Token = new Guid(textBox3.Text);
- //simplifyServer.ReceiveStringEvent += SimplifyServer_ReceiveStringEvent;
- simplifyServer.ReceivedBytesEvent += SimplifyServer_ReceivedBytesEvent;
- //ConnSetting.AutoRun();
- simplifyServer.ServerStart(int.Parse(txtServerPort.Text.Trim()));
- ConnSetting.PLCServiceSettings[1] = txtServerPort.Text.Trim();
- this.Text = FormText + ConnSetting.PLCServiceSettings[1];
- ConnSetting.SavePLCServicePort();
- lbls.Text = "启动";
- if (showFrmLog)
- {
- if (_FrmLog != null)
- {
- _FrmLog.Close();
- _FrmLog.Dispose();
- }
- _FrmLog = new FrmLog();
- _FrmLog.Text = this.Text;
- _FrmLog.Show();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("启动失败:" + ex.Message);
- }
- }
- // 1楼 5001
- //public const int PLC_Barcode_Flag = 330;
- //public const int PLC_Error_Flag = 331;
- //public const int PLC_GoodsCode_Length = 350;
- //public const int PLC_Barcode = 341;
- //public const int PLC_GoodsCode = 351;
- //// 2楼 5002
- //public const int PLC_Barcode_Flag2 = 510;
- //public const int PLC_Error_Flag2 = 511;
- //public const int PLC_GoodsCode_Length2 = 513;
- //public const int PLC_Barcode2 = 515;
- //public const int PLC_GoodsCode2 = 525;
- private void SimplifyServer_ReceivedBytesEvent(AppSession arg1, HslCommunication.NetHandle arg2, byte[] arg3)
- {
- StringBuilder lbl = new StringBuilder();
- MelsecA1EAscii m = null;
- bool hasDebugLog = true;
- bool result = false;
- int flag = 0;
- //int ff = (int)arg2;
- //string str = Encoding.ASCII.GetString(arg3);
- //MessageBox.Show(str, "c:" + ff.ToString());
- //return;
- lock (this)
- {
- try
- {
- //throw new NotImplementedException();
- flag = (int)arg2;
- #region 1 识别型号
- if (flag == 1)
- {
- string str = Encoding.ASCII.GetString(arg3);
- // 替换工号
- if (str.StartsWith("#"))
- {
- LogNet_BeforeSaveToFile("-----------------------------------------------");
- LogNet_BeforeSaveToFile("接收的工号为:" + str);
- lbl.AppendLine("接收的工号为:" + str);
- m = ConnSetting.PLC_ConnServer[flag.ToString()];
- try
- {
- m.Always = true;
- //m.ConnectSocket();
- }
- catch (Exception ex)
- {
- LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
- lbl.AppendLine("PLC连接失败 : " + ex.Message);
- return;
- }
- string usercode = str.Replace("#", "");
- if (string.IsNullOrWhiteSpace(usercode))
- {
- ConnSetting.SetPLCError(m, lbl, "接收的工号为空");
- simplifyServer.SendMessage(arg1, "0000");
- return;
- }
- string id = ConnSetting.CheckUsercode(usercode);
- usercode = ConnSetting.MESInfo.UserCode;
- //int l = usercode.Length;
- //LogNet_BeforeSaveToFile("写入 D350 当前工号长度 " + l);
- //lbl.AppendLine("写入 D350 当前工号长度 " + l);
- //// 写入 D350 当前工号长度
- ////result = m.SetData("D", PLC_GoodsCode_Length, 1, l.ToString("0000"));
- //result = m.SetData("D", 350, 1, l.ToString("X4"));
- //LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- //lbl.AppendLine(JsonUtil.FromObject(m));
- //if (!result)
- //{
- // LogNet_BeforeSaveToFile("PLC操作失败");
- // lbl.AppendLine("PLC操作失败");
- // return;
- //}
- LogNet_BeforeSaveToFile("写入 D351 当前工号 : " + usercode);
- lbl.AppendLine("写入 D351 当前工号 : " + usercode);
- // 写入 D351 当前工号
- byte[] byteCode = Encoding.ASCII.GetBytes(usercode);
- result = m.SetData("D", 351, 5, byteCode);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- if (string.IsNullOrWhiteSpace(id))
- {
- ConnSetting.SetPLCError(m, lbl, "工号不存在或不在此工序");
- simplifyServer.SendMessage(arg1, "0000");
- return;
- }
- else
- {
- LogNet_BeforeSaveToFile("写入 D330 标识 1 ");
- lbl.AppendLine("写入 D330 标识 1 ");
- // 写入 D330 标识 1
- result = m.SetData("D", 330, 1, "0001");
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- }
- simplifyServer.SendMessage(arg1, "0100");
- return;
- }
- string barcode = str;
- LogNet_BeforeSaveToFile("-----------------------------------------------");
- LogNet_BeforeSaveToFile("接收的条码为:" + barcode);
- lbl.AppendLine("接收的条码为:" + barcode);
- m = ConnSetting.PLC_ConnServer[flag.ToString()];
- try
- {
- m.Always = true;
- //m.ConnectSocket();
- }
- catch (Exception ex)
- {
- LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
- lbl.AppendLine("PLC连接失败 : " + ex.Message);
- return;
- }
- if (string.IsNullOrEmpty(barcode) || "0" == barcode)
- {
- ConnSetting.SetPLCError(m, lbl, "接收的条码为空");
- simplifyServer.SendMessage(arg1, "0000");
- return;
- }
- LogNet_BeforeSaveToFile("写入 D341 条码 : " + barcode);
- lbl.AppendLine("写入 D341 条码 : " + barcode);
- // 写入 D341 条码
- byte[] bytebarcode = Encoding.ASCII.GetBytes(barcode);
- result = m.SetData("D", 341, 6, bytebarcode);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- LogNet_BeforeSaveToFile("查询 产品编码 : ");
- lbl.Append("查询 产品编码 : ");
- // 查询 产品编码
- string code = ConnSetting.GetGoodscode(barcode);
- LogNet_BeforeSaveToFile(code);
- lbl.AppendLine(code);
- if (string.IsNullOrEmpty(code))
- {
- LogNet_BeforeSaveToFile("写入 D332 产品型号代码 0");
- lbl.AppendLine("写入 D332 产品型号代码 0");
- // 写入 D332 产品型号代码
- result = m.SetData("D", 332, 1, "0000");
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- /*
- LogNet_BeforeSaveToFile("写入 D351 产品型号");
- lbl.AppendLine("写入 D351 产品型号");
- result = m.SetData("D", PLC_GoodsCode, 10, "0000000000000000000000000000000000000000");
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- */
- ConnSetting.SetPLCError(m, lbl, "产品型号为空");
- simplifyServer.SendMessage(arg1, "0000");
- }
- else
- {
- //int l = code.Length;
- int l = int.Parse(code);
- LogNet_BeforeSaveToFile("写入 D332 产品型号代码 " + l);
- lbl.AppendLine("写入 D332 产品型号代码 " + l);
- // 写入 D332 产品型号代码
- //result = m.SetData("D", PLC_GoodsCode_Length, 1, l.ToString("0000"));
- result = m.SetData("D", 332, 1, l.ToString("X4"));
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- /*
- LogNet_BeforeSaveToFile("写入 D351 产品型号 : " + code);
- lbl.AppendLine("写入 D351 产品型号 : " + code);
- // 写入 D351 产品编码
- byte[] byteCode = Encoding.ASCII.GetBytes(code);
- result = m.SetData("D", PLC_GoodsCode, 10, byteCode);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- */
- LogNet_BeforeSaveToFile("写入 D330 标识 1 ");
- lbl.AppendLine("写入 D330 标识 1 ");
- // 写入 D330 标识 1
- result = m.SetData("D", 330, 1, "0001");
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- simplifyServer.SendMessage(arg1, "0100");
- }
- }
- #endregion
- #region 2 自动计件
- if (flag == 2)
- {
- string str = Encoding.ASCII.GetString(arg3);
- // 替换工号
- if (str.StartsWith("#"))
- {
- LogNet_BeforeSaveToFile("-----------------------------------------------");
- LogNet_BeforeSaveToFile("接收的工号为:" + str);
- lbl.AppendLine("接收的工号为:" + str);
- m = ConnSetting.PLC_ConnServer[flag.ToString()];
- try
- {
- m.Always = true;
- //m.ConnectSocket();
- }
- catch (Exception ex)
- {
- LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
- lbl.AppendLine("PLC连接失败 : " + ex.Message);
- return;
- }
- string usercode = str.Replace("#", "");
- if (string.IsNullOrWhiteSpace(usercode))
- {
- ConnSetting.SetPLCError(m, lbl, "接收的工号为空");
- simplifyServer.SendMessage(arg1, "0000");
- return;
- }
- string id = ConnSetting.CheckUsercode(usercode);
- usercode = ConnSetting.MESInfo.UserCode;
- this.lblUsercode.Text = usercode;
- //int l = usercode.Length;
- //LogNet_BeforeSaveToFile("写入 D150 当前工号长度 " + l);
- //lbl.AppendLine("写入 D150 当前工号长度 " + l);
- //// 写入 D150 当前工号长度
- ////result = m.SetData("D", PLC_GoodsCode_Length, 1, l.ToString("0000"));
- //result = m.SetData("D", 150, 1, l.ToString("X4"));
- //LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- //lbl.AppendLine(JsonUtil.FromObject(m));
- //if (!result)
- //{
- // LogNet_BeforeSaveToFile("PLC操作失败");
- // lbl.AppendLine("PLC操作失败");
- // return;
- //}
- LogNet_BeforeSaveToFile("写入 D150 当前工号 : " + usercode);
- lbl.AppendLine("写入 D150 当前工号 : " + usercode);
- // 写入 D150 当前工号
- byte[] byteCode = Encoding.ASCII.GetBytes(usercode);
- result = m.SetData("D", 150, 5, byteCode);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- if (string.IsNullOrWhiteSpace(id))
- {
- ConnSetting.SetPLCError2(m, lbl, "工号不存在或不在此工序");
- simplifyServer.SendMessage(arg1, "0000");
- return;
- }
- else
- {
- LogNet_BeforeSaveToFile("写入 D280 标识 1 ");
- lbl.AppendLine("写入 D280 标识 1 ");
- // 写入 D280 标识 1
- result = m.SetData("D", 280, 1, "0001");
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- }
- MelsecA1EAscii m1 = ConnSetting.PLC_ConnServer["1"];
- try
- {
- m1.Always = true;
- //m.ConnectSocket();LogNet_BeforeSaveToFile("写入 D351 当前工号 : " + usercode);
- lbl.AppendLine("写入 D351 当前工号 : " + usercode);
- // 写入 D351 当前工号
- byte[] byteCode1 = Encoding.ASCII.GetBytes(usercode);
- result = m1.SetData("D", 351, 5, byteCode1);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m1));
- lbl.AppendLine(JsonUtil.FromObject(m1));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- //return;
- }
- }
- catch (Exception ex)
- {
- LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
- lbl.AppendLine("PLC连接失败 : " + ex.Message);
- //return;
- }
- finally
- {
- m1?.CloseSocket();
- }
- simplifyServer.SendMessage(arg1, "0100");
- return;
- }
- string barcode = str;
- LogNet_BeforeSaveToFile("-----------------------------------------------");
- LogNet_BeforeSaveToFile("接收的条码为:" + barcode);
- lbl.AppendLine("接收的条码为:" + barcode);
- m = ConnSetting.PLC_ConnServer[flag.ToString()];
- //byte[] byteCode11 = Encoding.ASCII.GetBytes(barcode + "$");
- //result = m.SetData("D", 496, 6, byteCode11);
- //LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- //lbl.AppendLine(JsonUtil.FromObject(m));
- //if (!result)
- //{
- // LogNet_BeforeSaveToFile("PLC操作失败");
- // lbl.AppendLine("PLC操作失败");
- // return;
- //}
- try
- {
- m.Always = true;
- //m.ConnectSocket();
- }
- catch (Exception ex)
- {
- LogNet_BeforeSaveToFile("PLC连接失败:" + ex.Message);
- lbl.AppendLine("PLC连接失败 : " + ex.Message);
- return;
- }
- if (string.IsNullOrEmpty(barcode) || "0" == barcode)
- {
- ConnSetting.SetPLCError2(m, lbl, "接收的条码为空");
- simplifyServer.SendMessage(arg1, "0000");
- return;
- }
- /*
- LogNet_BeforeSaveToFile("写入 D515 条码 : " + barcode);
- lbl.AppendLine("写入 D515 条码 : " + barcode);
- // 写入 D515 条码
- byte[] bytebarcode = Encoding.ASCII.GetBytes(barcode);
- result = m.SetData("D", PLC_Barcode2, 6, bytebarcode);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- */
- LogNet_BeforeSaveToFile("读取 喷釉房号 ");
- lbl.AppendLine("读取 喷釉房号");
- // 读取 D510 喷釉房
- string roomcode = null;
- string roomAddress = ConnSetting.PLC_GlazingRoom;
- if (string.IsNullOrWhiteSpace(roomAddress) || roomAddress.Length < 2)
- {
- LogNet_BeforeSaveToFile("PLC喷釉房地址错误[" + roomAddress + "]");
- lbl.AppendLine("PLC喷釉房地址错误[" + roomAddress + "]");
- roomcode = "E";
- }
- else
- {
- string t = roomAddress[0].ToString();
- if (int.TryParse(roomAddress.Substring(1), out int n))
- {
- roomcode = m.GetHexData(t, n, 1);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (string.IsNullOrWhiteSpace(roomcode) ||
- !roomcode.StartsWith("8100") ||
- roomcode.Length < 8)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- //return;
- roomcode = "E";
- }
- else
- {
- roomcode = roomcode.Substring(4);
- if (int.TryParse(roomcode, out int rc))
- {
- roomcode = rc.ToString();
- }
- }
- }
- else
- {
- LogNet_BeforeSaveToFile("PLC喷釉房地址错误[" + roomAddress + "]");
- lbl.AppendLine("PLC喷釉房地址错误[" + roomAddress + "]");
- roomcode = "E";
- }
- }
- LogNet_BeforeSaveToFile(roomcode);
- lbl.AppendLine(roomcode);
- // 记录 喷釉房
- bool boolroom = ConnSetting.SetGlazingRoom(barcode, roomcode);
- roomcode = string.Format("喷釉房号[{0}]", roomcode);
- LogNet_BeforeSaveToFile(roomcode);
- lbl.AppendLine(roomcode);
- #region 防伪码
- /*
- if (ConnSetting.SE_CODE_FLAG)
- {
- // ==================================================================
- // 金马返回 启用防伪码标识
- // TODO get from db begin
- string se_code_flag = m.GetHexData("D", 502, 1);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (string.IsNullOrWhiteSpace(se_code_flag) ||
- !se_code_flag.StartsWith("8100") ||
- se_code_flag.Length < 8)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- //return;
- se_code_flag = "E";
- }
- else
- {
- se_code_flag = se_code_flag.Substring(4);
- if (int.TryParse(se_code_flag, out int rc))
- {
- se_code_flag = rc.ToString();
- }
- }
- LogNet_BeforeSaveToFile($"读取 启用防伪码标识[{se_code_flag}] ");
- lbl.AppendLine($"读取 启用防伪码标识[{se_code_flag}] ");
- // end
- // ==================================================================
- if (se_code_flag == "1")
- {
- // 读取防伪码
- int lianti = 496;
- string se_code1 = m.GetHexData("D", lianti, 6);
- //byte[] ss = m.GetByteData("D", lianti, 6);
- //string se_code1 = Encoding.ASCII.GetString(ss);
- if (!string.IsNullOrWhiteSpace(se_code1) &&
- se_code1.StartsWith("8100"))
- {
- se_code1 = se_code1.Substring(4);
- se_code1 = MelsecA1EAscii.GetByHexAscii(se_code1);
- if (se_code1.StartsWith("0"))
- {
- se_code1 = null;
- }
- }
- else
- {
- se_code1 = null;
- }
- //int zhineng = 222;
- //string se_code2 = m.GetHexData("D", zhineng, 6);
- //if (!string.IsNullOrWhiteSpace(se_code2) &&
- // se_code2.StartsWith("8100"))
- //{
- // se_code2 = se_code2.Substring(4);
- // if (se_code2.StartsWith("0"))
- // { se_code2 = null; }
- //}
- //else
- //{ se_code2 = null; }
- string se_code = null;
- if (!string.IsNullOrWhiteSpace(se_code1))
- {
- se_code = se_code1;
- }
- //else if (!string.IsNullOrWhiteSpace(se_code2))
- //{ se_code = se_code2; }
- if (string.IsNullOrWhiteSpace(se_code))
- {
- ConnSetting.SetPLCError2(m, lbl, "读取防伪码失败");
- return;
- }
- LogNet_BeforeSaveToFile($"读取 防伪码[{se_code}] ");
- lbl.AppendLine($"读取 防伪码[{se_code}] ");
- if (!string.IsNullOrWhiteSpace(ConnSetting.INI_PATH_UserCode))
- {
- this.lblUsercode.Text = INIUtil.Read("iBOSS.MES", "UserCode", ConnSetting.INI_PATH_UserCode);
- ConnSetting.MESInfo.UserCode = this.lblUsercode.Text;
- }
- //string se_result = SmartDeviceProxy.Instance.Invoke<string>(
- // p => p.CheckSecurityCodeByBarcode(
- // ConnSetting.MESInfo.AccountCode,
- // ConnSetting.MESInfo.UserCode,
- // barcode,
- // se_code
- // ));
- string se_result = SmartDeviceProxy.Instance.Invoke<string>(
- p => p.UpdateSecurityCodeByBarcode(
- ConnSetting.MESInfo.AccountCode,
- ConnSetting.MESInfo.UserCode,
- barcode,
- se_code,
- ConnSetting.MESInfo.Procedures[0]
- ));
- // 返回状态(成功、失败)
- if (se_result != "OK")
- {
- ConnSetting.SetPLCError2(m, lbl, "防伪码:" + this.lblUsercode.Text + " " + se_result);
- simplifyServer.SendMessage(arg1, "0000");
- return;
- }
- LogNet_BeforeSaveToFile($"绑定 防伪码[{se_result}] ");
- lbl.AppendLine($"绑定 防伪码[{se_result}] ");
- }
- }
- */
- // 1 获取条码对应的产品型号,是否需要绑定防伪码
- DataRow dr = ConnSetting.GetGoodsInfo(barcode);
- if (dr == null)
- {
- //LogNet_BeforeSaveToFile("写入 D281 错误标识 : 0 条码不存在" + barcode);
- //lbl.AppendLine("写入 D281 错误标识 : 0 条码不存在" + barcode);
- //// 写入 D281 错误标识
- //bool result = m.SetData("D", 281, 1, "0001");
- //LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- //lbl.AppendLine(JsonUtil.FromObject(m));
- //if (!result)
- //{
- // LogNet_BeforeSaveToFile("PLC操作失败");
- // lbl.AppendLine("PLC操作失败");
- //}
- ConnSetting.SetPLCError2(m, lbl, "条码不存在:" + barcode);
- return;
- }
- string SECURITYCODEBINDFLAG = dr["SECURITYCODEBINDFLAG"] + "";
- string goodstypeid = dr["goodstypeid"] + "";
- // 1. 扫码类型(int)【0:无动作,1:启动智能扫码枪 2:启动连体扫码枪】
- string gt_code = "0000";
- if (SECURITYCODEBINDFLAG == "1")
- {
- if (goodstypeid == "3" || goodstypeid == "4")
- {
- // 非智能
- gt_code = "0002";
- }
- else
- {
- // 智能
- gt_code = "0001";
- }
- LogNet_BeforeSaveToFile("写入 D492 : " + gt_code + " 条码型号 " + barcode);
- lbl.AppendLine("写入 D492 : " + gt_code + " 条码型号 " + barcode);
- // 写入 D281 错误标识
- result = m.SetData("D", 492, 1, gt_code);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- LogNet_BeforeSaveToFile("写入 D496 条码 : " + barcode);
- lbl.AppendLine("写入 D496 条码 : " + barcode);
- // 写入 D150 当前工号
- byte[] byteCode1 = Encoding.ASCII.GetBytes(barcode + "$");
- result = m.SetData("D", 496, 6, byteCode1);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- }
- else
- {
- LogNet_BeforeSaveToFile("写入 D492 : 条码不需要绑定 " + barcode);
- lbl.AppendLine("写入 D492 : 条码不需要绑定 " + barcode);
- // 写入 D281 错误标识
- result = m.SetData("D", 492, 1, gt_code);
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- }
- #endregion
- // 自动工序计件
- string scode = "OK";
- if (ConnSetting.AutoPost)
- {
- if (!string.IsNullOrWhiteSpace(ConnSetting.INI_PATH_UserCode))
- {
- this.lblUsercode.Text = INIUtil.Read("iBOSS.MES", "UserCode", ConnSetting.INI_PATH_UserCode);
- ConnSetting.MESInfo.UserCode = this.lblUsercode.Text;
- }
- scode = ConnSetting.AddWorkPieceByStatus(barcode, this.Text, roomcode);
- LogNet_BeforeSaveToFile("工序计件:" + this.lblUsercode.Text + " " + scode);
- lbl.AppendLine("工序计件:" + this.lblUsercode.Text + " " + scode);
- }
- //if (string.IsNullOrEmpty(scode))
- if(scode != "OK")
- {
- ConnSetting.SetPLCError2(m, lbl, "工序计件:" + this.lblUsercode.Text + " " + scode);
- simplifyServer.SendMessage(arg1, "0000");
- }
- else
- {
- LogNet_BeforeSaveToFile("写入 D280 标识 1 ");
- lbl.AppendLine("写入 D280 标识 1 ");
- // 写入 D280 标识 1
- result = m.SetData("D", 280, 1, "0001");
- LogNet_BeforeSaveToFile(JsonUtil.FromObject(m));
- lbl.AppendLine(JsonUtil.FromObject(m));
- if (!result)
- {
- LogNet_BeforeSaveToFile("PLC操作失败");
- lbl.AppendLine("PLC操作失败");
- return;
- }
- simplifyServer.SendMessage(arg1, "0100");
- }
- }
- #endregion
- // TODO 正常读取不记录日志
- //hasLog = false;
- simplifyServer.SendMessage(arg1, "0200");
- }
- catch (Exception ex)
- {
- if (flag == 1)
- {
- ConnSetting.SetPLCError(m, lbl, "系统异常:" + ex.Message);
- }
- else if (flag == 2)
- {
- ConnSetting.SetPLCError2(m, lbl, "系统异常:" + ex.Message);
- }
- string j = lbl + "\r\n";
- if (m != null)
- {
- j += JsonUtil.FromObject(m);
- }
- LogOut.Error(null, ex, j);
- //throw ex;
- }
- finally
- {
- m?.CloseSocket();
- if (hasDebugLog)
- {
- //string j = lbl + "\r\n";
- //if (m != null)
- //{
- // j += JsonUtil.FromObject(m);
- //}
- //LogOut.Debug(null, j);
- LogOut.Debug(null, lbl.ToString());
- }
- }
- }
- }
- private void LogNet_BeforeSaveToFile(string message)
- {
- if (InvokeRequired)
- {
- BeginInvoke(new Action<string>(LogNet_BeforeSaveToFile), message);
- return;
- }
- if (_FrmLog != null && _FrmLog.Visible)
- {
- _FrmLog.LogText.AppendText(message + Environment.NewLine);
- }
- }
- private void LogNet_BeforeSaveToFile(object sender, HslCommunication.LogNet.HslEventArgs e)
- {
- if (InvokeRequired)
- {
- BeginInvoke(new Action<object, HslCommunication.LogNet.HslEventArgs>(LogNet_BeforeSaveToFile), sender, e);
- return;
- }
- if (_FrmLog != null && _FrmLog.Visible)
- {
- _FrmLog.LogText.AppendText(e.HslMessage.ToString() + Environment.NewLine);
- }
- }
- private void btnStop_Click(object sender, EventArgs e)
- {
- if (simplifyServer != null)
- {
- simplifyServer.ServerClose();
- //simplifyServer.LogNet.Dispose();
- simplifyServer = null;
- lbls.Text = "停止";
- }
- if (_FrmLog != null)
- {
- _FrmLog.Close();
- _FrmLog.Dispose();
- _FrmLog = null;
- }
- }
- private void btnLog_Click(object sender, EventArgs e)
- {
- if (simplifyServer == null)
- {
- return;
- }
- if (_FrmLog == null || !_FrmLog.Visible)
- {
- _FrmLog = new FrmLog();
- _FrmLog.Text = this.Text;
- }
- _FrmLog.Show();
- }
- private void btnM1Test_Click(object sender, EventArgs e)
- {
- new FrmServerTest().Show();
- }
- private void btnDBTest_Click(object sender, EventArgs e)
- {
- IDataAccess dataAccess = null;
- try
- {
- ConnSetting.SetOracleConnStr();
- dataAccess = DataAccessFactory.CreateDataAccess(ConnSetting._oracleString, DataBaseType.Oracle);
- dataAccess.ExecuteScalar<string>("select 'x' from dual");
- MessageBox.Show("连接成功");
- }
- catch (Exception ex)
- {
- // todo log
- LogOut.Error(null, ex, ConnSetting._oracleString);
- MessageBox.Show("连接失败");
- }
- finally
- {
- dataAccess?.Close();
- }
- }
- private void btnExePath_Click(object sender, EventArgs e)
- {
- System.Diagnostics.Process.Start(System.AppDomain.CurrentDomain.BaseDirectory);
- }
- private void btnResetINI_Click(object sender, EventArgs e)
- {
- ConnSetting.SetOracleConnStr();
- }
- private void chkAutoPiece_CheckedChanged(object sender, EventArgs e)
- {
- if (this.chkAutoPiece.Checked)
- {
- ConnSetting.SetMESInfo();
- }
- else
- {
- ConnSetting.MESInfo = null;
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- string pp = ApplicationInformation.GetAbsolutePath("DBSetting.ini");
- string INI_PATH_UserCode = INIUtil.Read("iBOSS.MES", "UserCodeINIPath", pp);
- ConnSetting.AutoPost = (INIUtil.Read("iBOSS.MES", "AutoPost", pp).ToLower() == "true");
- this.chkAutoPiece.Checked = ConnSetting.AutoPost;
- //ConnSetting.AddWorkPiece("1234", this.Text);
- //string scode = ConnSetting.AddWorkPieceByStatus("1234", this.Text, string.Format("喷釉房号[{0}]", "1"));
- if (!string.IsNullOrWhiteSpace(INI_PATH_UserCode))
- {
- this.lblUsercode.Text = INIUtil.Read("iBOSS.MES", "UserCode", INI_PATH_UserCode);
- if (this.chkAutoPiece.Checked)
- {
- ConnSetting.MESInfo.UserCode = this.lblUsercode.Text;
- }
- }
- else
- {
- this.lblUsercode.Text = "NO CODE " + ConnSetting.MESInfo?.UserCode;
- }
- }
- }
- }
|