One for ControlLogix ladder
Peter Nachtwey said:
S7-300s don't have RS-232 ports. S7-200 can use freeport and implement Modbus. Implementing Modbus or any protocol in ladder code is not easy.
Rockwell has an implementation for Modbus -- one for a master, one for a slave -- written in ladder for their ControlLogix family. It can be downloaded off their website. As I recall, it was RTU only, no ASCII, and I think it was only a subset of Modbus, but they have a PDF document that describes its functionality. The ControlLogix port is RS-232C, so you need a converter to connect to RS-485 for Modbus.
I used the Modbus Master code. The PDF has inconsistencies with the actual ladder logic and type defs. Fortunately, the ladder code seems to work. The PDF talks about byte swapping to deal with endian issues, but the ladder doesn't seem to do this.
(This is good because when I looked at it, it appeared that Modbus and ControlLogix were the same-endian, big endian if I remember correcty.)
My bigest gripe with the code was that it triggered an endless stream of overflow errors in the minor error log. This would happen when copying numbers larger than 128 into bytes. ControlLogix would upgrade them from signed bytes into integers and then complain that it couldn't fit all the extended sign bits back into an unsigned byte. (Or something like that) I cleaned up a couple of them, but I think there were a few left when I ran out of time.
The total rung count was 145 spread over 14 routines. Here is a breakdown:
MainRoutine 10
ClearBuffer 1
CRCLoopR 2
CRCLoopT 2
CRC_TaskR 2
DataPack 4
Decode 46
H1Z_Modbus_Activate 16
IO_Scanner 21
ReadCoils_Inputs 10
ReadReg_Inputs 9
ReceiveMonitor 22
ShiftbitLeft 2
ShiftbitRight 2
WriteCoils 6
(I just looked at MainRoutine, and 8 of those rungs were my housekeeping rungs, unrelated to the Modbus functions.
)
It's a good thing he broke it down into so many routines because the routine names and the UDT structure member names are about the only documentation in the code.
Now he does use some of the very advanced ControlLogix concepts -- you know, things like array indexing
-- so that increases the difficulty of converting it to S7 LAD or FBD. While most accesses seem, surprisingly, to be to fixed indexes, there are still a good number of variable indexes. ControlLogix is also more intelligent about converting types, so you would have to add in a few more instructions for this.
Those with access to RSLogix5000 and a need to handle Modbus should check it out. It looks fairly straightforward: build up the command block, send it out, wait for the response.
And for those of who wanting to know what possible use the Masked Move (MVM) has, I see that he has a couple of those in there to handle the ReadCoils command.