public bool HsWriteToSSJ(TransportStr ts) { try { int start = int.Parse(ts.VAR3); byte[] buffer = new byte[18]; int[] info = new int[18]; #region 将任务号转为16进制,然后存入2字节 #endregion byte[] taskno = new byte[2]; taskno[0] = (byte)Convert.ToInt32(Convert.ToInt32(ts.ZXRWH) / 256); taskno[1] = (byte)Convert.ToInt32(Convert.ToInt32(ts.ZXRWH) % 256); byte[] barcode = new byte[4]; ts.TRAYCODE = string.IsNullOrEmpty(ts.TRAYCODE) ? "0" : ts.TRAYCODE; barcode[0] = (byte)Convert.ToInt32(Convert.ToInt32(ts.TRAYCODE) / (256 * 256 * 256)); barcode[1] = (byte)Convert.ToInt32(Convert.ToInt32(ts.TRAYCODE) / (256 * 256)); barcode[2] = (byte)Convert.ToInt32(Convert.ToInt32(ts.TRAYCODE) / 256); barcode[3] = (byte)Convert.ToInt32(Convert.ToInt32(ts.TRAYCODE) - barcode[0] * 256 * 256 * 256 - barcode[1] * 256 * 256 - barcode[2] * 256); for (int i = 0; i < 18; i++) { info[i] = 0; } for (int i = 0; i < taskno.Length; i++) { info[i] = int.Parse(taskno[i].ToString()); } for (int i = 0; i < barcode.Length; i++) { info[i + 2] = int.Parse(barcode[i].ToString()); } for (int i = 0; i < info.Length; i++) { buffer[i] = (byte)info[i]; } bool flag = false; if (HsPLCList.ContainsKey(ts.VAR1)) { flag = HsPLCList[ts.VAR1].HsWrite($"DB{dbnumber}.{start}", buffer); } else { HsPLCList.Add(ts.VAR1, new HsControlServer(ConfigurationManager.AppSettings[ts.VAR1])); } return(flag); } catch (Exception ex) { logWrite.WriteLog($"输送机{ts.SSJID}条码{ts.TRAYCODE}异常,异常信息为{ex.Message}"); return(false); } }
/// <summary> /// 初始化输送机对应参数 /// </summary> /// <returns></returns> public bool TransDeviceInit() { DataSet ds = DataTrans.D_GetDeviceSSJ(); if (ds != null && ds.Tables[0].Rows.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { //获取设备编号第一个字母,用于与绑定IP地址 _SSJDhead = ds.Tables[0].Rows[i]["VAR1"].ToString().Substring(0, 1); string _ip = ConfigurationManager.AppSettings[_SSJDhead].ToString(); //需要改进,将端口,DB块,端口等也进行构造函数初始化l if (!PLCList.ContainsKey(_SSJDhead)) { PLCList.Add(_SSJDhead, new RWLOPCServerl(_ip)); PLCFlag.Add(_SSJDhead, false); PLCList[_SSJDhead].Connect(); } if (!HsPLCList.ContainsKey(_SSJDhead)) { HsPLCList.Add(_SSJDhead, new HsControlServer(_ip)); bool flag = HsPLCList[_SSJDhead].HsServerConnect(); string res = flag ? "成功" : "失败"; NotifyEvent?.Invoke($"PLC{_ip}连接{res}"); } TransportStr tp = new TransportStr(); tp.SSJID = ds.Tables[0].Rows[i]["SSJID"].ToString(); tp.BTID = ds.Tables[0].Rows[i]["BTID"].ToString(); tp.DTYPE = ds.Tables[0].Rows[i]["DTYPE"].ToString(); tp.ZXRWH = ds.Tables[0].Rows[i]["ZXRWH"].ToString(); tp.DWXH = ds.Tables[0].Rows[i]["DWXH"].ToString(); tp.KXBZ = ds.Tables[0].Rows[i]["KXBZ"].ToString(); tp.TRAYCODE = ds.Tables[0].Rows[i]["TRAYCODE"].ToString(); tp.JYM = ds.Tables[0].Rows[i]["JYM"].ToString(); tp.BFLAG = ds.Tables[0].Rows[i]["BFLAG"].ToString(); tp.ALLEYID = ds.Tables[0].Rows[i]["ALLEYID"].ToString(); //tp.ALLEYID = ""; tp.VAR1 = ds.Tables[0].Rows[i]["VAR1"].ToString(); tp.VAR2 = ds.Tables[0].Rows[i]["VAR2"].ToString(); tp.VAR3 = ds.Tables[0].Rows[i]["VAR3"].ToString(); tp.VAR4 = ds.Tables[0].Rows[i]["VAR4"].ToString(); tp.VAR5 = ds.Tables[0].Rows[i]["VAR5"].ToString(); tp.SSRWLX = ds.Tables[0].Rows[i]["SSRWLX"].ToString(); tp.SSJIDhead = _SSJDhead; lsTransport.Add(tp); } return(true); } return(false); }