/// <summary> /// API 函数初始化 /// </summary> /// <returns></returns> private int APIOpen() { if (!bAPIOpen) { int ret = 0; try { ret = Dapapi.AB_API_Open(); if (ret < 0) { return(-1); } else { bAPIOpen = true; return(1); } } catch (Exception e) { //throw new Exception(e.Message); } } return(0); }
/// <summary> /// 发送订单到数码管 /// </summary> public void SetOrderNixielight(SortingLineTask[] sortingLineTasks) { //熄灭所有标签 TagClear(); if (Tags == null) { Tags = new Dictionary <int, Tag>(); } //int sortinglineboxcount = SortingLineBoxList.GetLineBoxList().Count; try { //foreach (SortingLineBox sortingLineBox in SortingLineBoxList.GetLineBoxList()) //{ // Tags.Add(Convert.ToInt32(sortingLineBox.LineBoxCode), new Tag("0", Convert.ToInt32(sortingLineBox.LineBoxCode), 0)); //} SortingSubLine[] sortingSubLineList = SortingSubLineList.GetSubSortingLineList().OrderBy(o => o.sequence).ToArray(); //初始化标签的内容 for (int j = 1; j < sortingLineTasks.Length; j++) { if (sortingLineTasks[j] != null) { //当前子线包含的所有任务明细列表 IEnumerable <SortingLineTaskDetail> sortingLineTaskDetails = sortingLineTasks[j].SortingLineTaskDetails.GetAreaDetails(sortingSubLineList[j - 1].sublineCode); foreach (SortingLineTaskDetail detail in sortingLineTaskDetails) { try { Tags.Add(Convert.ToInt32(detail.LINEBOXCODE), new Tag(sortingLineTasks[j].ID, Convert.ToInt32(detail.LINEBOXCODE), detail.QTY)); //Tags[Convert.ToInt32(detail.LINEBOXCODE)] = new Tag(sortingLineTasks[j].ID, // Convert.ToInt32(detail.LINEBOXCODE), detail.QTY); } catch (Exception) { } } } } //发送标签内容 foreach (KeyValuePair <int, Tag> keyValuePair in Tags) { Dapapi.AB_LB_DspNum(GWID[0], (short)keyValuePair.Key, keyValuePair.Value.Qty, iDigitPoint, iLEDInterval); } } catch (Exception) { } }
/// <summary> /// 获取控制器状态 /// </summary> private int GetGWStatus() { bool bGoOn; int ret, timeStart; for (int i = 0; i < GWCount; i++) { Dapapi.AB_GW_Open(GWID[i]); ret = Dapapi.AB_GW_Status(GWID[i]); if (ret != 7) { bGoOn = true; timeStart = System.Environment.TickCount; while (bGoOn) { ret = Dapapi.AB_GW_Status(GWID[i]); if (ret == 7) { bGoOn = false; } else if (System.Environment.TickCount - timeStart > 3000) { bGoOn = false; } } } if (ret == 7) { //写日志 MonitorLog monitorLog = MonitorLog.NewMonitorLog(); monitorLog.LOGNAME = "设备初始化"; monitorLog.LOGINFO = "电子标签串口路由ID:" + GWID[i] + " 连接成功, 状态 :" + ret; monitorLog.LOGLOCATION = "设备"; monitorLog.LOGTYPE = 0; monitorLog.Save(); return(1); } else { MessageBox.Show("电子标签串口路由ID:" + GWID[i] + " 连接失败, 状态 :" + ret); //写日志 MonitorLog monitorLog = MonitorLog.NewMonitorLog(); monitorLog.LOGNAME = "设备初始化"; monitorLog.LOGINFO = "电子标签串口路由ID:" + GWID[i] + " 连接失败, 状态 :" + ret; monitorLog.LOGLOCATION = "设备"; monitorLog.LOGTYPE = 0; monitorLog.Save(); } } return(0); }
/// <summary> /// API注销 /// </summary> /// <returns></returns> private int APIClose() { if (bAPIOpen) { Dapapi.AB_API_Close(); bAPIOpen = false; return(1); } else { return(0); } }
/// <summary> /// 清空消息对列 /// </summary> private void ClearGWQuene() { byte[] cData = new byte[200]; int gwid; short tagNode, subCmd, msgType, dataCnt; gwid = 0; tagNode = 0; subCmd = -1; msgType = 0; dataCnt = 0; while (Dapapi.AB_Tag_RcvMsg(ref gwid, ref tagNode, ref subCmd, ref msgType, cData, ref dataCnt) > 0) { } }
/// <summary> /// 熄灭所有标签 /// </summary> private void TagClear() { for (int i = 0; i < GWCount; i++) { if (Dapapi.AB_GW_Status(GWID[i]) == 7) { Dapapi.AB_LB_DspNum(GWID[i], -252, 0, 0, -3); Dapapi.AB_LB_DspNum(GWID[i], 252, 0, 0, -3); Dapapi.AB_LED_Dsp(GWID[i], -252, 0, 0); Dapapi.AB_LED_Dsp(GWID[i], 252, 0, 0); Dapapi.AB_BUZ_On(GWID[i], -252, 0); Dapapi.AB_BUZ_On(GWID[i], 252, 0); Dapapi.AB_LB_DspStr(GWID[i], -252, "", 0, -3); Dapapi.AB_LB_DspStr(GWID[i], 252, "", 0, -3); //12-digits Alphanumerical display Dapapi.AB_AHA_ClrDsp(GWID[i], -252); Dapapi.AB_AHA_ClrDsp(GWID[i], 252); Dapapi.AB_AHA_BUZ_On(GWID[i], -252, 0); Dapapi.AB_AHA_BUZ_On(GWID[i], 252, 0); } } }
/// <summary> /// 接收标签信息 /// </summary> private void RcvMsg() { int gwid, ret; short tagNode, subCmd, msgType, dataCnt; short gwPort, keyType, maxTag; byte[] rcvData = new byte[200]; Dapapi.Tccb ccb_data; string tmpStr, rcvStr; gwid = 0; //all gateway tagNode = 0; //all tagnode subCmd = -1; //all subcmd msgType = 0; dataCnt = 200; Dapapi.AB_GW_Status(0); ret = Dapapi.AB_Tag_RcvMsg(ref gwid, ref tagNode, ref subCmd, ref msgType, rcvData, ref dataCnt); if (ret > 0) { rcvStr = System.Text.Encoding.Default.GetString(rcvData); if (tagNode < 0) { gwPort = 1; } else { gwPort = 2; } tmpStr = "GW_ID:" + gwid + ",GW Port:" + gwPort + ",TagNode:" + System.Math.Abs(tagNode) + ",SubCmd:" + subCmd + ",Data:" + rcvStr; switch (subCmd) { case SUMCMD_CONFIRM_BUTTON: //写日志 monitorLog = MonitorLog.NewMonitorLog(); monitorLog.LOGNAME = "接收到标签信号"; monitorLog.LOGINFO = tmpStr; monitorLog.LOGLOCATION = "设备"; monitorLog.LOGTYPE = 0; monitorLog.Save(); //ShowMsg(tmpStr); if (Tags.ContainsKey(System.Math.Abs(Convert.ToInt32(tagNode)))) { Tags[System.Math.Abs(Convert.ToInt32(tagNode))].Qty = 0; } break; default: monitorLog = MonitorLog.NewMonitorLog(); monitorLog.LOGNAME = "设备初始化"; monitorLog.LOGINFO = tmpStr; monitorLog.LOGLOCATION = "设备"; monitorLog.LOGTYPE = 0; monitorLog.Save(); break; } } }
/// <summary> /// 控制器打开 /// </summary> /// <returns></returns> private int Dap_Open() { int posspace, postab, pos; //txAddrList.Clear(); GWCount = 0; if (!System.IO.File.Exists("IPINDEX")) { MessageBox.Show("串口路由地址文件不存在!"); return(-1); } try { using (System.IO.StreamReader sr = new System.IO.StreamReader("IPINDEX")) { String line; // Read and display lines from the file until the end of // the file is reached. while ((line = sr.ReadLine()) != null) { GWCount++; posspace = line.IndexOf(" "); //find space postab = line.IndexOf((char)9); //find tab if (posspace <= 0) { posspace = postab; } if (postab <= 0) { postab = posspace; } pos = System.Math.Min(posspace, postab); if (pos <= 0) { MessageBox.Show("串口路由地址文件格式错误!"); return(-1); } //txAddrList.AppendText(line + "\r\n"); GWID[GWCount - 1] = int.Parse(line.Substring(0, pos)); diagnosis[GWCount - 1].Init(2); } } } catch (Exception e) { MessageBox.Show("读取串口路由地址文件失败!" + e.Message); return(-1); } if (APIOpen() < 0) { MessageBox.Show("初始化串口API失败!"); return(-1); } //写日志 MonitorLog monitorLog = MonitorLog.NewMonitorLog(); monitorLog.LOGNAME = "设备初始化"; monitorLog.LOGINFO = "初始化串口API成功"; monitorLog.LOGLOCATION = "设备"; monitorLog.LOGTYPE = 0; monitorLog.Save(); //ShowMsg("API Open Success!"); for (int i = 0; i < GWCount; i++) { if (Dapapi.AB_GW_Open(GWID[i]) < 0) { MessageBox.Show("串口控制器'" + GWID[i] + "'打开失败!"); } } return(1); }