Previously we used VB6 (Visual Basic 6) to implement the Omron Host Link Protocol to communicate to our programmable logic controller. (PLC) In part 2 we used Visual Basic for Applications (VBA) to accomplish our host link serial communication. We will now look at using VB.Net to establish and retrieve information in and out of the PLC using host link. (C mode commands)
Hostlink communication protocol is a method developed by Omron for communication to PLC’s and other equipment. This ASCII based protocol is used over RS232 or RS422/RS485. It is a many to one implementation which means that you can communicate with up to 32 devices back to a master. (1:N) This communication on the industrial floor can control PLC’s, Temperature Controllers, Panel Meters, etc.
The Omron CP1H plc will be used with a CP1W-CIF11 (RS485) communication serial link adapter. We will also use a USB-485M Universal Serial Bus to RS485 adapter from automation direct. The video at the end of this post will demonstrate the communications by reading 10 registers from the DM area of the PLC. Continue Reading!
The hostlink communication protocol is a method developed by Omron for communication to PLCs and other equipment. This ASCII-based protocol is used over RS232 or RS422/RS485. It is a many-to-one implementation which means that you can communicate with up to 32 devices back to a master. (1: N) This communication on the industrial floor can control PLCs, Temperature Controllers, Panel Meters, etc.
Our look at this protocol will include the wiring, setting of RS232 port settings, protocol format, and writing a VB6 program to read information from the PLC. I will also point you links to then store this information into a database and share it over an intranet/internet. Let’s get started.
Wiring Serial Port – Omron PLC Host Link Protocol
The wiring of the communication ports will depend on the equipment purchased. If communicating over 15 meters, it is recommended to switch to an RS422 or RS485 connection. However, I have seen RS232 runs of 50 meters without an issue. It will depend on your implementation and electrical noise in the plant.
The above diagram is the basic communication needed for RS232C. Note that the shield of the communication wire is connected only to one side. This ensures that any noise induced in the communication is filtered to one end.
Serial Settings – Omron PLC Host Link Protocol
Settings for RS232C communications are set in a number of ways. Older Omron C**K PLC was set through a series of dip switches. Current Omron SMR1/CPM1 PLCs are set though data memory locations.
Note: Most of the time, you need to cycle the power or switch to program/run mode for the setting to be activated.
I generally tend to leave everything at the default settings: 9600 bps, Even parity, 7 data bits, 1 stop bit. The default host link unit number is 00. (32 max. – 00 – 31)
Protocol Format – Omron PLC Host Link Protocol
Each piece of equipment will have a list of parameters that can be read and written using the HostLink protocol. This can be found in the programming manual of the device. Here are the areas in the CPM1/CPM1A/CPM2A/CPM2C/SRM1(-V2) from the programming manual.
Let’s take a look at the command to read the DM area. All of the commands and responses will be in an ASCII format.
The command format begins with a ‘@‘ sign followed by the Node / Unit number that you wish to communicate. The header code is the command in which you wish to execute. (RD) This header code will determine the next series of information. In our case, the next four digits will be the beginning word followed by the next four digits to indicate the number of words. The next part of the command is the FCS (checksum) calculation. The comparison to this at each end will ensure that the command/response is correct. FCS is an 8-bit data converted into two ASCII characters. The 8 bits are a result of an Exclusive OR performed on the data from the beginning to the end of the text in the frame. In our case this would be performed on the following:
The last part of the command is the terminator. This is an ‘*’ followed by the character for the carriage return. (CHR$(13))
The response format begins with a ‘@’ sign followed by the Node / Unit number that you are communicating to. The header code is next (RD) followed by the End Code. The end code is a two-digit ASCII code that indicates the message response/errors when executing the action. A normal code of ’00’ indicates that everything is fine. See the operation manual for the entire list of end codes for your equipment. The next part of the response depends on the header code executed. In our case, it would contain the data requested. The last two parts of the response are the FCS and terminator just like the command format.
The above shows the timing of the command and responses.
Visual Basic VB6 (Example) – Omron PLC Host Link Protocol
Now let’s look at an example of reading the first 10 words from the DM area of an Omron PLC.
The first step is the design the form. You can see that we have our ten DM area words set out to populate with values. We also have a T$ for transmitting. This will show what we are sending to the PLC. The RXD$ will show what the response will be from the PLC.
The MSComm is used to communicate through the serial ports of the computer. The following are the settings for the communication port.
Here is the VB6 code for the program:
When the form loads the Date/Time will get updated and Timer1 is enabled. This timer controls the interval in which the commands get executed. (Set to 1 second)
Private Sub Form_Load()
Label2.Caption = Format(Date, "YYYY/MM/DD") + " " + Format(Time, "HH:MM:SS")
Timer1.Enabled = True
The following code will open the communication port, set the command format, send the command through the port, receive the response through the port and display the information. It will then close the communication port.
The following is the subroutine to communicate. Timer2 is the amount of time to wait before expecting an answer to the communication port. Once the command has been sent a maximum of two seconds is waited for a response. If no response nothing is returned. When the response is obtained, the FCS is checked and if correct the information is returned.
rxd$ = ""
Buffer = T$ + FCS$ + "*" + Chr$(13)
MSComm1.Output = Buffer
Timer2.Enabled = True
Loop Until Timer2.Enabled = False
If Time > #11:59:50 PM# Then
timeout = #12:00:02 AM#
timeout = DateAdd("s", 2, Time)
MSComm1.InputLen = 0
If timeout <= Time Then GoTo timeoutcom
Loop Until MSComm1.InBufferCount >= charreturn
rxd$ = MSComm1.Input
fcs_rxd$ = Left((Right(rxd$, 4)), 2)
If Left(rxd$, 1) = "@" Then
T$ = Mid(rxd$, 1, (Len(rxd$) - 4))
ElseIf Mid(rxd$, 2, 1) = "@" Then
T$ = Mid(rxd$, 2, (Len(rxd$) - 5))
rxd$ = Mid(rxd$, 2, (Len(rxd$) - 1))
If FCS <> fcs_rxd$ Then
rxd$ = ""
clearbuffer$ = MSComm1.Input
This is the FCS (checksum) calculation routine.
L = Len(T$)
A = 0
For J = 1 To L
TJ$ = Mid$(T$, J, 1)
A = Asc(TJ$) Xor A
FCS$ = Hex$(A)
If Len(FCS$) = 1 Then FCS$ = "0" + FCS$
This is the routine that will execute if the response is not received within the time period expected.
If you have any questions, need further information or would like a copy of this program please contact me.
If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLCs are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII, and Floating Point.
To get this free article, subscribe to my free email newsletter.
Use the information to inform other people how numbering systems work. Sign up now.
The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.