// serial port data received event handler private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { Byte[] recvData = new Byte[serialPort.BytesToRead]; serialPort.Read(recvData, 0, recvData.Length); for (int i = 0; i < recvData.Length; i++) { Byte c = recvData[i]; switch (rxState) { case RxState.READY: if (c == CODE_STX) { rxState = RxState.RECEIVING; rxBufferIdx = 0; } break; case RxState.RECEIVING: if (c == CODE_STX) { rxBufferIdx = 0; } else if (c == CODE_ETX) { rxBuffer[rxBufferIdx] = 0x00; ssAPI.executeCommand(rxBuffer); rxState = RxState.READY; } else { rxBuffer[rxBufferIdx] = c; rxBufferIdx++; if (rxBufferIdx >= RX_BUFFER_SIZE) { rxState = RxState.READY; } } break; default: rxState = RxState.READY; break; } } }
// initialize public SerialScreenServer(FormMain parent) { // serial port serialPort = new SerialPort(); serialPort.PortName = "COM1"; // default serialPort.BaudRate = 9600; // default serialPort.Parity = System.IO.Ports.Parity.None; serialPort.DataBits = 8; serialPort.StopBits = System.IO.Ports.StopBits.One; serialPort.ReadTimeout = 3000; // need not? serialPort.ReceivedBytesThreshold = 1; serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); rxState = RxState.READY; rxBuffer = new Byte[RX_BUFFER_SIZE]; rxBufferIdx = 0; ssAPI = new SerialScreenAPI(); ssAPI.setPictureBox(parent); }
// open public bool open() { serialPort.Close(); try { serialPort.Open(); serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); rxState = RxState.READY; rxBufferIdx = 0; } catch { errorMessage = "Can not open " + serialPort.PortName; serialPort.Close(); return(false); } return(true); }
private void AuthCallback(string x) { if (x.Substring(0, 1) == "v") { string version = x.Substring(0, 4); // *prays that this versioning system doesn't go away* IRxProtocol p; if (!ProtocolManager.TryGetProtocol(version, out p)) { throw new RxRemoteException( "Unknown version detected from server. This likely means the server is running a newer protocol version than this client supports."); } Protocol = p; State |= RxState.Opened; RxDataReceiveCallback -= AuthCallback; wait.Set(); } }
protected virtual void OnAsyncRead(Task <String> task) { if (task.Result != null && !task.IsFaulted && !task.IsCanceled) { if (RxDataReceiveCallback != null) { RxDataReceiveCallback(task.Result); } reader.ReadLineAsync().ContinueWith(OnAsyncRead, source.Token); } else if (task.Exception != null) { throw new RxRemoteException( "An error occurred reading from the remote client. See inner exception for more details.", task.Exception); } else if (task.IsCanceled) { State = RxState.Closed; client.Close(); } }
public bool EvalNextByte(byte b) { bool ret = false; _consumedHeader.Add(b); switch (_currentState) { case RxState.WaitForStatusInfo: if (b == (byte)AvrPacketType.LiftStatus) { _currentState = RxState.WaitForLen; ret = true; } else if (b == (byte)AvrPacketType.TraceMessage) { _currentState = RxState.WaitForTraceMsgLen; ret = true; } else if (b == (byte)AvrPacketType.ReadRegister) { _currentState = RxState.WaitRxReadRegLen; } else if (b == (byte)AvrPacketType.WriteRegister) { _currentState = RxState.WaitRxWriteRegStatus; } break; case RxState.WaitRxReadRegLen: _expectedRegisterBytes = b; _regData.Clear(); if (_expectedRegisterBytes == 0) // there was a problem { NotifyRegisterDataReceived(); _currentState = RxState.WaitForStatusInfo; } else { _receivedRegisterBytes = 0; _currentState = RxState.WaitRxReadRegData; } break; case RxState.WaitRxReadRegData: _regData.Add(b); _receivedRegisterBytes++; if (_receivedRegisterBytes == _expectedRegisterBytes) { _currentState = RxState.WaitForStatusInfo; NotifyRegisterDataReceived(); } break; case RxState.WaitRxWriteRegStatus: bool status = b == 1; _currentState = RxState.WaitForStatusInfo; NotifyWriteRegStatus(status); break; case RxState.WaitForLen: _currentState = RxState.WaitForStatusInfo; if (b == 6) { _currentState = RxState.WaitForSynch1; ret = true; } else if (b == 4) { _currentState = RxState.WaitForLen; } break; case RxState.WaitForSynch1: _currentState = RxState.WaitForStatusInfo; if (b == 0xA5) { _currentState = RxState.WaitForSynch2; ret = true; } break; case RxState.WaitForSynch2: _currentState = RxState.WaitForStatusInfo; if (b == 0x5A) { _currentState = RxState.WaitForStatus; ret = true; } break; case RxState.WaitForStatus: _status = b; _currentState = RxState.WaitForDoorOpen; ret = true; break; case RxState.WaitForDoorOpen: _status = (_status << 8) | b; NotifyStatusReceived(); Consumed.Clear(); _currentState = RxState.WaitForStatusInfo; _status = 0; ret = true; break; case RxState.WaitForTraceMsgLen: if ((b & (byte)AvrPacketType.TraceMassagePadLen) == (byte)AvrPacketType.TraceMassagePadLen) { _currentState = RxState.RxTraceMessage; _expectedTraceBytes = (b & 7) + 2; // the id is implicit _receivedTraceBytes = 0; _traceMessage.Clear(); ret = true; } else { _currentState = RxState.WaitForStatusInfo; Consumed.Clear(); } break; case RxState.RxTraceMessage: _receivedTraceBytes++; _traceMessage.Add(b); if (_receivedTraceBytes == _expectedTraceBytes) { _consumedHeader.Clear(); _currentState = RxState.WaitForStatusInfo; NotifyTrace(); } ret = true; break; } return(ret); }
private void ProcessBuffer() { while (m_rxBuffer.BytesToRead != 0) { byte ch = GetCh(); switch (m_rxState) { case RxState.WaitDollar: if (ch == '$') { m_rxState = RxState.WaitG; } break; case RxState.WaitG: if (ch == 'G') { m_rxState = RxState.WaitP; } else { m_rxState = RxState.WaitDollar; } break; case RxState.WaitP: if (ch == 'P') { m_rxState = RxState.WaitEnd; m_sentence.SetLength(0); } else { m_rxState = RxState.WaitDollar; } break; case RxState.WaitEnd: if (ch == '\r') { m_sentence.Seek(0, SeekOrigin.Begin); DecodeSentence(m_sentence); m_sentence.SetLength(0); m_rxState = RxState.WaitDollar; } else { if (m_sentence.Length > 80) { m_sentence.SetLength(0); m_rxState = RxState.WaitDollar; } else { m_sentence.WriteByte(ch); } } break; } } }
protected virtual void OnAsyncRead(Task<String> task) { if (task.Result != null && !task.IsFaulted && !task.IsCanceled) { if (RxDataReceiveCallback != null) { RxDataReceiveCallback(task.Result); } reader.ReadLineAsync().ContinueWith(OnAsyncRead, source.Token); } else if (task.Exception != null) { throw new RxRemoteException( "An error occurred reading from the remote client. See inner exception for more details.", task.Exception); } else if (task.IsCanceled) { State = RxState.Closed; client.Close(); } }
/// <summary> /// Start is called on the frame when a script is enabled just before /// any of the Update methods is called the first time. /// </summary> void Start() { coin = 0; single = this; }
public bool setRXbyte( byte rcv ) { bool wakePacketIsReceived = false; if( rcv == Constants.FEND ) { state = RxState.BEGIN; } // byte stuffing if( rcv == Constants.FESC && flagFESC == false ) { flagFESC = true; return false; } if( flagFESC == true ) { flagFESC = false; if( rcv == Constants.TFEND ) rcv = Constants.FEND; else if( rcv == Constants.TFESC ) rcv = Constants.FESC; } // end byte stuffing switch( state ) { case RxState.BEGIN: if( rcv == Constants.FEND ) { state = RxState.STARTPACKET; } break; case RxState.STARTPACKET: if( ( rcv & 0x80 ) != 0 ) { sendAddress = true; state = RxState.ADDRESS; this.Address = ( (byte)( rcv & 0x7F ) ); } else { sendAddress = false; state = RxState.COMMAND; this.Address = ( (byte)0 ); this.Command = rcv; } break; case RxState.ADDRESS: state = RxState.COMMAND; this.Command = rcv; break; case RxState.COMMAND: // receive CntData m_data.Clear(); state = ( rcv != 0 ) ? RxState.DATA : RxState.CRC; rxdataCnt = rcv; if( rxdataCnt > Constants.SLIPFRAME ) { // err: packet is very long throw new ArgumentException( "Received WakeUp packet is very long" ); } break; case RxState.DATA: m_data.Add( rcv ); if( m_data.Count == rxdataCnt ) { state = RxState.CRC; } break; case RxState.CRC: this.CodeErr = ( rcv == (byte)performCRCcalculation() ) ? Constants.ERR_NO : Constants.ERR_TX; state = RxState.BEGIN; wakePacketIsReceived = true; break; } return wakePacketIsReceived; }