Qaseem

Hi there,

I am using OpenPLC to query some slaves and I want to process the obtained values using a function that includes reading from a text file. This feature is not offered by a structure text, so I went to incorporate this function within a modbus_master.cpp file. I found the part that reads a holding register in querySlaveDevices and for simple testing, I assign a constant number to the holding register.

But when I run the PLC, the monitor page keeps giving me the value received from the slave. Did I miss something? Should I compile the OpenPLC code through the terminal to get the update?

Thanks,

Quote 0 0
thiagoralves
I have a feeling that you'll have better luck putting this custom code inside the HWLCB (hardware layer code box). Just click on the Hardware tab on OpenPLC runtime and write your code on the text area that appears. You can manipulate OpenPLC buffers directly from there.
Quote 0 0
Qaseem
Thanks, Thiago for a quick reply. I tried it but I got zero as value. Here is my code in the HWLCB

int ignored_int_inputs[] = {100}; //%IW100 is ignored

void updateCustomIn()
{
if (int_input[100] != NULL) *int_input[100] = 53;
}


I tried it with mutex but it didn't change the result and I found in the forum that we shouldn't use it since this part is included with buffer protected mutex.

The compilation didn't show any error but the runtime logs show the message (Processing command..)
The monitoring page shows server error but when I refresh the page, it shows zero value in the register IW100.
What should I fix to get the value 53? should I use mutex? ( the comment before the function requires
to use the mutex bufferLock but I removed it to follow your recommendation in the post ).

Thanks for your help!
Quote 0 0
thiagoralves
On /webserver/core/main.cpp line 266 you will have to reverse the order of input updates from
updateCustomIn();
updateBuffersIn_MB(); //update input image table with data from slave devices

to
updateBuffersIn_MB(); //update input image table with data from slave devices
updateCustomIn();
Quote 0 0
Qaseem
Thanks for your suggestion but I still got zero as value. Screen Shot 2020-06-17 at 8.14.03 AM.png Screen Shot 2020-06-17 at 8.17.19 AM.png
Quote 0 0
thiagoralves
Make sure you have saved your changes to main.cpp (the screenshot shows the file with a red title, perhaps meaning you haven’t saved it?) and that you have recompiled your program so that the changes can take effect. To recompile the program you can upload the .st file again or save changes on the HWLCB
Quote 0 0
Qaseem
I saved the changes and compiled the file after saving in HWLCB and another time when I uploaded the st file.  In the previous photo, I edited the indentation before the screenshot which shows unsaved changes.

Please note that I used mutex lock and mutex unlock to wrap the line in updateCustomIn() as follows:
pthread_mutex_lock(&bufferLock); //lock mutex
  if (int_input[100] != NULL) *int_input[100] = 53;
pthread_mutex_unlock(&bufferLock); //unlock mutex

If mutex is used, the value in the monitoring page shows zero, otherwise, the actual value received from the slave is shown (i.e, changes in the HWLCB has no effect).
So why OpenPLC buffer is not updated by the updateCustomIn() function?

Thank you in advance,

Quote 0 0
Qaseem
Finally, the OpenPLC buffer is updated. I reinstalled the Openplc and applied the changes in HWLCB and reversed the order of the function calls in main.cpp and it works.
Quote 0 0