C Code for serial comm. with Micrologix 1000


Thread Starter

Jon Duke

I am new to C programming and I was wondering if anyone had any example code that I could compile to try and figure out how to actually communicate (read/write) from the com port on my pc to an Allen Bradley Micrologix 1000 plc. I have download the .pdf from ab.com but I can't make heads or tails of it. Any example code on how to set the baud rate, messages or how any of this is done would be greatly appreciated. Thanks in advance for any help.
Hi Curt

Are you familiar with the DF1 protocol, as far as sending the command messages and recieving the
reply messages in addition to DLE STX, ACK, ETX, NAK? Some information in this area would be
greatly benefical to me. Please feel free to email or message me if you can help, thanks again
for the earlier reply.

Chris Hopper

whatever you're trying to achieve, i think you're going to need to use rslinx as an intermediary link - i don't know of any way of performing comms with a micrologix plc other than by using the above - be interested to hear if you find another way of doing it.
Actually, you'll find DF1 Full-Duplex pretty easy to do. My employer won't allow me to release code & DF1 is not widely available in free source, but I can offer these tips.

Get a table-driven CRC16 routines from the ODVA Ethernet/IP spec Vol 1 Appendix C - they use it to validate some dictionary entries. But this is the same routine as used for Modbus - just Modbus starts with a CRC of 0xFFFF and DF1 starts with 0x0000, so any Modbus example code could also be used if you make this change. The STX (0x10 0x02) isn't in the CRC, plus any escaped 0x10 0x10 is only counted once, plus only the 0x03 of the ETX is in the CRC - I know that's an odd collection, but it's the "law".

You need to escape any 0x10 bytes - this includes the station address, error returns, sequence number, etc! So only the 0x10 in the STX or ETX
or ACK/NAK/ENQ can exist unescaped (plus the CRC if it happens to contain 0x10 is NOT escaped).

You'll find the "Protected Typed Logical Read/Write with 3 address fields" the easiest to use for your MicroLogix. This is well documented
on page 7-17/18 of the PDF you downloaded.

Remember the MicroLogix support what they call "Duplicate Packet Detection" (it's in your PDF). This means if you send it a packet and it
answers, then you twiddle with your code and send the SAME packet with the same TNS (or sequence number) the MicroLogix will ACK that packet but NOT answer it. Even though I know this - it still stumps/bites me once in awhile. So it's best to use a true random number to start your TNS numbers, so if you "reboot" your code after only 1 message, your first new message will be always answered.

An easy why to "validate" your plan is to just set up a MSG block in your MicroLogix (IF you message YOU want to send. You'll see the exact bytes, any 0x10 escaping and even the CRC value. If you turn around and REPEAT this same message
byte by byte you should get a valid response from the PLC.


Lynn August Linse, Senior IA Application Engineer
15353 Barranca Parkway, Lantronix Inc, Irvine CA 92618
[email protected] www.lantronix.com
Tel: (949)300-6337 Fax: (949)453-7152