using libmodbus with python ctypes
I'm trying to use libmodbus from python using ctypes. I'm using libmodbus trunk.
I created the python classes for libmodbus header (structures, constants etc.,) using ctypeslib.
The code i wrote to connect to a modbus slave is:
$ cat mbinterface.py
from ctypes import *
from modbus_ctypes import *
try:
cdll.
except:
print "Unable to load libmodbus.so"
exit(0)
def main():
mb = CDLL("libmodbus
mb_param = modbus_param_t()
mb.
mb.
res = mb.modbus_
print 'device : %s, baud: %s' % (mb_param.device, mb_param.baud)
print 'parity : %s, slave: %s ' % ( mb_param.parity, mb_param.slave)
and the output is:
$ python mbinterface.py
Opening /dev/ttyUSB0 at 9600 bauds (none)
device : , baud: 0
parity : , slave: 3
I'm perplexed that interface and parity paramters have been assigned correctly, but not the slave id.
Question information
- Language:
- English Edit question
- Status:
- Solved
- For:
- libmodbus Edit question
- Assignee:
- No assignee Edit question
- Solved by:
- Pradeep Gowda
- Solved:
- Last query:
- Last reply:
Revision history for this message
|
#1 |
You don't handle correctly the types of data because device and parity are strings and baud and slave must be integers so something is certainly wrong in your modbus_ctypes.
Revision history for this message
|
#2 |
It appears i'd not installed (`sudo make install`) the latest version of libmodbus.
This is the working code, which prints the uptime, in seconds of Wattnode Modbus meter.
from ctypes import *
from modbus_ctypes import *
try:
cdll.
except:
print "Unable to load libmodbus.so"
exit(0)
def main():
mb = CDLL("libmodbus
mb_param = modbus_param_t()
tab_
bauds = c_int(9600)
slave = c_int(1)
bits = c_int(8)
stop_bits = c_int(1)
device = c_char_
parity = c_char_p("none")
mb.
mb.
mb.
mb.
mb.
val = mb.modbus_
print val
print 'done.'
mb.
if __name__ == '__main__':
main()
Revision history for this message
|
#3 |
Great, could you share your code (ctypes), please?
Revision history for this message
|
#4 |
I am also very interested in the modbus_ctypes module. Can you either post it here?
Revision history for this message
|
#5 |
This is autogenerated using h2xml.py and xml2py.py scripts
as outlined here: http://
The file generated by me is below
-------
from ctypes import *
STRING = c_char_p
IPPORT_SMTP = 25
IPPORT_CMDSERVER = 514
IPPORT_EXECSERVER = 512
IPPORT_FINGER = 79
IPPORT_NAMESERVER = 42
IPPORT_DAYTIME = 13
IPPROTO_DCCP = 33
SCM_CREDENTIALS = 2
IPPROTO_IGMP = 2
IPPROTO_FRAGMENT = 44
MSG_SYN = 1024
IPPROTO_RSVP = 46
SCM_RIGHTS = 1
MSG_CONFIRM = 2048
IPPROTO_NONE = 59
FLUSH_OR_
IPPORT_USERRESERVED = 5000
TCP_CA_CWR = 2
TCP_FIN_WAIT1 = 4
TCP_TIME_WAIT = 6
IPPORT_WHOIS = 43
IPPROTO_ROUTING = 43
MSG_NOSIGNAL = 16384
SHUT_RD = 0
IPPROTO_DSTOPTS = 60
IPPROTO_HOPOPTS = 0
TCP_LAST_ACK = 9
IPPORT_RJE = 77
SHUT_RDWR = 2
IPPROTO_UDP = 17
TCP = 1
MSG_RST = 4096
IPPROTO_IPV6 = 41
IPPROTO_IPIP = 4
IPPORT_DISCARD = 9
TCP_CA_Open = 0
SOCK_NONBLOCK = 2048
IPPORT_SYSTAT = 11
TCP_CA_Loss = 4
IPPROTO_UDPLITE = 136
IPPROTO_AH = 51
IPPORT_RESERVED = 1024
SOCK_PACKET = 10
MSG_MORE = 32768
TCP_CLOSE = 7
SOCK_SEQPACKET = 5
IPPROTO_ESP = 50
IPPORT_NETSTAT = 15
MSG_PROXY = 16
IPPROTO_TP = 29
TCP_CLOSE_WAIT = 8
MSG_PEEK = 2
IPPROTO_GRE = 47
SOCK_CLOEXEC = 524288
SHUT_WR = 1
TCP_FIN_WAIT2 = 5
MSG_CTRUNC = 8
SOCK_STREAM = 1
MSG_TRUNC = 32
TCP_SYN_RECV = 3
IPPORT_WHOSERVER = 513
IPPROTO_ENCAP = 98
IPPORT_TELNET = 23
TCP_CA_Disorder = 1
MSG_DONTROUTE = 4
RTU = 0
IPPORT_TIMESERVER = 37
IPPROTO_IDP = 22
IPPROTO_ICMP = 1
SOCK_RDM = 4
IPPORT_SUPDUP = 95
IPPORT_MTP = 57
SOCK_DCCP = 6
IPPROTO_TCP = 6
IPPROTO_ICMPV6 = 58
MSG_DONTWAIT = 64
MSG_FIN = 512
MSG_EOR = 128
MSG_TRYHARD = 4
TCP_LISTEN = 10
IPPORT_LOGINSERVER = 513
IPPROTO_MTP = 92
MSG_WAITALL = 256
SOCK_DGRAM = 2
IPPROTO_PUP = 12
TCP_ESTABLISHED = 1
IPPROTO_PIM = 103
IPPORT_ECHO = 7
IPPROTO_MAX = 256
IPPORT_FTP = 21
IPPROTO_RAW = 255
TCP_CA_Recovery = 3
NOP_ON_ERROR = 1
IPPORT_ROUTESERVER = 520
IPPORT_TFTP = 69
IPPORT_EFSSERVER = 520
IPPROTO_IP = 0
TCP_SYN_SENT = 2
IPPORT_TTYLINK = 87
MSG_OOB = 1
TCP_CLOSING = 11
IPPROTO_SCTP = 132
MSG_CMSG_CLOEXEC = 1073741824
IPPROTO_COMP = 108
IPPROTO_EGP = 8
MSG_ERRQUEUE = 8192
SOCK_RAW = 3
IPPORT_BIFFUDP = 512
# values for enumeration 'type_com_t'
type_com_t = c_int # enum
# values for enumeration 'error_handling_t'
error_handling_t = c_int # enum
class modbus_
pass
uint8_t = c_uint8
class termios(Structure):
pass
tcflag_t = c_uint
cc_t = c_ubyte
speed_t = c_uint
termios._fields_ = [
('c_iflag', tcflag_t),
('c_oflag', tcflag_t),
('c_cflag', tcflag_t),
('c_lflag', tcflag_t),
('c_line', cc_t),
('c_cc', cc_t * 32),
('c_ispeed', speed_t),
('c_ospeed', speed_t),
]
modbus_
('slave', c_int),
('fd', c_int),
('type_com', type_com_t),
('debug', c_int),
('port', c_int),
('device', c_char * 16),
('baud', c_int),
('data_bit', uint8_t),
('stop_bit', uint8_t),
('parity', c_char * 5),
('error_
('ip', c_char * 16),
('old_tios', termios),
]
class modbus_
pass
uint16_t = c_uint16
modbus_
('nb_
('nb_
('nb_
('nb_
('tab_
('tab_
('tab_
('tab_
]
class ip_opts(Structure):
pass
class in_addr(Structure):
pass
uint32_t = c_uint32
in_addr_t = uint32_t
in_addr._fields_ = [
('s_addr', in_addr_t),
]
ip_opts._fields_ = [
('ip_dst', in_addr),
('ip_opts', c_char * 40),
]
class ip_mreqn(
pass
ip_mreqn._fields_ = [
('imr_
('imr_address', in_addr),
('imr_ifindex', c_int),
]
class in_pktinfo(
pass
in_pktinfo._fields_ = [
('ipi_ifindex', c_int),
('ipi_
('ipi_addr', in_addr),
]
pthread_t = c_ulong
class pthread_
pass
pthread_
('__size', c_char * 36),
('__align', c_long),
]
class __pthread_
pass
__pthread_
('__next', POINTER(
]
__pthread_slist_t = __pthread_
class __pthread_
pass
class N15pthread_
pass
N15pthread_
('__spins', c_int),
('__list', __pthread_slist_t),
]
__pthread_
__pthread_
('__lock', c_int),
('__count', c_uint),
('__owner', c_int),
('__kind', c_int),
('__nusers', c_uint),
('_0', N15pthread_
]
class pthread_
pass
pthread_
('__data', __pthread_mutex_s),
('__size', c_char * 24),
('__align', c_long),
]
class pthread_
pass
pthread_
('__size', c_char * 4),
('__align', c_int),
]
class N14pthread_
pass
N14pthread_
N14pthread_
('__lock', c_int),
('__futex', c_uint),
('__total_seq', c_ulonglong),
('_
('__woken_seq', c_ulonglong),
('__mutex', c_void_p),
('__nwaiters', c_uint),
('_
]
class pthread_
pass
pthread_
pthread_
('__data', N14pthread_
('__size', c_char * 48),
('__align', c_longlong),
]
class pthread_
pass
pthread_
('__size', c_char * 4),
('__align', c_int),
]
pthread_key_t = c_uint
pthread_once_t = c_int
class N16pthread_
pass
N16pthread_
('__lock', c_int),
('_
('_
('_
('_
('_
('__flags', c_ubyte),
('__shared', c_ubyte),
('__pad1', c_ubyte),
('__pad2', c_ubyte),
('__writer', c_int),
]
class pthread_
pass
pthread_
('__data', N16pthread_
('__size', c_char * 32),
('__align', c_long),
]
class pthread_
pass
pthread_
('__size', c_char * 8),
('__align', c_long),
]
pthread_spinlock_t = c_int
class pthread_
pass
pthread_
('__size', c_char * 20),
('__align', c_long),
]
class pthread_
pass
pthread_
('__size', c_char * 4),
('__align', c_int),
]
__sig_atomic_t = c_int
class __sigset_
pass
__sigset_t._fields_ = [
('__val', c_ulong * 32),
]
sa_family_t = c_ushort
__socklen_t = c_uint
socklen_t = __socklen_t
# values for enumeration '__socket_type'
__socket_type = c_int # enum
class sockaddr(
pass
sockaddr._fields_ = [
('sa_family', sa_family_t),
('sa_data', c_char * 14),
]
class sockaddr_
pass
sockaddr_
('ss_family', sa_family_t),
('__ss_align', c_ulong),
('_
]
# values for unnamed enumeration
class msghdr(Structure):
pass
class iovec(Structure):
pass
size_t = c_uint
msghdr._fields_ = [
('msg_name', c_void_p),
('msg_namelen', socklen_t),
('msg_iov', POINTER(iovec)),
('msg_iovlen', size_t),
('msg_control', c_void_p),
('msg_
('msg_flags', c_int),
]
class cmsghdr(Structure):
pass
cmsghdr._fields_ = [
('cmsg_len', size_t),
('cmsg_level', c_int),
('cmsg_type', c_int),
('__cmsg_data', c_ubyte * 0),
]
# values for unnamed enumeration
class ucred(Structure):
pass
__pid_t = c_int
pid_t = __pid_t
__uid_t = c_uint
uid_t = __uid_t
__gid_t = c_uint
gid_t = __gid_t
ucred._fields_ = [
('pid', pid_t),
('uid', uid_t),
('gid', gid_t),
]
class linger(Structure):
pass
linger._fields_ = [
('l_onoff', c_int),
('l_linger', c_int),
]
class timeval(Structure):
pass
__time_t = c_long
__suseconds_t = c_long
timeval._fields_ = [
('tv_sec', __time_t),
('tv_usec', __suseconds_t),
]
__u_char = c_ubyte
__u_short = c_ushort
__u_int = c_uint
__u_long = c_ulong
__int8_t = c_byte
__uint8_t = c_ubyte
__int16_t = c_short
__uint16_t = c_ushort
__int32_t = c_int
__uint32_t = c_uint
__int64_t = c_longlong
__uint64_t = c_ulonglong
__quad_t = c_longlong
__u_quad_t = c_ulonglong
__dev_t = __u_quad_t
__ino_t = c_ulong
__ino64_t = __u_quad_t
__mode_t = c_uint
__nlink_t = c_uint
__off_t = c_long
__off64_t = __quad_t
class __fsid_
pass
__fsid_t._fields_ = [
('__val', c_int * 2),
]
__clock_t = c_long
__rlim_t = c_ulong
__rlim64_t = __u_quad_t
__id_t = c_uint
__useconds_t = c_uint
__daddr_t = c_int
__swblk_t = c_long
__key_t = c_int
__clockid_t = c_int
__timer_t = c_void_p
__blksize_t = c_long
__blkcnt_t = c_long
__blkcnt64_t = __quad_t
__fsblkcnt_t = c_ulong
__fsblkcnt64_t = __u_quad_t
__fsfilcnt_t = c_ulong
__fsfilcnt64_t = __u_quad_t
__ssize_t = c_int
__loff_t = __off64_t
__qaddr_t = POINTER(__quad_t)
__caddr_t = STRING
__intptr_t = c_int
iovec._fields_ = [
('iov_base', c_void_p),
('iov_len', size_t),
]
# values for unnamed enumeration
in_port_t = uint16_t
# values for unnamed enumeration
class in6_addr(
pass
class N8in6_addr4DOT_
pass
N8in6_addr4DOT_
('__u6_addr8', uint8_t * 16),
('__u6_addr16', uint16_t * 8),
('__u6_addr32', uint32_t * 4),
]
in6_addr._fields_ = [
('__in6_u', N8in6_addr4DOT_
]
class sockaddr_
pass
sockaddr_
('sin_family', sa_family_t),
('sin_port', in_port_t),
('sin_addr', in_addr),
('sin_zero', c_ubyte * 8),
]
class sockaddr_
pass
sockaddr_
('sin6_family', sa_family_t),
('sin6_port', in_port_t),
('sin6_
('sin6_addr', in6_addr),
('sin6_
]
class ip_mreq(Structure):
pass
ip_mreq._fields_ = [
('imr_
('imr_
]
class ip_mreq_
pass
ip_mreq_
('imr_
('imr_
('imr_
]
class ipv6_mreq(
pass
ipv6_mreq._fields_ = [
('ipv6mr_
('ipv6mr_
]
class group_req(
pass
group_req._fields_ = [
('gr_
('gr_group', sockaddr_storage),
]
class group_source_
pass
group_source_
('gsr_
('gsr_group', sockaddr_storage),
('gsr_source', sockaddr_storage),
]
class ip_msfilter(
pass
ip_msfilter.
('imsf_
('imsf_
('imsf_fmode', uint32_t),
('imsf_numsrc', uint32_t),
('imsf_slist', in_addr * 1),
]
class group_filter(
pass
group_filter.
('gf_
('gf_group', sockaddr_storage),
('gf_fmode', uint32_t),
('gf_numsrc', uint32_t),
('gf_slist', sockaddr_storage * 1),
]
class in6_pktinfo(
pass
in6_pktinfo.
('ipi6_addr', in6_addr),
('ipi6_
]
class ip6_mtuinfo(
pass
ip6_mtuinfo.
('ip6m_addr', sockaddr_in6),
('ip6m_mtu', uint32_t),
]
class timestamp(
pass
u_int8_t = c_ubyte
u_int32_t = c_uint
timestamp._fields_ = [
('len', u_int8_t),
('ptr', u_int8_t),
('flags', c_uint, 4),
('overflow', c_uint, 4),
('data', u_int32_t * 9),
]
class iphdr(Structure):
pass
u_int16_t = c_ushort
iphdr._fields_ = [
('ihl', c_uint, 4),
('version', c_uint, 4),
('tos', u_int8_t),
('tot_len', u_int16_t),
('id', u_int16_t),
('frag_off', u_int16_t),
('ttl', u_int8_t),
('protocol', u_int8_t),
('check', u_int16_t),
('saddr', u_int32_t),
('daddr', u_int32_t),
]
class ip(Structure):
pass
u_short = __u_short
ip._fields_ = [
('ip_hl', c_uint, 4),
('ip_v', c_uint, 4),
('ip_tos', u_int8_t),
('ip_len', u_short),
('ip_id', u_short),
('ip_off', u_short),
('ip_ttl', u_int8_t),
('ip_p', u_int8_t),
('ip_sum', u_short),
('ip_src', in_addr),
('ip_dst', in_addr),
]
class ip_timestamp(
pass
ip_timestamp.
('ipt_code', u_int8_t),
('ipt_len', u_int8_t),
('ipt_ptr', u_int8_t),
('ipt_flg', c_uint, 4),
('ipt_oflw', c_uint, 4),
('data', u_int32_t * 9),
]
class tcphdr(Structure):
pass
tcphdr._fields_ = [
('source', u_int16_t),
('dest', u_int16_t),
('seq', u_int32_t),
('ack_seq', u_int32_t),
('res1', u_int16_t, 4),
('doff', u_int16_t, 4),
('fin', u_int16_t, 1),
('syn', u_int16_t, 1),
('rst', u_int16_t, 1),
('psh', u_int16_t, 1),
('ack', u_int16_t, 1),
('urg', u_int16_t, 1),
('res2', u_int16_t, 2),
('window', u_int16_t),
('check', u_int16_t),
('urg_ptr', u_int16_t),
]
# values for unnamed enumeration
# values for enumeration 'tcp_ca_state'
tcp_ca_state = c_int # enum
class tcp_info(
pass
tcp_info._fields_ = [
('tcpi_state', u_int8_t),
('tcpi_
('tcpi_
('tcpi_probes', u_int8_t),
('tcpi_
('tcpi_
('tcpi_
('tcpi_
('tcpi_rto', u_int32_t),
('tcpi_ato', u_int32_t),
('tcpi_
('tcpi_
('tcpi_
('tcpi_sacked', u_int32_t),
('tcpi_lost', u_int32_t),
('tcpi_
('tcpi_
('tcpi_
('tcpi_
('tcpi_
('tcpi_
('tcpi_pmtu', u_int32_t),
('tcpi_
('tcpi_rtt', u_int32_t),
('tcpi_rttvar', u_int32_t),
('tcpi_
('tcpi_
('tcpi_advmss', u_int32_t),
('tcpi_
('tcpi_
('tcpi_
('tcpi_
]
class tcp_md5sig(
pass
tcp_md5sig._fields_ = [
('tcpm_addr', sockaddr_storage),
('__tcpm_pad1', u_int16_t),
('tcpm_keylen', u_int16_t),
('__tcpm_pad2', u_int32_t),
('tcpm_key', u_int8_t * 80),
]
int8_t = c_int8
int16_t = c_int16
int32_t = c_int32
int64_t = c_int64
uint64_t = c_uint64
int_least8_t = c_byte
int_least16_t = c_short
int_least32_t = c_int
int_least64_t = c_longlong
uint_least8_t = c_ubyte
uint_least16_t = c_ushort
uint_least32_t = c_uint
uint_least64_t = c_ulonglong
int_fast8_t = c_byte
int_fast16_t = c_int
int_fast32_t = c_int
int_fast64_t = c_longlong
uint_fast8_t = c_ubyte
uint_fast16_t = c_uint
uint_fast32_t = c_uint
uint_fast64_t = c_ulonglong
intptr_t = c_int
uintptr_t = c_uint
intmax_t = c_longlong
uintmax_t = c_ulonglong
sigset_t = __sigset_t
__fd_mask = c_long
class fd_set(Structure):
pass
fd_set._fields_ = [
('fds_bits', __fd_mask * 32),
]
fd_mask = __fd_mask
class osockaddr(
pass
osockaddr._fields_ = [
('sa_family', c_ushort),
('sa_data', c_ubyte * 14),
]
# values for unnamed enumeration
u_char = __u_char
u_int = __u_int
u_long = __u_long
quad_t = __quad_t
u_quad_t = __u_quad_t
fsid_t = __fsid_t
loff_t = __loff_t
ino_t = __ino_t
ino64_t = __ino64_t
dev_t = __dev_t
mode_t = __mode_t
nlink_t = __nlink_t
off_t = __off_t
off64_t = __off64_t
id_t = __id_t
ssize_t = __ssize_t
daddr_t = __daddr_t
caddr_t = __caddr_t
key_t = __key_t
useconds_t = __useconds_t
suseconds_t = __suseconds_t
ulong = c_ulong
ushort = c_ushort
uint = c_uint
u_int64_t = c_ulonglong
register_t = c_int
blksize_t = __blksize_t
blkcnt_t = __blkcnt_t
fsblkcnt_t = __fsblkcnt_t
fsfilcnt_t = __fsfilcnt_t
blkcnt64_t = __blkcnt64_t
fsblkcnt64_t = __fsblkcnt64_t
fsfilcnt64_t = __fsfilcnt64_t
clock_t = __clock_t
time_t = __time_t
clockid_t = __clockid_t
timer_t = __timer_t
class timespec(
pass
timespec._fields_ = [
('tv_sec', __time_t),
('tv_nsec', c_long),
]
__all__ = ['__uint16_t', '__pthread_
'dev_t', 'int32_t', 'SOCK_RDM', 'IPPROTO_AH', 'u_int64_t',
'TCP', 'RTU', '__fsfilcnt64_t', 'IPPORT_SUPDUP',
'iovec', '__fsblkcnt_t', 'error_handling_t', '__rlim_t',
'ulong', 'IPPROTO_ROUTING', 'N16pthread_