/// <summary> /// /// </summary> /// <param name="frame"></param> public void SendData(ProtocolFrame frame) { if (Connected) { _serialPort.Write(frame.ConvertFrameToByteArray(), 0, frame.FrameLength); OnMessageSent(frame); } }
public ProtocolFrameMessage(ProtocolFrame frame, bool isOutputFrame = false) { Frame = frame; IsOutputFrame = isOutputFrame; }
/// <summary> /// /// </summary> /// <param name="frame"></param> private void UpdateSlave(ProtocolFrame frame) { int slaveId = frame.Id; if (!Slaves.ContainsKey(slaveId)) Slaves[slaveId] = new SlaveModel() { SlaveId = slaveId }; var cmd = new string(frame.Command); switch(cmd) { case "SA": Slaves[slaveId].AnalogValue = BitConverter.ToInt16(frame.Data,0); break; case "SD": Slaves[slaveId].DigitalValue = BitConverter.ToUInt16(frame.Data,0); break; case "AS": case "ID": Slaves[slaveId].AnalogValue = BitConverter.ToInt16(frame.Data, 0); Slaves[slaveId].DigitalValue = BitConverter.ToUInt16(frame.Data, frame.DataLength / 2); break; } }
private void OnSlaveUpdated(ProtocolFrame frame, int slaveId) { var handler = SlaveUpdated; handler.Invoke(this, new MessageStatusChangedEventArgs(frame, slaveId)); }
private void OnMessageSent(ProtocolFrame frame) { var handler = MessageSent; handler?.Invoke(this, new MessageStatusChangedEventArgs(frame)); }
/// <summary> /// /// </summary> /// <param name="slave"></param> /// <param name="command"></param> /// <returns></returns> private ProtocolFrame ConvertSlaveModelToProtocolFrame(SlaveModel slave, string command ) { ProtocolFrame frame = null; switch (command) { case "SA": frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), BitConverter.GetBytes(slave.AnalogValue)); break; case "SD": frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), BitConverter.GetBytes(slave.DigitalValue)); break; case "AS": var concatedArray = BitConverter.GetBytes(slave.AnalogValue).Concat(BitConverter.GetBytes(slave.DigitalValue)).ToArray(); frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), concatedArray); break; case "ID": frame = new ProtocolFrame(slave.SlaveId, command.ToCharArray(), null); break; } return frame; }
public MessageStatusChangedEventArgs(ProtocolFrame frame, int slaveId = 255) { Frame = frame; SlaveId = slaveId; }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected override void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { _lastIncomingFrame = ReadData(); UpdateSlave(_lastIncomingFrame); OnSlaveUpdated(_lastIncomingFrame, _lastIncomingFrame.Id); }