<%@ WebHandler Language="C#" Class="autoPacking6151" %> using System; using System.Web; using System.Data; using DK.XuWei.WebMes; using Curtain.DataAccess; using Newtonsoft.Json.Linq; /// /// PLC对接使用 /// 带参数barcode,扫描头处理逻辑; /// 不带参数barcode,轮询处理逻辑; /// xuwei add 2023-12-31 /// public class autoPacking6151 : IHttpHandler { public static class PLC { public static string plcIp = "172.18.36.95"; public static int plcPort = 102; public static string dbBarcode = "151.2"; //151.0 => 151.2 public static string dbGoodsId = "151.14"; public static string dbGoodsNum = "151.16"; public static string dbResult = "151.18"; public static string dbLogoId = "151.22";//替换产品等级 public static string dbMaterialcode = "151.30"; public static string dbRead = "151.26"; public static string[] dbBarcodeBlock = { "150.2", "150.32", "150.62", "150.92", "150.122", "150.152", "150.182", "150.212", "150.242", "150.272", "150.302", "150.332" }; public static string dbBanMa = "150.362";//150.360 => 150.362 public static string dbUsercode = "150.376"; //150.374 => 150.376 public static string dbGoodsCodes = "151.46"; //151.44 => 151.46 } public static class API { public static string accountCode = "dongke"; public static string userId = "1529"; public static string userCode = "DONGKEXW"; public static string userName = "徐伟"; public static string userPassword = "D63AC55256F8048265387A2B0388F1D2"; public static string sessionKey = "C2A5AE55-8E9B-45F9-A0AF-6A5A1BF3457C"; public static int procedureID = 107; //3#包装 public static bool debug = false; } public bool resultFlag = false; public JArray result = new JArray(); public S7 s7 = new S7(); //打开PLC public int PlcOpen() { try { s7.Open(PLC.plcIp, PLC.plcPort); resultFlag = true; result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接成功!"))); } catch (Exception e) { resultFlag = false; result.Add(new JObject(new JProperty("PLC连接", PLC.plcIp + ":" + PLC.plcPort + " 连接失败!"))); result.Add(new JObject(new JProperty("PLC错误信息", e.Message))); } return 1; } //关闭PLC public int PlcClose() { s7.Close(); result.Add(new JObject(new JProperty("PLC关闭", PLC.plcIp + ":" + PLC.plcPort + " 关闭成功!"))); return 1; } //读取PLC标识 public int PlcReadReadFlag() { if (!resultFlag) return -1; try { int read = Convert.ToInt16(s7.Read(PLC.dbRead)); result.Add(new JObject(new JProperty("PLC读取(读取标识:OK)", PLC.dbRead + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //读取PLC产品ID public int PlcReadGoodsid() { if (!resultFlag) return -1; try { int read = Convert.ToInt16(s7.Read(PLC.dbGoodsId)); result.Add(new JObject(new JProperty("PLC读取(产品ID:OK)", PLC.dbGoodsId + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //读取装板数量 public int PlcReadGoodsnum() { if (!resultFlag) return -1; try { int read = Convert.ToInt16(s7.Read(PLC.dbGoodsNum)); result.Add(new JObject(new JProperty("PLC读取(装板数量:OK)", PLC.dbGoodsNum + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //读取PLC商标ID public int PlcReadLogoid() { if (!resultFlag) return -1; try { int read = Convert.ToInt16(s7.Read(PLC.dbLogoId)); result.Add(new JObject(new JProperty("PLC读取(产品ID:OK)", PLC.dbLogoId + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } public string[] PlcReadBarcodes(int goodsNum) { if (!resultFlag) return null; try { string[] barCodes = new string[12]; JObject obj = new JObject(); for (int i = 0; i < goodsNum; i++) { barCodes[i] = s7.Read(PLC.dbBarcodeBlock[i], 11).ToString(); obj.Add(new JProperty("条码" + (i + 1).ToString().PadLeft(2, '0') + "(" + PLC.dbBarcodeBlock[i] + ")", barCodes[i])); } result.Add(new JObject(new JProperty("PLC读取(产品条码:OK)", obj))); return barCodes; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return null; } } //读取板码 public string PlcReadBanma() { if (!resultFlag) return ""; try { string read = s7.Read(PLC.dbBanMa, 8).ToString(); result.Add(new JObject(new JProperty("PLC读取(板码:OK)", PLC.dbBanMa + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return ""; } } //读取物料号 public string PlcReadMaterialcode() { if (!resultFlag) return ""; try { string read = s7.Read(PLC.dbMaterialcode, 14).ToString(); result.Add(new JObject(new JProperty("PLC读取(物料号:OK)", PLC.dbMaterialcode + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return ""; } } //读取结果标识 public int PlcReadResultNum() { if (!resultFlag) return -1; try { int read = Convert.ToInt32(s7.Read(PLC.dbResult)); result.Add(new JObject(new JProperty("PLC读取(结果:OK)", PLC.dbResult + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //读取USERCODE public string PlcReadUsercode() { if (!resultFlag) return ""; try { string read = s7.Read(PLC.dbUsercode,6).ToString(); result.Add(new JObject(new JProperty("PLC读取(USECODE:OK)", PLC.dbUsercode + " = " + read.ToString()))); return read; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return ""; } } //写入条码 public int PlcWriteBarcode(string barCode) { if (!resultFlag) return -1; try { s7.Write(PLC.dbBarcode, barCode); result.Add(new JObject(new JProperty("PLC写入(条码:OK)", PLC.dbBarcode + " = " + barCode))); resultFlag = true; return 1; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //写入物料码 public int PlcWriteMaterialcode(string materialCode) { if (!resultFlag) return -1; try { s7.Write(PLC.dbMaterialcode, materialCode); result.Add(new JObject(new JProperty("PLC写入(物料码:OK)", PLC.dbMaterialcode + " = " + materialCode))); resultFlag = true; return 1; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //写入产品型号 public int PlcWriteGoodsCodes(string goodsCodes) { if (!resultFlag) return -1; try { s7.Write(PLC.dbGoodsCodes, goodsCodes); result.Add(new JObject(new JProperty("PLC写入(产品型号:OK)", PLC.dbGoodsCodes + " = " + goodsCodes))); resultFlag = true; return 1; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //写入产品ID public int PlcWriteGoodsid(Int16 goodsId) { if (!resultFlag) return -1; try { s7.Write(PLC.dbGoodsId, goodsId); result.Add(new JObject(new JProperty("PLC写入(产品ID:OK)", PLC.dbGoodsId + " = " + goodsId))); resultFlag = true; return 1; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //写入商标ID public int PlcWriteLogoid(Int16 logoId) { if (!resultFlag) return -1; try { s7.Write(PLC.dbLogoId, logoId); result.Add(new JObject(new JProperty("PLC写入(商标ID:OK)", PLC.dbLogoId + " = " + logoId))); resultFlag = true; return 1; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //写入装板数量 public int PlcWriteGoodsnum(Int16 goodsNum) { if (!resultFlag) return -1; try { s7.Write(PLC.dbGoodsNum, goodsNum); result.Add(new JObject(new JProperty("PLC写入(装板数量:OK)", PLC.dbGoodsNum + " = " + goodsNum))); resultFlag = true; return 1; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERR", e.Message))); resultFlag = false; return -1; } } //写入执行结果 // 1 通过 2 条码格式错误 3 产品不可到达 4 MES异常 5 条码不一致 6 绑板完成 7 工号读取错误 public int PlcWriteResult(Int16 flag) { try { string message = ""; switch(flag) { case 1:message = "扫码通过:OK";break; case 2:message = "条码格式错误:ERR";break; case 3:message = "产品不可到达:ERR";break; case 4:message = "MES异常:ERR";break; case 5:message = "条码不一致:ERR";break; case 6:message = "绑板完成:OK";break; case 7:message = "工号读取错误:ERR";break; default:message = "未定义:ERR";break; } s7.Write(PLC.dbResult, flag); result.Add(new JObject(new JProperty("PLC写入("+message+")", PLC.dbResult + " = " + flag.ToString()))); return 1; } catch (Exception e) { result.Add(new JObject(new JProperty("PLC错误信息:ERROR", e.Message))); return -1; } } //加载默认参数 public static JObject ApiLoadDefaultPara() { JObject userPara = new JObject(); userPara.Add(new JProperty("accountCode", API.accountCode)); userPara.Add(new JProperty("userCode", API.userCode)); userPara.Add(new JProperty("userPassword", API.userPassword)); userPara.Add(new JProperty("sessionKey", API.sessionKey)); return userPara; } //PDA接口验证条码 public int ApiGetStatusBarcode(string barCode) { if (!resultFlag) return -1; try { WCF wcf = new WCF(); wcf.Para = ApiLoadDefaultPara(); wcf.Para.Add(new JProperty("procedureID", API.procedureID)); wcf.Para.Add(new JProperty("barCode", barCode)); //测试用 string jsonStr = @" { 'd': { '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels', 'Message': '读取成功!', 'Result': '{\'data\':[{\'STATUS\':0.0}]}', 'Status': 0 } } ".Replace("'","\""); //正式调用 if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/GetStatusByBarcode"); JObject json = JObject.Parse(jsonStr); if (json["d"]["Status"].ToString() == "0") { result.Add(new JObject( new JProperty("API调用(条码状态:OK)", "操作成功!"), new JProperty("接口", "/DKService/PDAModuleService/GetStatusByBarcode"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(barCode)", barCode) //,new JProperty("结果", jsonStr) ) ); return 1; } else { resultFlag = false; result.Add(new JObject( new JProperty("API调用(条码状态:ERR)", json["d"]["Message"].ToString()), new JProperty("接口", "/DKService/PDAModuleService/GetStatusByBarcode"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(barCode)", barCode) //,new JProperty("结果", jsonStr) ) ); return -1; } } catch (Exception e) { resultFlag = false; result.Add(new JObject( new JProperty("API调用(条码状态:ERROR)", "系统异常!"), new JProperty("接口", "/DKService/PDAModuleService/GetStatusByBarcode"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(barCode)", barCode), new JProperty("异常", e.Message) ) ); return -1; } } public int ApiCheckBarcode(string barCode) { if (!resultFlag) return -1; try { WCF wcf = new WCF(); wcf.Para = ApiLoadDefaultPara(); wcf.Para.Add(new JProperty("procedureID", API.procedureID)); wcf.Para.Add(new JProperty("barcode", barCode)); //测试用 string jsonStr = @" { 'd': { '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels', 'Message': '操作成功', 'Result': '[{\'out_errMsg\':\'\',\'out_goodsID\':\'179\',\'out_goodsCode\':\'H0175M\',\'out_goodsName\':\'HC0175PT-305\',\'out_groutingUserCode\':\'SFC035\',\'out_groutingUserName\':null,\'out_groutingUserID\':null,\'out_groutingNum\':\'73\',\'out_mouldCode\':\'C06B11-001\',\'out_ispublicbody\':\'0\',\'out_ispublicbodyTrach\':\'\',\'out_groutingdate\':\'2023/12/30 0:00:00\',\'out_specialRepairFlag\':\'0\',\'out_isReFire\':\'0\',\'out_isLengBu\':\'0\',\'out_missFlag\':\'0\',\'out_logoID\':\'15\',\'out_logoCode\':\'020\',\'out_logoName\':\'HEGII(新)\',\'out_MaterialCode\':\'CT175PD2210B01\',\'out_glazeName\':\'智洁釉\',\'out_deliverLimitCycle\':null,\'out_barcode\':\'00000000001\',\'out_WaterLabelCode\':null,\'out_CodeCheckFlag\':null,\'out_LeakFlag1\':\'\',\'out_LeakFlag2\':\'\',\'out_LeakFlag3\':\'\',\'out_specialRepairFlagName\':\'否\',\'out_isReFireName\':\'否\',\'out_LeakFlag1Name\':\'未检测\',\'out_LeakFlag2Name\':\'未检测\',\'out_LeakFlag3Name\':\'未检测\',\'out_lengBuName\':\'否\',\'out_LeakFlag4\':\'\',\'out_LeakFlag5\':\'\',\'out_LeakFlag4Name\':\'未检测\',\'out_LeakFlag5Name\':\'未检测\',\'GOODSMODELforCheck\':\'CT175PD2210B01#0\',\'PlateLimitNum\':12,\'DefectFlagID\':null,\'pdid\':null,\'InspectionLevel\':\'\',\'PackingDefect\':\'\',\'InspectionGoodsLevel\':\'\',\'offlineFlag\':\'0\',\'recyclingFlag\':\'0\',\'waterLabelCode\':\'http://wl.bbqk.com/2bwta/0.html\',\'PLCWeight\':0.0}]', 'Status': 0 } } ".Replace("'","\""); //正式用 if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/CheckBarcode"); JObject json = JObject.Parse(jsonStr); JArray jsonArray = JArray.Parse(json["d"]["Result"].ToString()); if (json["d"]["Status"].ToString() == "0" && jsonArray[0]["out_errMsg"].ToString()== "") { result.Add(new JObject( new JProperty("API调用(条码验证:OK)", "操作成功!"), new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(barcode)", barCode) //,new JProperty("结果", jsonStr) ) ); return 1; } else { resultFlag = false; result.Add(new JObject( new JProperty("API调用(条码验证:ERR)", json["d"]["Message"].ToString()), new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(barcode)", barCode) ,new JProperty("结果", jsonStr) ) ); return -1; } } catch (Exception e) { resultFlag = false; result.Add(new JObject( new JProperty("API调用(条码验证:ERROR)", "系统异常!"), new JProperty("接口", "/DKService/PDAModuleService/CheckBarcode"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(barcode)", barCode), new JProperty("异常", e.Message) ) ); return -1; } } public int ApiFinishedLoadingCar(string barCode,int logoId,int goodsId,int banMa = 0) { if (!resultFlag) return -1; string jsonData = "{\"logoID\":\"" + logoId.ToString() + "\",\"GoodsID\":\"" + goodsId.ToString() + "\"}"; try { WCF wcf = new WCF(); wcf.Para = ApiLoadDefaultPara(); wcf.Para.Add(new JProperty("procedureID", API.procedureID)); wcf.Para.Add(new JProperty("barcode", barCode)); wcf.Para.Add(new JProperty("module", "FinishedLoadingCar")); wcf.Para.Add(new JProperty("action", "GetSetting")); wcf.Para.Add(new JProperty("jsonData", jsonData)); //测试用 string jsonStr = @" { 'd': { '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels', 'Message': '操作成功', 'Result': '{\'GoodsID\':\'179\',\'logoID\':\'15\',\'S_PM_011\':\'1\',\'S_PM_012\':\'1\',\'S_PM_013\':\'1\',\'PlatelitNum\':12,\'PlatelitNumNew\':2}', 'Status': 0 } } ".Replace("'","\""); //正式用 if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/DoAction"); JObject json = JObject.Parse(jsonStr); if (json["d"]["Status"].ToString() == "0") { JObject goods = JObject.Parse(json["d"]["Result"].ToString()); int goodsNum = 0; //新板 if(banMa == 1) goodsNum = Convert.ToInt32(goods["PlatelitNumNew"]); //旧板 if(banMa == 0) goodsNum = Convert.ToInt32(goods["PlatelitNum"]); result.Add(new JObject( new JProperty("API调用(装板数量:OK)", goodsNum.ToString() + "操作成功!"), new JProperty("接口", "/DKService/PDAModuleService/DoAction"), new JProperty("参数(ACTION)", "GetSetting"), new JProperty("参数(MODULE)", "FinishedLoadingCar"), new JProperty("参数(jsonData)",jsonData) //,new JProperty("结果", jsonStr) ) ); return goodsNum; } else { resultFlag = false; result.Add(new JObject( new JProperty("API调用(装板数量:ERR)", json["d"]["Message"].ToString()), new JProperty("接口", "/DKService/PDAModuleService/DoAction"), new JProperty("参数(ACTION)", "GetSetting"), new JProperty("参数(MODULE)", "FinishedLoadingCar"), new JProperty("参数(jsonData)",jsonData) //,new JProperty("结果", jsonStr) ) ); return -1; } } catch (Exception e) { resultFlag = false; result.Add(new JObject( new JProperty("API调用(装板数量:ERROR)", "系统异常!"), new JProperty("接口", "/DKService/PDAModuleService/DoAction"), new JProperty("参数(ACTION)", "GetSetting"), new JProperty("参数(MODULE)", "FinishedLoadingCar"), new JProperty("参数(jsonData)",jsonData), new JProperty("异常", e.Message) ) ); return -1; } } public int ApiAddWorkPiece(string[] barCodes, string banMa, int goodsNum) { if (!resultFlag) return -1; JArray productionDataEntitys = new JArray(); for (int i = 0; i < goodsNum; i++) { JObject obj = new JObject( new JProperty("UserID", API.userId), new JProperty("UserCode", API.userCode), new JProperty("UserName", API.userName), new JProperty("Barcode", barCodes[i]), new JProperty("BanMa", banMa) ); productionDataEntitys.Add(obj); } try { WCF wcf = new WCF(); wcf.Para = ApiLoadDefaultPara(); wcf.Para.Add(new JProperty("procedureID", API.procedureID)); wcf.Para.Add(new JProperty("productionDataEntitys", productionDataEntitys)); //测试用 string jsonStr = @" { 'd': { '__type': 'ActionResult:#Dongke.IBOSS.PRD.WCF.DataModels', 'Message': '操作成功', 'Result': '[{\'out_errMsg\':\'\',\'out_goodsID\':\'179\',\'out_goodsCode\':\'H0175M\',\'out_goodsName\':\'HC0175PT-305\',\'out_groutingUserCode\':\'SFC035\',\'out_groutingUserName\':null,\'out_groutingUserID\':null,\'out_groutingNum\':null,\'out_mouldCode\':null,\'out_ispublicbody\':null,\'out_ispublicbodyTrach\':null,\'out_groutingdate\':\'\',\'out_specialRepairFlag\':null,\'out_isReFire\':null,\'out_isLengBu\':null,\'out_missFlag\':null,\'out_logoID\':null,\'out_logoCode\':\'\',\'out_logoName\':\'\',\'out_MaterialCode\':null,\'out_glazeName\':null,\'out_deliverLimitCycle\':null,\'out_barcode\':null,\'out_WaterLabelCode\':null,\'out_CodeCheckFlag\':null,\'out_LeakFlag1\':null,\'out_LeakFlag2\':null,\'out_LeakFlag3\':null,\'out_specialRepairFlagName\':null,\'out_isReFireName\':null,\'out_LeakFlag1Name\':null,\'out_LeakFlag2Name\':null,\'out_LeakFlag3Name\':null,\'out_lengBuName\':null,\'out_LeakFlag4\':null,\'out_LeakFlag5\':null,\'out_LeakFlag4Name\':null,\'out_LeakFlag5Name\':null,\'GOODSMODELforCheck\':null,\'PlateLimitNum\':null,\'DefectFlagID\':null,\'pdid\':null,\'InspectionLevel\':null,\'PackingDefect\':null,\'InspectionGoodsLevel\':null,\'offlineFlag\':null,\'recyclingFlag\':null,\'waterLabelCode\':null}]', 'Status': 0 } } ".Replace("'","\""); //正式用 if (!API.debug) jsonStr = wcf.Post("/DKService/PDAModuleService/AddWorkPiece"); JObject json = JObject.Parse(jsonStr); JArray jsonArray = JArray.Parse(json["d"]["Result"].ToString()); //此处容错处理,如果已经绑定完成,重复绑码则视为完成 if (json["d"]["Status"].ToString() == "0" && (jsonArray[0]["out_errMsg"].ToString()== "" || jsonArray[0]["out_errMsg"].ToString().IndexOf("已经完成生产") >= 0)) { result.Add(new JObject( new JProperty("API调用(包装码垛:OK)", "操作成功!"), new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(productionDataEntitys)", productionDataEntitys) //,new JProperty("结果", jsonStr) ) ); return 1; } else { resultFlag = false; result.Add(new JObject( new JProperty("API调用(包装码垛:ERR)", json["d"]["Message"].ToString()), new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(productionDataEntitys)", productionDataEntitys) ,new JProperty("结果", jsonStr) ) ); return -1; } } catch (Exception e) { resultFlag = false; result.Add(new JObject( new JProperty("API调用(包装码垛:ERROR)", "系统异常!"), new JProperty("接口", "/DKService/PDAModuleService/AddWorkPiece"), new JProperty("参数(procedureID)", API.procedureID), new JProperty("参数(productionDataEntitys)", productionDataEntitys), new JProperty("异常", e.Message) ) ); return -1; } } public DataRow GetGoods(string outCode) { try { using (IDataAccess conn = DataAccess.Create()) { DataTable dt = conn.ExecuteDatatable(@" SELECT gd.OUTLABELCODE,gd.BARCODE,gd.GOODSID,gd.LOGOID,gd.MATERIALCODE,gd.GOODSCODE AS GOODSCODES FROM TP_PM_GROUTINGDAILYDETAIL gd WHERE gd.VALUEFLAG = '1' AND gd.OUTLABELCODE = @OUTLABELCODE@ ", new CDAParameter("OUTLABELCODE", outCode) ); if(dt.Rows.Count >0 ) { JObject obj = new JObject(); obj.Add(new JProperty("OUTLABELCODE", dt.Rows[0]["OUTLABELCODE"].ToString())); obj.Add(new JProperty("BARCODE", dt.Rows[0]["BARCODE"].ToString())); obj.Add(new JProperty("MATERIALCODE", dt.Rows[0]["MATERIALCODE"].ToString())); obj.Add(new JProperty("GOODSID", dt.Rows[0]["GOODSID"].ToString())); obj.Add(new JProperty("LOGOID", dt.Rows[0]["LOGOID"].ToString())); obj.Add(new JProperty("GOODSCODES", dt.Rows[0]["GOODSCODES"].ToString())); result.Add(new JObject(new JProperty("MES读取(产品信息:OK)", obj))); return dt.Rows[0]; } else { resultFlag = false; result.Add(new JObject(new JProperty("MES读取(产品信息:ERR)","产品未找到!"))); return null; } } } catch (Exception e) { resultFlag = false; result.Add(new JObject(new JProperty("系统异常", e.Message))); return null; } } //读取用户信息 public DataRow GetUserinfo(string userCode) { try { using (IDataAccess conn = DataAccess.Create()) { DataTable dt = conn.ExecuteDatatable(@" SELECT u.ACCOUNTCODE, u.USERID, u.USERCODE, u.USERNAME, u.PASSWORD, l.SESSIONKEY FROM TP_MST_USER u LEFT JOIN TP_MST_USERLOGIN l ON u.USERID = l.USERID WHERE u.USERCODE = @USERCODE@ ", new CDAParameter("USERCODE", userCode) ); if(dt.Rows.Count >0 ) { API.accountCode = dt.Rows[0]["ACCOUNTCODE"].ToString(); API.userId = dt.Rows[0]["USERID"].ToString(); API.userCode = dt.Rows[0]["USERCODE"].ToString(); API.userName = dt.Rows[0]["USERNAME"].ToString(); API.userPassword = dt.Rows[0]["PASSWORD"].ToString(); API.sessionKey = dt.Rows[0]["SESSIONKEY"].ToString(); JObject obj = new JObject(); obj.Add(new JProperty("accountCode", dt.Rows[0]["ACCOUNTCODE"].ToString())); obj.Add(new JProperty("userId", dt.Rows[0]["USERID"].ToString())); obj.Add(new JProperty("userCode", dt.Rows[0]["USERCODE"].ToString())); obj.Add(new JProperty("userName", dt.Rows[0]["USERNAME"].ToString())); obj.Add(new JProperty("userPassword", dt.Rows[0]["PASSWORD"].ToString())); obj.Add(new JProperty("sessionKey", dt.Rows[0]["SESSIONKEY"].ToString())); result.Add(new JObject(new JProperty("MES读取(用户信息:OK)", obj))); return dt.Rows[0]; } else { resultFlag = false; result.Add(new JObject(new JProperty("MES读取(用户信息:ERR)","用户未找到!"))); return null; } } } catch (Exception e) { resultFlag = false; result.Add(new JObject(new JProperty("系统异常:ERROR", e.Message))); return null; } } //主程序入口 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request["barcode"] is object) { BarcodeRead(context); } else { BarcodeDo(context); } } // 读取条码,对接扫描头使用 public void BarcodeRead(HttpContext context) { if (PlcOpen() == 1) { int readFlag = PlcReadReadFlag(); string barCode = ""; string materialCode = ""; string goodsCodes = ""; Int16 goodsId = 0; Int16 logoId = 0; Int16 goodsNum = 0; //条码处理 if(readFlag == 1 || readFlag == 0 || readFlag == 10) { DataRow dr = GetGoods(context.Request["barcode"].ToString()); if (resultFlag) { barCode = dr["BARCODE"].ToString(); materialCode = dr["MATERIALCODE"].ToString(); goodsCodes = dr["GOODSCODES"].ToString(); goodsId = Convert.ToInt16(dr["GOODSID"]); logoId = Convert.ToInt16(dr["LOGOID"]); //读取工号 string userCode = PlcReadUsercode(); if (userCode != "") { GetUserinfo(userCode); //接口验证条码状态(非必须取消这个接口) //int status = ApiGetStatusBarcode(barCode); //接口验证可到达 int check = ApiCheckBarcode(barCode); //写验证结果 if (check == -1) PlcWriteResult(3); } else { //工号错误 PlcWriteResult(7); } } } //首个条码处理 if ((readFlag == 1 || readFlag == 10) && resultFlag) { //写PLC PlcWriteBarcode(barCode); PlcWriteMaterialcode(materialCode); PlcWriteGoodsCodes(goodsCodes); //PlcWriteGoodsid(goodsId); //PlcWriteLogoid(logoId); //获取装板数量 if(readFlag == 1) goodsNum = Convert.ToInt16(ApiFinishedLoadingCar(barCode, logoId, goodsId,1)); if(readFlag == 10) goodsNum = Convert.ToInt16(ApiFinishedLoadingCar(barCode, logoId, goodsId,0)); PlcWriteGoodsnum(goodsNum); //写结果 if (resultFlag) PlcWriteResult(1); else PlcWriteResult(4); } //过程条码处理 if (readFlag == 0 && resultFlag) { //验证条码是否符合装板 int plcGoodsnum = PlcReadGoodsnum(); string plcMaterialcode = PlcReadMaterialcode(); if (resultFlag) { if(materialCode == plcMaterialcode) { //写PLC PlcWriteBarcode(barCode); PlcWriteMaterialcode(materialCode); PlcWriteGoodsCodes(goodsCodes); //PlcWriteGoodsid(goodsId); //PlcWriteLogoid(logoId); //PlcWriteGoodsnum(goodsNum); //通过 或 异常 if (resultFlag) PlcWriteResult(1); else PlcWriteResult(4); } else { //条码不一致 PlcWriteResult(5); } } else { //异常 PlcWriteResult(4); } } PlcClose(); } context.Response.Write(new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson()); } // 绑板处理,轮询到标识,绑板处理 public void BarcodeDo(HttpContext context) { if (PlcOpen() == 1) { int readFlag = PlcReadReadFlag(); int goodsNum = PlcReadGoodsnum(); int resultNum = PlcReadResultNum(); if(resultNum != 6) { //绑板处理 if(readFlag == 2 || readFlag == 3) { //读取工号 string userCode = PlcReadUsercode(); if (userCode != "") { //读取工号信息 GetUserinfo(userCode); //读取条码 String[] barCodes = PlcReadBarcodes(goodsNum); string banMa = ""; if (readFlag == 3) banMa = PlcReadBanma(); //绑板处理 int addWorkPiece = ApiAddWorkPiece(barCodes, banMa, goodsNum); if(addWorkPiece == 1) { //绑板完成 PlcWriteResult(6); } else { //MES异常 绑定异常 PlcWriteResult(4); } } else { //工号错误 PlcWriteResult(7); } } //强制绑板完成 if(readFlag == 4) { PlcWriteResult(6); } } PlcClose(); } context.Response.Write(new JsonResult(resultFlag ? JsonStatus.success : JsonStatus.error) { rows = result }.ToJson()); } public bool IsReusable { get { return false; } } }