private Response SendCommand(Command command) { if (!Open()) { return(null); } try { List <byte> bytes = new List <byte>() { command.CommandNumber }; bytes.AddRange(command.Data); lock (CommunicationPortLock) { Log(LogLevel.Information, "Sending Command: 0x{0}", BytesToString(bytes)); CommunicationPort.Write(bytes); Log(LogLevel.Information, "Sent"); if (command.ResponseLength == 0) { Log(LogLevel.Information, "Command expects no response."); return(new EmptyResponse()); } byte[] buffer = CommunicationPort.Read(command.ResponseLength).ToArray(); Log(LogLevel.Information, "Response received: 0x{0}", BytesToString(buffer)); return(command.BuildResponse(buffer)); } } catch (InvalidOperationException) { StopUpdateTimer(); RefreshInProgress = false; Close(); return(null); } }
void CommunicationPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] buffer = new byte[CommunicationPort.BytesToRead]; CommunicationPort.Read(buffer, 0, CommunicationPort.BytesToRead); Invoke(new UpdateDataTextBoxDeleagte(UpdateDataTextBox), buffer); }