comModbusRtu.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 # Modifed from the orginal comModbusTcp by Kelsey Hawkins @ Georgia Tech
37 
38 
39 
40 """@package docstring
41 Module comModbusRtu: defines a class which communicates with Robotiq Grippers using the Modbus RTU protocol.
42 
43 The module depends on pymodbus (http://code.google.com/p/pymodbus/) for the Modbus RTU client.
44 """
45 
46 from pymodbus.client.sync import ModbusSerialClient
47 from math import ceil
48 
50 
51  def __init__(self):
52  self.client = None
53 
54  def connectToDevice(self, device):
55  """Connection to the client - the method takes the IP address (as a string, e.g. '192.168.1.11') as an argument."""
56  self.client = ModbusSerialClient(method='rtu',port=device,stopbits=1, bytesize=8, baudrate=115200, timeout=0.2)
57  if not self.client.connect():
58  print "Unable to connect to %s" % device
59  return False
60  return True
61 
63  """Close connection"""
64  self.client.close()
65 
66  def sendCommand(self, data):
67  """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)"""
68  #make sure data has an even number of elements
69  if(len(data) % 2 == 1):
70  data.append(0)
71 
72  #Initiate message as an empty list
73  message = []
74 
75  #Fill message by combining two bytes in one register
76  for i in range(0, len(data)/2):
77  message.append((data[2*i] << 8) + data[2*i+1])
78 
79  #To do!: Implement try/except
80  self.client.write_registers(0x03E8, message, unit=0x0009)
81 
82  def getStatus(self, numBytes):
83  """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"""
84  numRegs = int(ceil(numBytes/2.0))
85 
86  #To do!: Implement try/except
87  #Get status from the device
88  response = self.client.read_holding_registers(0x07D0, numRegs, unit=0x0009)
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_rtu
Author(s): Nicolas Lauzier (Robotiq inc.), Kelsey Hawkins
autogenerated on Tue Jun 1 2021 02:30:05