Using TCP/IP with a PLC

Created by Rico Stodt, Modified on Thu, 17 Feb, 2022 at 8:23 AM by Rico Stodt

This page provides a detailed example of how to use TCP/IP in combination with a PLC. 


Contents


Overview


TCP/IP communication is an option starting in Intera version 5.1.  This opens up the ability to share data with many different types of devices.  It is great for robot to robot communication or for reading data from a device like a barcode scanner.  Since the Modbus communication is currently limited to only discrete data, this is one way of reading and writing integers.  However, there is a lot more work involved to set it up in a robust manner compared to Modbus.

The first reason this is complex is because the PLC program has to manage the connection.  For a robust system, this means monitoring the status and automatically retrying when necessary.  The second reason it is complex is because the data needs to be parsed and converted between strings, ASCII, and integers.

PLCs generally will require some ladder logic or function block programming to communicate with Sawyer over generic TCP/IP.  Typically instructions are needed to open and manage the connection, to read and write, and to format data.  The instructions will be slightly different depending on whether the PLC is the client or the server.


PLC Commands


The following commands are required on the PLC to make this work.

  1. Socket Create
  2. Either Socket Accept (if the Sawyer initiates communication) or Socket Open (if the PLC initiates the communication)
  3. Socket Read (if sending data from Sawyer to the PLC)
  4. Socket Write (if sending data from the PLC to Sawyer)


Socket Create


In the PLC, the first step is to create a socket. The port number is the most important input value, which by default on the robot is 4000.  Make sure the port number used here and the one on the robot are matching.  This instruction outputs an “instance” which is a number that identifies the socket and is used by the other socket instructions.

Socket Accept
 If Sawyer is the client, it means Sawyer will initiate the communications.  When you configure the device in Intera Studio, under “Sawyer will”, select “connect to a device".  



You will need to supply the IP address of the PLC.  The PLC needs to accept the connection request from Sawyer using the Socket Accept instruction.  When the socket accept function is executed on the PLC, the status of the device in Intera Studio will turn green, indicating there is an open line of communication between the robot and the PLC.  


 

Make sure to use the instance created by the Socket Create command as the instance input to the Socket Accept instruction.


Socket Open


 If Sawyer is the server, it means the PLC will initiation the communications.  When you configure the device in Intera Studio, under “Sawyer will”, select “receive connections".   The PLC needs to make the connection using the Socket Open function, which, when executed will turn the device status green in Intera Studio, indicating there is an open line of communication between the robot and the PLC.  Make sure to use the instance created by the Socket Create command as the instance input to the Socket Open instruction.


Socket Read 


 Setup on the robot:  In order to send data from the robot to the PLC, the signal needs to be set up on the robot.  This is done in the same “Devices” window where the PLC itself was configured.  The first section is “Pack Data - Out” where you set the number of fields and the delimiters.  In this example, we are only sending 1 field and not using any delimiters.  The next section is “Define Data - Out”.  Here you only need to provide a name, the data type, and default value.  In this example, I am using the integer data type.

Data is sent from Sawyer to the PLC when it is updated on the robot.  The update would normally be done with a Set To node in the behavior tree, but can also be done manually by typing a value in Shared Data.  It is important to note that it only sends the data once at the time it is updated.  It does not constantly send the value.  This means that if a signal on Sawyer hasn't been updated since the connection with the PLC was established, the PLC will not see that value.  When the PLC receives a message, it places it in a buffer on the ethernet module.  It isn't placed into the PLC's data tables until a Socket Read function is executed in the PLC.  If Sawyer has sent multiple values before the Socket Read has been executed on the PLC, there will be multiple values in the buffer, and each read will update the data table location in the PLC with the new value from the buffer.  If the buffer is empty, the Socket Read function will return a timeout error.  This means that you will most likely want to create logic in the PLC to execute the read function at regular intervals, but while it is not busy already in a read.  This will ensure the PLC is always reading the most recent value from the robot, and not an old value from the buffer.  

The data coming from Sawyer will be a string of ASCII characters.  Therefore, you will need to convert these characters to the appropriate format in the PLC logic.  For example, if it sends the number “48”, it will send the string 5256.  “52” is the ASCII equivalent of 4 and “56” is the ASCII equivalent of 8.  Since the data being read is array of bytes, the first byte will be “52” and the second “56”.  It is important to set up the length of the Socket Read to be greater than or equal to the number of characters being sent because each element of the array is a byte and each ASCII character is 1 byte.  So, if you need to read a 5 digit number, you need to read a length of 5.  After reading the value in the PLC, you need to:
Convert the ASCII character to its string equivalent. This gives us 4, and 8.
Convert the string to integer.
Multiply the number in the tens place (4 in this example) by 10 to get its true value.
 Add the result of step 3 plus the number in the ones place, which results in 48 (4 x 10 + 8).

But what if there are 3 digits in the integer?  The steps are the same, but you will need to multiply the first number by 100, the second by 10, and add all 3.  Luckily, the read instruction in the PLC provides as output the number of bytes that were read (3 bytes = a 3 digit number).  This can then be used to manipulate the string data into an integer.  

Socket Write
 The PLC writes data to another device using the Socket Write function.  Similar to the read function, the data needs to be transmitted in ASCII format.  Therefore, the integer value needs to be converted to a string, and each character converted to its ASCII equivalent before sending.  This is a bit simpler than sending data in the other direction.   After executing the Socket Write, the new value should be immediately updated on the robot.

  

Socket Info


 In order to have a robust solution, error handling for the socket connection is required.  In the Micro850, the instruction Socket Info can be used to monitor the status of the connection, and retry to establish the connections when it is not active.  This is important so that it will automatically connect if the robot is taken offline and put back online.


More on ASCII


 The robot communicates with other devices in strings of data in ASCII format.  This means that you need to put the ASCII equivalent for the character you want to send.  If you want to send the capital letter “A”, you would send the number 65.  Here are some more examples.  

For multiple characters, each ASCII number would be in its own byte in the array of data to be sent.  The commas are not included.

  • “Robot” = 82, 111, 98, 111, 116
  • “-78” = 45, 55, 56
  • “56.76” = 53, 54, 46, 55, 54

Sending multiple values


 If 2 integers should be sent, e.g. 20 and 146 from the PLC to the robot, a delimiter needs to be set up -  for example a semicolon.  In which case, the ASCII equivalent of 20;146 which would be 50, 48, 59, 49, 52, 54 needs to be sent.  On Sawyer, the signal would be set up to have 2 fields and a delimiter of a semicolon.  The value of 20 would be placed in the first, and 146 in the second.  Similarly, if multiple values should be sent from Sawyer to the PLC, the delimiter would be specified in Sawyer and would have to be handled by the PLC logic.

 


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article