public void start() { this.shouldStop = false; if (mqttBrokerAddress == null) { throw new ArgumentOutOfRangeException("Mqtt Broker Address not initialized"); } mqttClient = new uPLibrary.Networking.M2Mqtt.MqttClient(mqttBrokerAddress, mqttBrokerPort, false, null, null, uPLibrary.Networking.M2Mqtt.MqttSslProtocols.None); string clientID = new Guid().ToString(); try { if (MqttUserName == null || MqttPassword == null) { mqttClient.Connect(clientID); } else { mqttClient.Connect(clientID, MqttUserName, MqttPassword); } if (!modbusClient.Connected) { modbusClient.Connect(); } } catch (Exception exc) { if (!this.AutomaticReconnect) { throw exc; } } for (int i = 0; i < readOrders.Count; i++) { readOrders[i].thread = new System.Threading.Thread(new ParameterizedThreadStart(ProcessData)); readOrders[i].thread.Start(readOrders[i]); } }
private async Task TryConnect(EasyModbus.ModbusClient client, CancellationToken token) { int cnt = 1; while (client.Connected == false) { try { client.Connect(); //client.Disconnect(); if (client.Connected) { string connectingInfo = string.IsNullOrEmpty(client.IPAddress) ? $"RTU ({client.SerialPort})" : $"TCP ({client.IPAddress}:{client.Port})"; this.logger.Info($"Connecting to Modbus slave at {connectingInfo}"); } else { throw new TimeoutException("Connect error"); } //await Task.Delay(TimeSpan.FromSeconds(5), token); } catch (Exception ex) { this.logger.Error(ex, ex.Message); this.logger.Info($"Attempting connection to Modbus Slave...Attempt #{cnt++}"); await Task.Delay(TimeSpan.FromSeconds(5), token); } } }
public void GetValues(ConnectionProperties connectionProperties, int functionPropertyID) { modbusClient = connectionProperties.modbusClient; if (!modbusClient.Connected) { modbusClient.IPAddress = connectionProperties.ModbusTCPAddress; modbusClient.Port = connectionProperties.Port; modbusClient.Connect(); } switch (connectionProperties.FunctionPropertiesList[functionPropertyID].FunctionCodeRead) { case FunctionCodeRd.ReadCoils: connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadCoils(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity); break; case FunctionCodeRd.ReadDiscreteInputs: connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadDiscreteInputs(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity); break; case FunctionCodeRd.ReadHoldingRegisters: connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadHoldingRegisters(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity); break; case FunctionCodeRd.ReadInputRegisters: connectionProperties.FunctionPropertiesList[functionPropertyID].values = modbusClient.ReadInputRegisters(connectionProperties.FunctionPropertiesList[functionPropertyID].StartingAdress, connectionProperties.FunctionPropertiesList[functionPropertyID].Quantity); break; default: break; } if (valuesChanged != null) { valuesChanged(this); } }
public bool Connect() { try { modbusClient = new ModbusClient(ip, port); modbusClient.Connect(); modbusClient.akbReceiveDataChanged += OnReceiveDataChanged; return(true); } catch (Exception) { throw; } }
public void start() { this.shouldStop = false; if (mqttBrokerAddress == null) { throw new ArgumentOutOfRangeException("Mqtt Broker Address not initialized"); } if (!modbusClient.Connected) { modbusClient.Connect(); } mqttClient = new uPLibrary.Networking.M2Mqtt.MqttClient(mqttBrokerAddress); string clientID = new Guid().ToString(); mqttClient.Connect(clientID); for (int i = 0; i < readOrders.Count; i++) { readOrders[i].thread = new System.Threading.Thread(new ParameterizedThreadStart(ProcessData)); readOrders[i].thread.Start(readOrders[i]); } }
private void connect_Click(object sender, EventArgs e) { try { if (modbusClient.Connected) { modbusClient.Disconnect(); } else { modbusClient.SerialPort = "COM3"; modbusClient.Baudrate = 38400; modbusClient.Parity = System.IO.Ports.Parity.None; modbusClient.StopBits = System.IO.Ports.StopBits.One; modbusClient.Connect(); } } catch (Exception exc) { MessageBox.Show(exc.Message, "Unable to connect to Server", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void setText() { seww.Connect(); seww.receiveDataChanged += new ModbusClient.ReceiveDataChanged(get); }
private void btnPortConnect_Click(object sender, EventArgs e) { if (btnPortConnect.Text == "Connect") { if (comboBoxPorts.SelectedItem == null) { Debug.WriteLine("Connection not possible. No Port selected."); txtFetchError.Text = "Choose a serial port first!"; return; } try { // Configure the modbusClient (Master) modbusClient.SerialPort = comboBoxPorts.SelectedItem.ToString(); modbusClient.UnitIdentifier = SLAVE_ADDR; modbusClient.Baudrate = BAUDRATE; // The MODBUS implementation on FW side doesn't work with any parities except "NONE" modbusClient.Parity = System.IO.Ports.Parity.None; modbusClient.StopBits = System.IO.Ports.StopBits.One; modbusClient.Connect(); if (modbusClient.Connected) { txtConnectState.Text = "Connected!"; txtConnectState.BackColor = Color.Lime; txtComErrors.Text = ""; } /** * Read 'default' settings from the firmware via modbus. * (Which mode, how many batteries, how many cells per battery, etc.) * * Show info in the status textbox (green or red) */ try { /* --- Fetch server response --- */ int[] serverResponse = modbusClient.ReadHoldingRegisters(HOLDING_REG_START - 1, HOLDING_REG_NUM_REGS); Debug.WriteLine("Read DEFAULT-settings from holding registers:"); updateCurrentSettings(serverResponse); if ((ushort)serverResponse[8] == 1) { alertEmergencyStop(); } } catch (Exception exc) { Debug.WriteLine("Exception Reading HOLDING from Server."); txtConnectState.Text = "MODBUS not connected"; txtConnectState.BackColor = Color.Red; } } catch (Exception ex) { Debug.WriteLine("Exception occured. Could not connect to Port/Server."); } } else { modbusClient.Disconnect(); txtConnectState.Text = "MODBUS not connected"; txtConnectState.BackColor = Color.Red; clearData(); clearCurrentSettings(); } }