comModbusTcp.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Robotiq, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Robotiq, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 #
33 # Copyright (c) 2012, Robotiq, Inc.
34 # Revision $Id$
35 
36 
37 
38 """@package docstring
39 Module comModbusTcp: defines a class which communicates with Robotiq Grippers using the Modbus TCP protocol.
40 
41 The module depends on pymodbus (http://code.google.com/p/pymodbus/) for the Modbus TCP client.
42 """
43 
44 from pymodbus.client.sync import ModbusTcpClient
45 from pymodbus.register_read_message import ReadInputRegistersResponse
46 from math import ceil
47 import time
48 import threading
49 
51 
52  def __init__(self):
53  self.client = None
54  self.lock = threading.Lock()
55 
56  def connectToDevice(self, address):
57  """Connection to the client - the method takes the IP address (as a string, e.g. '192.168.1.11') as an argument."""
58  self.client = ModbusTcpClient(address)
59 
61  """Close connection"""
62  self.client.close()
63 
64  def sendCommand(self, data):
65  """Send a command to the Gripper - the method takes a list of uint8 as an argument. The meaning of each variable depends on the Gripper model (see support.robotiq.com for more details)"""
66  #make sure data has an even number of elements
67  if(len(data) % 2 == 1):
68  data.append(0)
69 
70  #Initiate message as an empty list
71  message = []
72 
73  #Fill message by combining two bytes in one register
74  for i in range(0, len(data)/2):
75  message.append((data[2*i] << 8) + data[2*i+1])
76 
77  #To do!: Implement try/except
78  with self.lock:
79  self.client.write_registers(0, message)
80 
81  def getStatus(self, numBytes):
82  """Sends a request to read, wait for the response and returns the Gripper status. The method gets the number of bytes to read as an argument"""
83  numRegs = int(ceil(numBytes/2.0))
84 
85  #To do!: Implement try/except
86  #Get status from the device
87  with self.lock:
88  response = self.client.read_input_registers(0, numRegs)
89 
90  #Instantiate output as an empty list
91  output = []
92 
93  #Fill the output with the bytes in the appropriate order
94  for i in range(0, numRegs):
95  output.append((response.getRegister(i) & 0xFF00) >> 8)
96  output.append( response.getRegister(i) & 0x00FF)
97 
98  #Output the result
99  return output


robotiq_modbus_tcp
Author(s): Nicolas Lauzier (Robotiq inc.)
autogenerated on Tue Jun 1 2021 02:30:06