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); } }
public async Task RunningAsync(CancellationToken cancellationToken) { DateTime triggerTime = DateTime.MinValue; InitializeModbus(ref pvClient, pvmodbusConfig); InitializeModbus(ref essClient, essmodbusConfig); while (true) { //if (triggerTime > DateTime.Now) // continue; //triggerTime = DateTime.Now.Add(modbusConfig.Interval); cancellationToken.ThrowIfCancellationRequested(); try { Task[] client_access_task = new Task[] { TryConnect(pvClient, cancellationToken), TryConnect(essClient, cancellationToken) }; Task.WaitAll(client_access_task); int[] ess_read_values = essClient.ReadInputRegisters(28, 30); int[] pv_read_values = pvClient.ReadInputRegisters(28, 16); //int[] trms = pvClient.ReadInputRegisters(538, 2); //float trms_value = EasyModbus.ModbusClient.ConvertRegistersToFloat(new int[] { trms[0], trms[1] }); //logger.Info($"trms_value = {trms_value}"); float pcs_freq; float pcs_actPwr; float pcs_rctPwr; float pcs_appPwr; float pcs_act_forward_high; float pcs_act_forward_low; float pcs_act_reverse_high; float pcs_act_reverse_low; float pv_freq; float pv_actPwr; float pv_rctPwr; float pv_appPwr; float pv_act_forward_high; float pv_act_forward_low; ReadValues(ess_read_values, out pcs_freq, out pcs_actPwr, out pcs_rctPwr, out pcs_appPwr, out pcs_act_forward_high, out pcs_act_forward_low, out pcs_act_reverse_high, out pcs_act_reverse_low); ReadValues(pv_read_values, out pv_freq, out pv_actPwr, out pv_rctPwr, out pv_appPwr, out pv_act_forward_high, out pv_act_forward_low); double pv_accum = pv_act_forward_high + (pv_act_forward_low / 10000); double ess_total_chg = pcs_act_forward_high + (pcs_act_forward_low / 10000); double ess_total_dhg = pcs_act_reverse_high + (pcs_act_reverse_low / 10000); Console.WriteLine("High: " + pv_act_forward_high + " log: " + pv_act_forward_low + " accum: " + pv_accum); await InsertDbAsync(pcs_freq, pv_actPwr, pv_accum, pcs_actPwr, ess_total_chg, ess_total_dhg, DateTime.Now, cancellationToken); //await PublishEvent(freq, actPwr, rctPwr, appPwr, act_forward_high, act_forward_low, DateTime.Now, cancellationToken); await Task.Delay(essmodbusConfig.Interval, cancellationToken); } catch (Exception ex) { logger.Error(ex, ex.Message); essClient.Disconnect(); pvClient.Disconnect(); } } }
public void stop() { modbusClient.Disconnect(); mqttClient.Disconnect(); this.shouldStop = true; }
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(); } }