public void ReadCoil() { try { InputModule input = new InputModule(); input.bySlaveID = m_SlaveID; input.byFunction = Modbus.byREAD_COIL; input.nStartAddr = 0; input.nDataLength = 30; OutputModule rev = m_Modbus.SendMessage_Sync(input); int count = 0; for (; ;) { if (count > 3) { throw new Exception("通信异常,读取自动运行线圈失败!"); } if ((rev == null) || (rev.byFunction != input.byFunction)) { rev = m_Modbus.SendMessage_Sync(input); count++; Thread.Sleep(200); } else { break; } } int dataLength = rev.byRecvData[8];//读取到的数据长度 string binary = string.Empty; for (int i = 0; i < dataLength; i++) { int Decimal = rev.byRecvData[9 + i]; //已经为十进制 --数据开始位置为第9个 string temp = Convert.ToString(Decimal, 2).PadLeft(8, '0'); //十进制转二进制 binary += MyFunction.StrReverse(temp); } this.Coils.SetBitDatasValue(binary);//直接修改线圈的值 } catch (Exception ex) { log.AddERRORLOG("单次读取线圈失败:" + ex.Message); } }
private void btn_MinBox_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_Mininize, (IntPtr)0, (IntPtr)0); }
private void SystemForm_Load(object sender, EventArgs e) { MyFunction myfunction = new MyFunction(); this.label_Version.Text = "软件版本:" + myfunction.GetVersion(); }
/// <summary> /// 处理串口接收的数据 /// </summary> /// <param name="reDataStr"></param> private void QueryData(string reDataStr) { string cass = reDataStr.Substring(1, 1); string msg = reDataStr.Substring(1, reDataStr.IndexOf("#") - 1);//接收到的消息 string message = string.Empty; switch (cass.ToUpper()) { case "E": //握手-【回复握手】 if (!GlobalVar.SoftWareShutDown) { message = "!" + msg + "#" + myfunction.CRC8(msg) + "\n"; } SendMsg(message); if (msg.Contains('1')) //允许作业 { MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSConnection, (IntPtr)0, (IntPtr)0); //握手成功 } if (msg.Contains('0')) //禁止作业 { MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSForbideWork, (IntPtr)0, (IntPtr)0); } break; case "W": //通知检查机托盘到位-【检查机收到信号】 MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSStartCheck, (IntPtr)0, (IntPtr)0); //到位,开始检测 break; case "D": //回归到位-【暂不处理】 break; case "F": //IC测试结果发送-【检查机收到数据】 MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSAcceptData, (IntPtr)0, (IntPtr)0); //接收到IC条码数据 break; case "G": //异常消息,需要报警灯 message = "!" + msg + "#" + myfunction.CRC8(msg) + "\n"; SendMsg(message); //回复检查机 if (msg.Contains("1")) //需要报警 { MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSCheckEMG, (IntPtr)0, (IntPtr)0); } if (msg.Contains("0")) //报警解除 { MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSCheckEMG_Reset, (IntPtr)0, (IntPtr)0); } break; case "M": //允许下料机下料 message = "!" + msg + "#" + myfunction.CRC8(msg) + "\n"; SendMsg(message); //回复检查机 //if (msg.Contains('0')) // MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSAllowDropBoard, (IntPtr)0, (IntPtr)0); if (msg.Contains('1')) { MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSForbideDrop, (IntPtr)0, (IntPtr)0); } break; case "L": //拍照完成 message = "!" + msg + "#" + myfunction.CRC8(msg) + "\n"; SendMsg(message); //回复检查机 if (msg.Contains('1')) { MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSNG, (IntPtr)0, (IntPtr)0); } else { MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSResult, (IntPtr)0, (IntPtr)0); } break; case "T": break; case "S": //IC禁用模式【不处理】 MyFunction.SendMessage(GlobalVar.gl_IntPtr_MainWindow, GlobalVar.WM_PCSArrive, (IntPtr)0, (IntPtr)0); break; case "C": //oee上传命令 GlobalVar.OeeSent = true; break; default: MsgBox("通信异常:未设定命令!", "串口通信异常", Color.Red); break; } }