private void handleDataSerialPort() { int count; byte[] response = null; while (!isStop) { byte[] readBuffer = new byte[1]; try { sp.Write(common, 0, 4); while ((count = sp.BytesToRead) != 8) { // Console.WriteLine("wait to read bytes count = " + count); //等待反馈 Thread.Sleep(10); } response = new byte[count]; sp.Read(response, 0, count); outCommand = readDataCommand(response); /*if (outCommand == LIFT_OUT_COMMAND_T.LIFT_OUT_COMMAND_loushang_baojing || outCommand == LIFT_OUT_COMMAND_T.LIFT_OUT_COMMAND_louxia_baojing) * { * if (dataCommand[0] == 0) //当前没有命令的时候才去发送复位信号,否则会覆盖之前的信号 * { * dataCommand[0] = 0x4; * } * * Console.WriteLine("will to reset"); * }*/ if (outCommand > LIFT_OUT_COMMAND_T.LIFT_OUT_COMMAND_UP_DOWN) //如果读取到升降机异常, 不再向升降机发送命令 { AGVLog.WriteInfo("升降机异常 " + outCommand, new StackFrame(true)); continue; } if (dataCommand[0] > 0) { sendCommand(dataCommand); dataCommand[0] = 0; dataCommand[1] = 0; //清空数据 } } catch (Exception ex) { Console.WriteLine(ex.ToString()); handleLiftComException(); } Thread.Sleep(200); } }
private LIFT_OUT_COMMAND_T readDataCommand(byte[] response) { LIFT_OUT_COMMAND_T command = LIFT_OUT_COMMAND_T.LIFT_OUT_COMMAND_MIN; int i = 0; for (i = 0; i < response.Length; i++) { //Console.WriteLine(" read response[" + i + "] = " + response[i]); if ((LIFT_OUT_COMMAND_T)response[i] < LIFT_OUT_COMMAND_T.LIFT_OUT_COMMAND_MAX && (LIFT_OUT_COMMAND_T)response[i] > LIFT_OUT_COMMAND_T.LIFT_OUT_COMMAND_MIN) { command = (LIFT_OUT_COMMAND_T)response[i]; } } if (command != LIFT_OUT_COMMAND_T.LIFT_OUT_COMMAND_MIN) { //Console.WriteLine(" out vommmmmm = " + command); } return(command); }