public override void UpdateState(int chainIndex, ICommand[] outputStates) { if (SerialPortIsValid && _serialPort.IsOpen) { _packet = new byte[_headerLen + OutputCount + _footerLen]; var packetLen = _packet.Length; _header.CopyTo(_packet, 0); _footer.CopyTo(_packet, packetLen - _footerLen); for (int i = 0; i < outputStates.Length && IsRunning; i++) { _commandHandler.Reset(); ICommand command = outputStates[i]; if (command != null) { command.Dispatch(_commandHandler); } _packet[i + _headerLen] = _commandHandler.Value; } if (packetLen > 0) { try { _serialPort.Write(_packet, 0, packetLen); } catch (Exception ex) { Logging.Error(ex, "An error occured writing to the serial port."); } } } }
public override void UpdateState(int chainIndex, ICommand[] outputStates) { if (serialPortIsValid && _SerialPort.IsOpen) { _packet = new byte[headerLen + OutputCount + footerLen]; var packetLen = _packet.Length; _header.CopyTo(_packet, 0); _footer.CopyTo(_packet, packetLen - footerLen); for (int i = 0; i < outputStates.Length && IsRunning; i++) { _commandHandler.Reset(); ICommand command = outputStates[i]; if (command != null) { command.Dispatch(_commandHandler); } _packet[i + headerLen] = _commandHandler.Value; } if (outputStates.Length > headerLen + footerLen) { _SerialPort.Write(_packet, 0, packetLen); } } }
internal void SendUpdate(ICommand[] outputStates, Data _Data, CommandHandler _commandHandler) { if (serialPortIsValid && isSerialPortOpen) { byte[] _header = Encoding.ASCII.GetBytes(_Data.Header == null ? string.Empty : _Data.Header); var headerLen = _header.Length; // compute this here so we don't have to compute it in the loop byte[] _footer = Encoding.ASCII.GetBytes(_Data.Footer == null ? string.Empty : _Data.Footer); byte[] _packet = new byte[headerLen + outputStates.Length + _footer.Length]; _header.CopyTo(_packet, 0); _footer.CopyTo(_packet, _packet.Length - _footer.Length); ICommand command = null; // Why do we use the command handler? For this module, it just seems like unnecessary overhead. for (int i = 0; i < outputStates.Length; i++) { _commandHandler.Reset(); command = outputStates[i]; if (command != null) { command.Dispatch(_commandHandler); } _packet[i + headerLen] = _commandHandler.Value; } if (_packet.Length > 0) { _SerialPort.Write(_packet, 0, _packet.Length); } } }