public byte[] GetBytesStream(FrameFormat frameRequestFwVersion) { byte[] retVal = new byte[7 + frameRequestFwVersion.DataLength]; retVal[0] = frameRequestFwVersion.Header; retVal[1] = frameRequestFwVersion.FrameVersion; retVal[2] = frameRequestFwVersion.MessageType; retVal[3] = (byte)((frameRequestFwVersion.Command & 0xFF00) >> 8); retVal[4] = (byte)(frameRequestFwVersion.Command & 0x00FF); retVal[5] = frameRequestFwVersion.DataLength; for (int n = 0; n < frameRequestFwVersion.DataLength; n++) { retVal[6 + n] = frameRequestFwVersion.OptionalData[n]; } byte crc = 0; for (int n = 0; n < 6 + frameRequestFwVersion.DataLength; n++) { crc = (byte)(crc ^ frameRequestFwVersion.OptionalData[n]); } retVal[7 + frameRequestFwVersion.DataLength - 1] = crc; return(retVal); }
public void AddChar(byte newChar) { switch (uART_MsgComposer_State) { case UART_MsgComposer_State_t.WAITING_FOR_BC: if (newChar == 0xBC) { actualFrame = new FrameFormat(); actualFrame.Header = 0xBC; uART_MsgComposer_State = UART_MsgComposer_State_t.WAITING_FOR_MSG_COMPLETE; } break; case UART_MsgComposer_State_t.WAITING_FOR_MSG_COMPLETE: composeFrame(newChar); break; } }
static void Main(string[] args) { UART_MsgComposer uART_MsgComposer = new UART_MsgComposer(); UartBuffer uartBuffer = new UartBuffer(100000); /* Passarlo al driver seriale che non abbiamo ancora */ SerialManager serialManager = new SerialManager(); serialManager.OpenSerial(uartBuffer, "COM4", 115200); ProcessBytes processBytes = new ProcessBytes(); processBytes.UartBuffer = uartBuffer; processBytes.UART_MsgComposer = uART_MsgComposer; processBytes.StartThread(); uART_MsgComposer.OnFrameReceived += UART_MsgComposer_OnFrameReceived; /* Handshake */ FrameFormat frameRequestFwVersion = new FrameFormat(); frameRequestFwVersion.Command = 0x01; /* Read Fw Version */ byte[] bytesToSend = uART_MsgComposer.GetBytesStream(frameRequestFwVersion); serialManager.SendCommand(bytesToSend); /* Wait For Rx */ if (resetEvent.WaitOne(1000)) { resetEvent.Reset(); /* Analizzo frameRx */ } else { /* Segnalo il timeout */ } }
private static void UART_MsgComposer_OnFrameReceived(object sender, FrameFormat e) { frameRx = e; resetEvent.Set(); }