bob_franchez
Been banging my head against the wall for ~2 days now, and have come to conclusion that either there's a fundamental error/bug in the Raspberry PI (3B, 64-bit if that matters) implementation of OpenPLC_v3 Runtime, or I'm missing something super basic.

I'm trying to extract the 16-bits from a UINT analog input in a Raspberry PI.  The FBD used seems to work until the WORD_TO_BYTE type conversion, then that conversion always returns 0.  The weird thing is the exact same FBD, just run within the PLC Simulator inside OpenPLC Editor instead of on the Raspberry PI, works fine.

FBD to Extract 16-bits from a UINT
Untitled.png

D
etails of BYTE_TO_BIT Block:
Untitled.png


This test program works perfectly fine in OpenPLC Editor, in the "Start PLC Simulation" mode.
Untitled.png


But uploading the *.ST file to a Raspberry PI running OpenPLC_v3 Runtime fails (DEBUG_LSB = 0, DEBUG_MSB = 0, all Bx = FALSE):
Untitled.png
Reply 0 0
thiagoralves
Hmmm...this could be nuances in how compilers are doing this conversion. On Windows the code is compiled using MinGW, on Linux, the same code is compiled using G++. Also, there could be a discrepancy in the MatIEC (the IEC to C transpiler) that runs on the Editor vs the one that runs on the Runtime. I will check the sources to make sure they are both identical.
Reply 0 0
bob_franchez
Came across your previous post on inlining C code into a function block using {}, so just created my own WORD_TO_BYTE type conversion to work around this potential bug. With that in mind, do you have any postings or resources you can point me to in order to understand the details of C code inlining? Things like what libraries are included (looks like <cstring> is since your post used memcpy), what functions are available (GetFbVar and SetFbVar are obviously available), and the like.  If there are no available resources, perhaps where I should be looking in the github source?
Reply 0 0
bob_franchez
Well shoot. Tried using your C-code inline version but it didn't work either. Starting to thing there's something amiss with SetFbVar() when one attempts to set an IEC_BYTE. That would certainly explain why the original WORD_TO_BYTE type conversion didn't work.

Used your other sample code and it worked just fine on a Raspberry PI. However, use that exact same code and just change the input IEC_REAL to an IEC_WORD, and then change the output from IEC_WORD to IEC_BYTE and it fails. The output bytes are never set. Furthermore, used printf() commands to verify all variables do contain the correct values.

{
   IEC_WORD var_in;
   IEC_BYTE var_out[2];
   var_in = GetFbVar(IN);
   memcpy(var_out, &var_in, sizeof(var_in));
   printf("IN: %d, LSB: %d, MSB: %d", var_in, var_out[0], var_out[1]);
   SetFbVar(OUT1, var_out[0]);
   SetFbVar(OUT2, var_out[1]);
}

As an FYI, looks like runtime and editor use the same compiler.
* ./iec2c -v (on raspberry PI) --> matiec version 0.1
* ./iec2c -v (on OpenPLC Editor) --> matiec version 0.1
Reply 0 0
thiagoralves
This is good debugging. I’ll try to take a better look at it on Monday, but I must say that just the -v command on matiec does not suffice. They could be from different commit versions, and more importantly, they can have different libs. In essence, the lib is what matter most in that particular case. That’s why I wanted to check if you swap the libs if that would work. On the runtime the lib is split is two folders, one with the text files and another with the C files
Reply 0 0
bob_franchez
All of this stems from trying to connect OpenPLC to Inductive Automation's Ignition, using their free Maker Edition version. The nice thing about this system is it's a well proven industrial SCADA system used by tens of thousands of industries across the globe, and hence had nearly all simple bug eliminated decades ago.

However, I'm continuing to find numerous unexplained nuances here and there. Is there any way to enable debugging of OpenPLC registers and the associated Modbus server?  Perhaps dumping all register I/O to a log, Modbus network and Modbus register manipulation to a log?

I'd like to try and get to the bottom of a few other outstanding issues. For example, I've got a LINT stored at %ML0 and %ML1. Looking at your Modbus documents I should be able to reach these addresses via Modbus at 4096 and 40100.
Capture.jpg
However, after way too many hours I started doing random address searched. I would tell Ignition to start at Modbus address 4090 and slowly walked my way up to around 40120. Found out that %ML0 is actually at 4097 and %ML1 at 4101.  It was off by a single integer, so I thought perhaps 0-index vs 1-index error. Flipped the switch in Ignition to do 0-indexing and then all my other Modbus address values from OpenPLC went bad, so clearly it's not 0-indexing.  Some sort of detailed logging or error reporting might be helpful in the future here, hence the inquiry.

