Beispiel #1
0
        private void btnApplySettings_Click(object sender, EventArgs e)
        {
            // Check for serial port connection first.
            if (!modbusClient.Connected)
            {
                // Show error message
                Debug.WriteLine("Submit of settings not possible. No connection to MODBUS.");
                txtFetchError.Text        = "You have to connect to serial port before you can fetch data!";
                txtConnectState.Text      = "MODBUS connected";
                txtConnectState.BackColor = Color.Red;
                return;
            }

            // TODO:
            //      - Check settings are valid:
            if (false)
            {
                // Can't happen..
            }

            //      - Stop all requests e.g. CYCLIC_READ
            if (CYCLIC_READ)
            {
                CYCLIC_READ             = false;
                btnCyclicRead.Text      = "Start Cyclic Read";
                btnCyclicRead.BackColor = Color.Plum;

                btnRead1.Enabled = true;
            }

            //      - Clear displayed data
            // clearData();

            //      - Read input fields and write settings to MODBUS holding registers
            try
            {
                int[] values = new int[5];      // { 1, 2, 3, 4, 5 };

                values[0] = cbbModeSelect.SelectedIndex;
                int.TryParse(cbbNumBats.Text, out values[1]);
                int.TryParse(cbbNumBatCells.Text, out values[2]);

                if (cbbActiveBat.SelectedIndex >= 0)
                {
                    int.TryParse(cbbActiveBat.Text, out values[3]);
                }
                else
                {
                    values[3] = 0;
                }

                if (cbbChrgBat.SelectedIndex >= 1)
                {
                    int.TryParse(cbbChrgBat.Text, out values[4]);
                    values[4] += 4;
                }
                else if (cbbChrgBat.SelectedIndex == 0)
                {
                    values[4] = 0xBABE;
                }
                else
                {
                    values[4] = 0xBABE;
                }

                Debug.WriteLine(values[0]);
                Debug.WriteLine(values[1]);
                Debug.WriteLine(values[2]);
                Debug.WriteLine(values[3]);
                Debug.WriteLine(values[4]);

                // Fetch server response
                modbusClient.WriteMultipleRegisters(HOLDING_REG_START - 1, values);
                Debug.WriteLine("Wrote HOLDING REGISTER values to Server");
            }
            catch (Exception exc)
            {
                Debug.WriteLine("Exception Writing HOLDING to Server.");
            }

            // Update "Current Settings" on successful response.
            inputRegToGUI();

            /*try
             * {
             *  // Fetch server response
             *  int[] serverResponse = modbusClient.ReadHoldingRegisters(HOLDING_REG_START - 1, HOLDING_REG_NUM_REGS);
             *
             *  Debug.WriteLine("Read DEFAULT-settings from holding registers");
             *  for(int i=0; i<serverResponse.Length; i++)
             *  {
             *      Debug.WriteLine(serverResponse[i]);
             *  }
             *
             *  updateCurrentSettings(serverResponse);
             * }
             * catch (Exception exc)
             * {
             *  Debug.WriteLine("Exception Reading HOLDING from Server.");
             *
             * }*/
        }