Author Topic: Modbus 16bit addresses Read and hold value and display value in set time  (Read 1966 times)

cajunman64

  • Newbie
  • *
  • Posts: 6
    • View Profile
I am working on updating my HMI project on a "BasicTrendChart" on the 2nd page with 3 Basiclabels; 2 with the same registers address; 1st Label will display the current valve and the 2nd Label will display the current valve in a set time period (say in15 minute) note: the pressure values will be live data and all the labels will be updating every second, and the 3rd Basiclabel will display the difference value between the first and second label.
I know I will have to write a code to achieve my goal but not sure when to start.

Thanks for any help

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Modbus 16bit addresses Read and hold value and display value in set time
« Reply #1 on: February 17, 2024, 09:03:47 AM »
Only the live value should be a BasicLabel. The other two will be updated via code, so they should only be of type Label.

- From the toolbox, add a Timer to the form
- Set the interval property to the time you want in milliseconds
- Set Enabled to True
- Double click the timer to get back to code
- Add code similar to this:
Code: [Select]
Try
  Label1.Text=BasicLabel1.Text
  Label2.Text=Label1.Text - BasicLabel1.Text
Catch ex as exception
  Label2.Text=ex.message
End Try

cajunman64

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Modbus 16bit addresses Read and hold value and display value in set time
« Reply #2 on: February 18, 2024, 06:16:54 AM »
I'm not sure what going on but I can't seam to correct the Errors

Below is my code;

using AdvancedHMIControls;
using System;
using System.Activities.Expressions;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AdvancedHMICS
{
    public partial class MainForm : Form
    {
        public object BasicLabel1 { get; private set; }
        public object Label2 { get; private set; }

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        private void analogValueDisplay1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
       
        }

        private void OFDReader_FileOk(object sender, CancelEventArgs e)
        {
           
       

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Try
                Label1.Text = BasicLabel1.Text
                Label2.Text = Label1.Text - BasicLabel1.Text
            Catch ex as exception
                Label2.Text = ex.message
            End Try
        }

        private void label3_Click(object sender, EventArgs e)
        {
           
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Modbus 16bit addresses Read and hold value and display value in set time
« Reply #3 on: February 18, 2024, 07:25:31 AM »
Looks like you are using VB syntax in a c# program. c# is going to be stricter with case sensitivity and type conversions.

Your code would look something more like this:

            label4.Text = (int.Parse(label3.Text) - int.Parse(BasicLabel1.Text)).ToString();

cajunman64

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Modbus 16bit addresses Read and hold value and display value in set time
« Reply #4 on: February 19, 2024, 04:53:06 AM »
Thanks I had been looking at how C# code is different