Once I corrected for the modbus address index error, I could get the correct numbers. But weirdly if I try to write to %ML0 via Modbus it zeros out the register.  It's as if I can read from it but writing to it sets it to zero.

Another place debugging, if it exists, would be helpful is this one. Noticed your %MW1023 is accessible at modbus address 2048, but then %MD0 is also accessible at that same address index. That was another one I had to search around a bit for via address poking to find the real addresses.

Reply 0 0
thiagoralves
Modbus addressing has always worked fine for me. I guess the confusion for you is coming due to the fact that %MD and %ML don’t use a single Modbus register as they are larger than 16-bits.

The Modbus table on documentation is 0-indexed. If you take %MW, they start at Modbus address 1024 (%MW0) and go all the way up to 2047 (%MW1023). After that, each %MD takes two Modbus locations, so %MD0 is at 2048 and 2049. %MD1 is at 2050 and 2051, and so on… Same applies for %ML locations, with the difference that these take 4 registers instead of 2.

Because Modbus does not have any standard controlling the usage of larger numbers through multiple registers, different software uses different approaches to store/retrieve these numbers. For example, some consider the LSB coming first, others consider MSB coming first. For OpenPLC, it is MSB coming first. You need to make sure that your SCADA software matches the encoding used by OpenPLC, otherwise you will have headache after headache
Reply 0 0
thiagoralves
By the way, last time I did a security assessment on an ignition SCADA server, I found out that they had a RCE bug that allowed me to get admin rights on the entire Windows server just by sending a malformed packet from the outside:  https://www.infosecmatter.com/metasploit-module-library/?mm=exploit/multi/scada/inductive_ignition_rce

So yes, even expensive commercial software have nasty bugs every now and then. There is no way to escape from this, at least not while we humans write the code.
Reply 1 0
thiagoralves
I took a better look at your project below and now I understood what is happening. You located your byte values at %MB locations. These locations don't exist on OpenPLC mapping, that's why your values are being displayed as zeros. For the simulation, the compiler strips out all located variables for the debugger, so it doesn't matter what you write in the location field. Remove the location address from your BYTE variables and try again. I'm pretty confident it is going to work just fine.

For debugging purposes, you can always convert back the BYTE to WORD and store it in a %MW register. That can be displayed just fine.



Been banging my head against the wall for ~2 days now, and have come to conclusion that either there's a fundamental error/bug in the Raspberry PI (3B, 64-bit if that matters) implementation of OpenPLC_v3 Runtime, or I'm missing something super basic.

I'm trying to extract the 16-bits from a UINT analog input in a Raspberry PI.  The FBD used seems to work until the WORD_TO_BYTE type conversion, then that conversion always returns 0.  The weird thing is the exact same FBD, just run within the PLC Simulator inside OpenPLC Editor instead of on the Raspberry PI, works fine.

FBD to Extract 16-bits from a UINT
Untitled.png

D
etails of BYTE_TO_BIT Block:
Untitled.png


This test program works perfectly fine in OpenPLC Editor, in the "Start PLC Simulation" mode.
Untitled.png


But uploading the *.ST file to a Raspberry PI running OpenPLC_v3 Runtime fails (DEBUG_LSB = 0, DEBUG_MSB = 0, all Bx = FALSE):
Untitled.png
Reply 0 0
bob_franchez
Very much appreciate the clarification on %MB locations. Is the Byte (B) data type supported at all? I ask because I'm making cheat-sheets for various aspects of OpenPLC to help me understand how to use it, and I currently have locations are:
* format of: <Prefix1><Prefix2><address_location>
* where <Prefix1> can be any of I, Q, or M
* and <Prefix2> can be any of X, B, W, D, and L
However, seems like %MB is "invalid", and for modbus communications I saw a previous post of yours that effectively makes %MX "invalid".  Want to ensure my notes are accurate.

I'll dig into the Modbus endian-ness you suggested.
Reply 0 0
thiagoralves
For located variables, just stick with the Modbus table from documentation. BYTE is supported for internal variables (not Modbus) and is also supported on located variables for some hardware drivers that use it (i.e. there are some boards that uses BYTE to configure a few parameters). So, in summary, whatever is there in the docs (Modbus table) is supported and well tested.

edit:
I forgot to answer your specific question. %MB and %MX are indeed not supported
Reply 0 0
Reply