public void SendMessage(Rs232CommunicationManager rs232Manager, ModbusMessage message) { int messageLength = message.Data.Length * 2; var data = new byte[messageLength + 7]; data[0] = (byte)':'; byte[] address = HexHelper.EncodeByteAsHex(message.Address); byte[] function = HexHelper.EncodeByteAsHex(message.Function); data[1] = address[0]; data[2] = address[1]; data[3] = function[0]; data[4] = function[1]; byte[] messageBytes = HexHelper.EncodeAsHexArray(message.Data); for (int i = 0; i < messageLength; i++) data[5 + i] = messageBytes[i]; byte[] numericMessage = HexHelper.DecodeHexArray(data, 1, 4 + messageLength); byte[] lrc = HexHelper.EncodeByteAsHex(CalculateLRC(numericMessage)); data[data.Length - 2] = lrc[0]; data[data.Length - 1] = lrc[1]; // The data is now 8 bits per character, but it's not a problem, it will // be reduced to 7 bits per character automatically. rs232Manager.SendMessage(new RS232Message(data)); }
public void ClosePort() { if (rs232Manager != null) { rs232Manager.ClosePort(); rs232Manager = null; } IsPortOpen = false; }
public void OpenPort(ModbusConnectionParameters parameters) { if (rs232Manager != null) { rs232Manager.ClosePort(); rs232Manager.DataReceivedEvent -= OnDataReceived; } rs232Manager = new Rs232CommunicationManager(parameters.GetConnectionParameters()); rs232Manager.DataReceivedEvent += OnDataReceived; rs232Manager.OpenPort(); messageProcessor = parameters.GetMessageProcessor(); IsPortOpen = true; }
public void SendMessage(Rs232CommunicationManager rs232Manager, ModbusMessage message) { throw new NotImplementedException(); }
private void UpdateCommunicationManager() { ConnectionParameters parameters = new ConnectionParameters( cboPort.Text, Int32.Parse(cboBaud.Text), Int32.Parse(cboData.Text), (Parity)cboParity.SelectedItem, ((HandshakeMenuItem) cboHandshake.SelectedItem).type, (StopBits)cboStop.SelectedItem, ((EndMarkerMenuItem) cboEndMarker.SelectedItem).type); communicationManager = new Rs232CommunicationManager(parameters); communicationManager.LoggableEventOccurred += new EventHandler<LogEventArgs>(communicationManager_LoggableEventOccurred); communicationManager.DataReceivedEvent += OnDataReceived; }