private void NewDataAvailable(SerialPortWrapper.SimpleSerialPacket packet) { lock (thisLock) { if (currentCommand == null) { Console.WriteLine("Error: Received data, but no command was sent!"); foreach (byte b in packet.Data) { Console.Write(b.ToString("x") + ", "); } Console.WriteLine(); } else { currentCommand.ProcessResponse(packet.Data); if (currentCommand.IsDataValid()) { // See if there's any state information in the command used to // update location or other fields... byte locations = MaxLocations; if (currentCommand is StatusCommand) { StatusCommand c = currentCommand as StatusCommand; currentPosition = Vector3.Divide (c.CurrentPosition.ToVector3(), ScaleFactors); locations = c.Locations; if (onPositionUpdate != null) { onPositionUpdate(this, EventArgs.Empty); } } if (currentCommand is MoveCommand) { MoveCommand m = currentCommand as MoveCommand; locations = m.Locations; } currentCommand = GetNextCommand(locations); elapsedCounter = 0; serial.Transmit(currentCommand.GenerateCommand(), 0x21); } else { Console.WriteLine("Error: Did not process data correctly!"); } } } }
private void NewDataAvailable(byte data) { lock (thisLock) { elapsedCounter = 0; if (currentCommand == null) { Console.WriteLine("Error: Received data, but no command was sent!"); Console.Write(data.ToString("x") + ", "); Console.WriteLine(); } else { if (currentCommand.ProcessResponse(data)) { bool canAcceptMoveCommand = false; if (currentCommand is IRobotCommandWithStatus) { IRobotCommandWithStatus status = currentCommand as IRobotCommandWithStatus; currentPosition = status.CurrentPosition; canAcceptMoveCommand = status.CanAcceptMoveCommand; if (this.lastPositionKnown == false) { lastPosition = currentPosition; lastPositionKnown = true; } if (onRobotStatusChange != null) { onRobotStatusChange(status, EventArgs.Empty); } } else if (currentCommand is RobotDetectionCommand) { RobotDetectionCommand r = currentCommand as RobotDetectionCommand; commandGenerator = r.GetCommandGenerator(); } currentCommand = GetNextCommand(canAcceptMoveCommand); serial.Transmit(currentCommand.GenerateCommand()); } } } }
void t_Elapsed(object sender, ElapsedEventArgs e) { t.Stop(); lock (thisLock) { if (serial != null && serial.IsOpen) { elapsedCounter++; if ((elapsedCounter * 50) > (1000)) // More than 1 second to reply { Console.WriteLine("Device Timeout!"); // Send a status command currentCommand = new StatusCommand(); serial.Transmit(currentCommand.GenerateCommand(), 0x21); elapsedCounter = 0; } } } t.Start(); }
void t_Elapsed(object sender, ElapsedEventArgs e) { t.Stop(); lock (thisLock) { if (elapsedCounter > timeout_ms) { if (currentCommand != null && currentCommand is RobotDetectionCommand) { Console.WriteLine("Unexpected Response from robot detection: " + (currentCommand as RobotDetectionCommand).DumpData()); } // Expected reply not received within 1 second, assume command was lost. Console.WriteLine("Device Timeout!"); } if (serial == null || !serial.IsOpen || elapsedCounter > timeout_ms) { lastPositionKnown = false; commandGenerator = null; currentCommand = null; elapsedCounter = 0; } else { if (currentCommand == null) { currentCommand = new RobotDetectionCommand(); serial.Transmit(currentCommand.GenerateCommand()); elapsedCounter = 0; } else { elapsedCounter += 50; } } } t.Start(); }