using Mesnac.Action.ChemicalWeighing.Barrel; using Mesnac.Action.ChemicalWeighing.Entity.material; using Mesnac.Action.ChemicalWeighing.MaterialManage; using Mesnac.Action.ChemicalWeighing.Product.XlPlan; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Action.ChemicalWeighing { public class ScanControl { Dictionary dicCom1=new Dictionary(); Dictionary dicCom3 = new Dictionary();//COM3缓存 Dictionary dicCom5 = new Dictionary();//COM5缓存 public static string scanStr { get; set; } //public string portName = "COM3"; /// /// 实例化串行端口资源_COM3 /// public void InstanceSerialPort3(string portName) { //实例化串行端口 SerialPort serialPort = new SerialPort(); //端口名 注:因为使用的是USB转RS232 所以去设备管理器中查看一下虚拟com口的名字 serialPort.PortName = portName; //波特率 serialPort.BaudRate = 9600; //奇偶校验 serialPort.Parity = Parity.None; //停止位 serialPort.StopBits = StopBits.One; //数据位 serialPort.DataBits = 8; //忽略null字节 serialPort.DiscardNull = true; //接收事件 serialPort.DataReceived += serialPort_DataReceived3; //开启串口 serialPort.Open(); } /// /// 实例化串行端口资源_COM5 /// public void InstanceSerialPort5(string portName) { //实例化串行端口 SerialPort serialPort = new SerialPort(); //端口名 注:因为使用的是USB转RS232 所以去设备管理器中查看一下虚拟com口的名字 serialPort.PortName = portName; //波特率 serialPort.BaudRate = 9600; //奇偶校验 serialPort.Parity = Parity.None; //停止位 serialPort.StopBits = StopBits.One; //数据位 serialPort.DataBits = 8; //忽略null字节 serialPort.DiscardNull = true; //接收事件 serialPort.DataReceived += serialPort_DataReceived5; //开启串口 serialPort.Open(); } /// /// 接收数据 /// /// /// void serialPort_DataReceived3(object sender, SerialDataReceivedEventArgs e) { try { //防止数据接收不完整 线程sleep(1000) System.Threading.Thread.Sleep(1000); SerialPort serialPort = (SerialPort)sender; //开启接收数据线程 Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(ReceiveData3)); threadReceiveSub.Start(serialPort); } catch (Exception ex) { throw; } } /// /// 接收数据 /// /// /// void serialPort_DataReceived5(object sender, SerialDataReceivedEventArgs e) { try { //防止数据接收不完整 线程sleep(1000) System.Threading.Thread.Sleep(1000); SerialPort serialPort = (SerialPort)sender; //开启接收数据线程 Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(ReceiveData5)); threadReceiveSub.Start(serialPort); } catch (Exception ex) { throw; } } private void ReceiveData3(object serialPortobj) { try { SerialPort serialPort = (SerialPort)serialPortobj; string code = serialPort.ReadExisting(); if (string.IsNullOrEmpty(code)) { MessageBox.Show("未扫描到条码!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } SetBarCodeState(code); } catch (Exception ex) { throw; } } /// /// 小料 称量结束 绑定料桶 /// /// 桶条码 private static void SetBarCodeState(string barCode) { //验证该桶是否绑定了物料,绑定弹框提醒 if (BarrelHelper.IsBindBarrel(barCode)) { MessageBox.Show("该桶已经绑定物料!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //检查检量称是否称量完成 if (BasePlcHelper.Instance.plt_Read1Jc.NowValue.ToInt() == 1) { var down = PlanHelper.GetExecPlan().FirstOrDefault(d => d.Plan_State == 3); //绑定桶 int tcheckWeight = BasePlcHelper.Instance.plt_TCheck_Weight.NowValue.ToInt();//检量秤重量 bool bind= BarrelHelper.BindBarrel(barCode, down.MaterialID, down.Material_name, tcheckWeight); if (bind) { MessageBox.Show("物料绑定桶失败!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int tcheckBatch = BasePlcHelper.Instance.plt_Batch.NowValue.ToInt();//当前执行的批次 int plan_Serial = down.Plan_Num;//计划的批次 double totalError = down.Total_Error;//当前批次设定总误差 int batch = down.Plan_Num - tcheckBatch;//剩余批次 double error = tcheckWeight - down.Total_Weight;//实际误差 double total_Weight = down.Total_Weight + tcheckWeight;//重量累加 double total_Error = down.Total_Error + error;//误差累加 if (-totalError <= error && error <= totalError)//在误差之内 { total_Weight = total_Weight / 1000; total_Error = total_Error / 1000; if (batch == 0) //完成 { PlanHelper.UpdatePlanCompletedQuantity(down.Plan_Id, tcheckBatch, 8, DateTime.Now, total_Weight, total_Error); //判断上位机判断当前值与配方总重,满足要求时,置“2” BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Read1Jc, new object[] { 2 }); } else { PlanHelper.UpdatePlanCompletedQuantity(down.Plan_Id, tcheckBatch, total_Weight, total_Error); //判断上位机判断当前值与配方总重,满足要求时,置“2” BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Read1Jc, new object[] { 2 }); } } else { total_Weight = total_Weight / 1000; total_Error = total_Error / 1000; if (batch == 0)//完成 { PlanHelper.UpdatePlanCompletedQuantity(down.Plan_Id, tcheckBatch, 8, DateTime.Now, total_Weight, total_Error); //PlanHelper.AddPlanReport(down, 0, tcheckBatch, tcheckWeight, error); } else { PlanHelper.UpdatePlanCompletedQuantity(down.Plan_Id, tcheckBatch, total_Weight, total_Error); //PlanHelper.AddPlanReport(down, 0, tcheckBatch, tcheckWeight, error); } //6检量超差记录 BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Read1Jc, new object[] { 6 }); } } #region MyRegion //MaterialBarCode mater = MaterialHelper.QueryMaterBarCode(barCode); //switch (mater.Bin_Serial) //{ // case 1: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station1_Barcode1, new object[] { state }); // break; // case 2: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station1_Barcode2, new object[] { state }); // break; // case 3: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station2_Barcode3, new object[] { state }); // break; // case 4: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station2_Barcode4, new object[] { state }); // break; // case 5: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station3_Barcode5, new object[] { state }); // break; // case 6: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station3_Barcode6, new object[] { state }); // break; // case 7: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station4_Barcode7, new object[] { state }); // break; // case 8: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station4_Barcode8, new object[] { state }); // break; // case 9: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station5_Barcode9, new object[] { state }); // break; // case 10: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station5_Barcode10, new object[] { state }); // break; // case 11: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station6_Barcode11, new object[] { state }); // break; // case 12: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station6_Barcode12, new object[] { state }); // break; // case 13: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station7_Barcode13, new object[] { state }); // break; // case 14: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station7_Barcode14, new object[] { state }); // break; // case 15: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station8_Barcode15, new object[] { state }); // break; // case 16: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station8_Barcode16, new object[] { state }); // break; // case 17: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station9_Barcode17, new object[] { state }); // break; // case 18: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station9_Barcode18, new object[] { state }); // break; // case 19: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station10_Barcode19, new object[] { state }); // break; // case 20: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station10_Barcode20, new object[] { state }); // break; // case 21: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station11_Barcode21, new object[] { state }); // break; // case 22: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station11_Barcode22, new object[] { state }); // break; // case 23: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station12_Barcode23, new object[] { state }); // break; // case 24: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station12_Barcode24, new object[] { state }); // break; // case 25: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station13_Barcode25, new object[] { state }); // break; // case 26: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station13_Barcode26, new object[] { state }); // break; // case 27: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station14_Barcode27, new object[] { state }); // break; // case 28: // BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_Station14_Barcode28, new object[] { state }); // break; //} #endregion } /// /// 溶剂 /// /// private void ReceiveData5(object serialPortobj) { try { SerialPort serialPort = (SerialPort)serialPortobj; string str = serialPort.ReadExisting(); if (str == null) { SetSolventBarCodeState(0); return; } if (dicCom5.Count == 0) { dicCom5.Add(1, str); } else { string barcode = dicCom5.SingleOrDefault(d => d.Key == 1).Value; if (str == barcode) { SetSolventBarCodeState(1); } else { SetSolventBarCodeState(2); } dicCom5.Clear(); } } catch (Exception ex) { throw; } } /// /// 溶剂扫码 /// /// private static void SetSolventBarCodeState(int state) { BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.plt_solvent_BarCode, new object[] { state }); } } }