The cyclic generated values in my Blazor Server side app service, are not refreshed (actualised) in my razor page correctly. What is missing?

Viewed 21

Hello I have a Blazor server side app, where a service is responsable to read cyclic data from a PLC control system. I am using a 3. party dll for reading the PLC values. The PLC values is are read in a second intervall for 30 seconds tottaly. The function is placed in a service.

In the service I am writing the read values in parallel to a log file with timestamp. I see that the values are logged correctly when they are generated, that means each second. (see code !!!!! part) That means my function is working correct.

I am calling the function from my razor page and I want to visualise the PLC values live on my screen. But the PLC process values are updated in my screen after the function is completed totaly (after 30 seconds). That means not every second. I think I need something like "StateHasChanged" in my service to rerender my page, but I don't know how to do this in a service.

My Razor page

@page "/PLC_Var"
@inject PLCService PLCService // 
<button @onclick="Read_PLC_Var">Read</button>

@for (int i = 0; i <= 4; i++)
{
  // The values are not updated after each second !!!
  <td> @PLC_VAR_result[i]</td>
  <br>
}


@code {
public async Task Read_PLC_Var()
    {
       DoCyclicReadingEx(Deltalogic.Return_Connect_PLC, 5000);
    }


}

This is my function in my service (PLCService)

public Int32 DoCyclicReadingEx(Int32 connnr, Int32 timeout)
    {
        Int32 cycletime = 1000;
        AGL4.DATA_RW40[] rwfield = new AGL4.DATA_RW40[1];

        rwfield[0] = new AGL4.DATA_RW40();
        rwfield[0].BitNr = 0;
        rwfield[0].DBNr = 1;
        rwfield[0].Offset = 0;
        rwfield[0].OpAnz = 1;
        rwfield[0].OpArea = AGL4.AREA_FLAG;
        rwfield[0].OpType = AGL4.TYP_DWORD;
        rwfield[0].Result = 0;
        rwfield[0].Value = new UInt32[rwfield[0].OpAnz];
        rwfield[0].Value[0] = 1;



        Int32 handle = 0;
        Int32 cycles = 5;
        String errormsg = "";
        Boolean start = false;
        // When start is set to "true" StartCyclicRead will be called automatically
        Int32 result = AGL4.InitCyclicReadEx(connnr, cycletime, start, ref rwfield, out handle, timeout);
        if (result == AGL4.AGL40_SUCCESS)
        {
            // Only required if start = false at InitCyclicReadEx
            result = AGL4.StartCyclicRead(connnr, handle, timeout);
            if (result == AGL4.AGL40_SUCCESS)
            {
                for (Int32 i = 0; i < cycles; i++)
                {
                    result = AGL4.GetCyclicReadEx(connnr, ref rwfield, handle, timeout);
                    if (result == AGL4.AGL40_SUCCESS)
                    {
                        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
                        // The PLC_VAR_result values are updated in my screen after the function is executed completely (after 30 seconds)
                        PLC_VAR_result[i] = rwfield[0].Value[0];
                        // But the values are written to the log file just when they are read
                        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        using (StreamWriter sw = File.AppendText(log_file))
                        {
                            sw.WriteLine("PLC VAR_"+i+ " "+PLC_VAR_result[i] + "" + Convert.ToString(DateTime.Now));
                        }
                    }
                    else
                    {
                        // Error happened
                        AGL4.GetErrorMsg(result, out errormsg);
                    }

                    
                }
                result = AGL4.StopCyclicRead(connnr, handle, timeout);
                result = AGL4.ExitCyclicRead(connnr, handle, timeout);
            }
            else
            {
                // Error happened
                AGL4.GetErrorMsg(result, out errormsg);
            }
        }
        else
        {
            // Error happened
            AGL4.GetErrorMsg(result, out errormsg);
        }
        return result;


    }
0 Answers
Related