Need help using modbus_tk library - modbus-tk

I'm using modbus_tk library to use as a Modbus RTU slave. I have an off the shelf Modbus RTU master simulator running on another PC through a usb to 485 converter. I cannot see my holding register in the Master.
I have verified that the serial link is good because I can send strings to the slave using a serial program. I have tried setting up the Master for 16 and 32 bit ints the response is always 83 04.
I have tried using a few different masters with staring address of 0, this one happens to default to first register 40001. Baud rates and serial port setting match.
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serial
import time
modbusServ = modbus_rtu.RtuServer(serial.Serial('/dev/ttyS0'),baudrate= 9600,
bytesize=8, parity='N', stopbits=1, xonxoff=0)
print("start")
modbusServ.start()
slave_1 = modbus_tk.modbus.Slave(1)
slave_1.add_block("BlockName", modbus_tk.defines.HOLDING_REGISTERS, 40001, 10)
aa= (1,2,3,4,5,6,7,8,9,10) # data in the register
while True:
slave_1.set_values ("BlockName", 40001, aa)
time.sleep(0.5)

First off, I don't see any reason for you to keep updating the values on your
"BlockName" in the loop, but maybe you have one.
The numbering of your register also seems to be wrong, you don't need to define register 0 as number 40001, you can replace these lines:
slave_1.add_block("BlockName", modbus_tk.defines.HOLDING_REGISTERS, 40001, 10)
slave_1.set_values ("BlockName", 40001, aa)
With:
slave_1.add_block("BlockName", cst.HOLDING_REGISTERS, 0, 10)
slave_1.set_values ("BlockName", 0, aa)
There is also a small problem in the way you instantiate your data block and slave.
So a complete slave example should look like this:
import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
import serial
import time
modbusServ = modbus_rtu.RtuServer(serial.Serial('/dev/ttyS0'),baudrate= 9600,
bytesize=8, parity='N', stopbits=1, xonxoff=0)
print("start")
modbusServ.start()
slave_1 = modbusServ.add_slave(1)
slave_1.add_block("BlockName", cst.HOLDING_REGISTERS, 0, 10)
aa= (1,2,3,4,5,6,7,8,9,10) # data in the register
#you need to get a new handler to write values to your slave
slave = modbusServ.get_slave(1)
slave.set_values ("BlockName", 0, aa)
while True:
print("Modbus Server Waiting for client queries...")
time.sleep(0.5)
There is a full slave example with command line arguments and all included with the code: https://github.com/ljean/modbus-tk/blob/master/examples/rtuslave_example.py
To be consistent with the register numbering, in your client you have to read from register 0 too:
master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 10)
Resulting in: (1,2,3,4,5,6,7,8,9,10)
I guess you already know but there are some other good libraries to work with Modbus like pymodbus and pylibmodbus.
EDIT: After testing I had to correct my complete example to add
slave = modbusServ.get_slave(1)
Apparently, you cannot use the original slave_1 as a handler to write values on your salve, instead you have to call function modbusServ.get_slave(slave_id)

Related

Pixhawk controller won't pick up every message

I have been trying to inject raw UBX data which I gather from UBXReader library into my Pixhawk. For this, I use a GPS module to extract UBX data and a serial to USB converter to stream data into my Pixhawk. Here is what my setup looks like:
Using my other USB port, I gather GPS data and try to stream it into pixhawk as seen above. For this task, I use python.
from serial import Serial
from pyubx2 import UBXReader
stream = Serial('/dev/ttyUSB0', 38400, timeout=3)
stream2 = Serial('/dev/ttyUSB1', 38400, timeout=3)
while 1:
ubr = UBXReader(stream)
(raw_data, parsed_data) = ubr.read()
output = parsed_data.serialize()
stream2.write(output)
From MAVLink, I can see location and altitude data but I fail to stream HDOP and VDOP messages into my Pixhawk. What might be causing this and how should I proceed on fixing it?
You need to ensure your external GPS is outputting the requisite UBX message types - not all UBX messages contain hDOP or vDOP data. For example:
The NAV-PVT message contains lat, lon and pDOP but not vDOP or hDOP.
The NAV-DOP message contains hDOP and vDOP but not lat or lon.
So you may need to enable the NAV-DOP message on your external GPS using uCenter or PyGPSClient.

Can not read data from uart with micropython and SIM7070G NB on ESP32 controller

I try to get my SIM7070G Cat-M/NB-IoT/GPRS HAT running with micropython on a ESP32 MC via UART. Unfortunately I did not find any libraries but I thought this can not be too difficult with micropython. I am working on this problem now for 3 days and do not get any response when sending commands with uart.
USB with computer:
Sending AT commands gives an answer like sending AT and receiving OK.
Micropython:
from machine import UART
from time import sleep
sleep(1)
print("activate")
p = Pin(27, Pin.OUT, Pin.PULL_UP)
sleep(0.1)
p.on()
sleep(1)
p.off()
sleep(0.5)
print("activated")
uart = UART(1, 115200,rx=9,tx=10,timeout=5000)
#uart.init(9600, bits=8, parity=None,rx=25,tx=26,stop=1)
uart.write(b'AT\r\n')
print("uart.write(b'AT\r\n')")
sleep(1)
data = uart.any()
print(str(data))
I just do not get a response. data is always 0.
What I tried:
checked connection 100 times, TX->RX and RX->TX, 5V, GND, PWR
different pins did not work
different baudrate... no difference.
Anyone a solution? That would be really great.
Link to manufacturer of SIM7070G HAT
I figured out the solution. As #hcheung sais, I have to call AT command a few times (up until 10 times) to let the module get the baudrate. It will work than properly.

Receive data from host computer using Circuit Python on Circuit Playground Express

I am using a Circuit Playground Express from Adafruit, and I'm programming it with Circuit Python.
I want to read data transmitted from the computer to which the Circuit Playground Express is connected via USB. Using input() works fine, but I would rather get the buffer of the serial instead, so that the loop would go on while there's no input. Something like serial.read().
import serial does not work on Circuit Python, or maybe I must install something. Is there anything else I could do to read the serial buffer using Circuit Python?
Aiden's answer lead me in the right direction and I found a nice (and slightly different) example of how to use supervisor.runtime.serial_bytes_available from Adafruit here (specifically lines 89-95): https://learn.adafruit.com/AT-Hand-Raiser/circuitpython-code
A minimum working example for code.py that takes the input and formats a new string in the form "RX: string" is
import supervisor
print("listening...")
while True:
if supervisor.runtime.serial_bytes_available:
value = input().strip()
# Sometimes Windows sends an extra (or missing) newline - ignore them
if value == "":
continue
print("RX: {}".format(value))
Tested on: Adafruit CircuitPython 4.1.0 on 2019-08-02; Adafruit ItsyBitsy M0 Express with samd21g18. Messages sent using the serial connection in mu-editor on macOS.
Sample output
main.py output:
listening...
hello!
RX: hello!
I got a simple example to work based on above posts, not sure how stable or useful it is, but still posting it here:
CircuitPython Code:
import supervisor
while True:
if supervisor.runtime.serial_bytes_available:
value = input().strip()
print(f"Received: {value}\r")
PC Code
import time
import serial
ser = serial.Serial('COM6', 115200) # open serial port
command = b'hello\n\r'
print(f"Sending Command: [{command}]")
ser.write(command) # write a string
ended = False
reply = b''
for _ in range(len(command)):
a = ser.read() # Read the loopback chars and ignore
while True:
a = ser.read()
if a== b'\r':
break
else:
reply += a
time.sleep(0.01)
print(f"Reply was: [{reply}]")
ser.close()
c:\circuitpythontest>python TEST1.PY
Sending Command: [b'hello\n\r']
Reply was: [b'Received: hello']
This is now somewhat possible!
In the January stable release of CircuitPython 3.1.2 the function serial_bytes_available was added to the supervisor module.
This allows you to poll the availability of serial bytes.
For example in the CircuitPython firmware (i.e. boot.py) a serial echo example would be:
import supervisor
def serial_read():
if supervisor.runtime.serial_bytes_available():
value = input()
print(value)
and ensure when you create the serial device object on the host side you set the timeout wait to be very small (i.e. 0.01).
i.e in python:
import serial
ser = serial.Serial(
'/dev/ttyACM0',
baudrate=115200,
timeout=0.01)
ser.write(b'HELLO from CircuitPython\n')
x = ser.readlines()
print("received: {}".format(x))

Bluetooth Serial between Raspberry Pi 3 / Zero W and Arduino / HM-10

I am trying to establish a bluetooth serial communication link between a Raspberry Pi Zero W, running Raspbian Jessie [03-07-2017], and an Arduino (UNO).
I am currently able to write data to the Arduino using bluetoothctl.
The application requires that we are able to write data to a particular BLE Slave. There are multiple [HM-10] Slaves to switch between, the Slave needs to be chosen during the program execution.
There is no BAUD rate preference. Currently, we are using 9600 universally.
Functions have been created that automatically connect and then write data to an "attribute", this shows up as data on the Serial Monitor of the Arduino.
Python Code - using BlueZ 5.44 (manually installed):
import subprocess
from subprocess import Popen, PIPE
# Replaces the ':' with '_' to allow the MacAddress to be in the form
# of a "Path" when "selecting an attribute"
def changeMacAddr(word):
return ''.join(c if c != ':' else '_' for c in word)
# Connects to a given MacAddress and then selects the attribute to write to
def connBT(BTsubProcess, stringMacAddr):
BTsubProcess.stdin.write(bytes("".join("connect "+stringMacAddr +"\n"), "utf-8"))
BTsubProcess.stdin.flush()
time.sleep(2)
stringFormat = changeMacAddr(stringMacAddr)
BTsubProcess.stdin.write(bytes("".join("select-attribute /org/bluez/hci0/dev_"
+ stringFormat +
"/service0010/char0011" + "\n"), "utf-8"))
BTsubProcess.stdin.flush()
# Can only be run once connBT has run - writes the data in a list [must have numbers 0 - 255 ]
def writeBT(BTsubProcess, listOfData):
stringList = [str('{0} ').format(elem) for elem in listOfData]
BTsubProcess.stdin.write(bytes("".join("write " + "".join(stringList) + "\n"), "utf-8"))
BTsubProcess.stdin.flush()
# Disconnects
def clostBT(BTsubProcess):
BTsubProcess.communicate(bytes("disconnect\n", "utf-8"))
# To use the functions a subprocess "instance" of bluetoothctl must be made
blt = subprocess.Popen(["bluetoothctl"], stdin=subprocess.PIPE, shell=True)
# blt with then be passed into the function for BTsubProcess
# Note: the MacAddresses of the Bluetooth modules were pre-connected and trusted manually via bluetoothctl
This method works fine for small sets of data, but my requirements require me to stream data to the Arduino very quickly.
The current set up is:
Sensor data (accelerometer, EEG) via USB serial is received by the Pi
The Pi processes the data
Commands are then sent to the Arduino via the in built bluetooth of the Pi Zero W
However, while using this method the bluetooth data transmission would delay (temporarily freeze) when the sensor data changed.
The data transmission was flawless when using two pre-paired HM-10 modules, the Pi's GPIO serial port was configured using PySerial.
The following methods have also been tried:
Using WiringPi to set-up a bluetooth serial port on the /dev/ttyAMA0
using Python sockets and rfcomm
When attempting to use both of these methods. The Python code compiles, however, once the Serial Port is opened the data is seemingly not written and does not show up on the Arduino's Serial Monitor.
This then cripples the previous functions. Even when using bluetoothctl manually, the module cannot be unpaired/disconnected. Writing to the appropriate attribute does not work either.
A restart is required to regain normal function.
Is this approach correct?
Is there a better way to send data over BLE?
UPDATE: 05/07/2017
I am no longer working on this project. But troubleshooting has led me to believe that a "race condition" in the code may have led to the program not functioning as intended.
This was verified during the testing phase where a more barebones code was created that functioned very well.

Extend existing Twisted Service with another Socket/TCP/RPC Service to get Service informations

I'm implementing a Twisted-based Heartbeat Client/Server combo, based on this example. It is my first Twisted project.
Basically it consists of a UDP Listener (Receiver), who calls a listener method (DetectorService.update) on receiving packages. The DetectorService always holds a list of currently active/inactive clients (I extended the example a lot, but the core is still the same), making it possible to react on clients which seem disconnected for a specified timeout.
This is the source taken from the site:
UDP_PORT = 43278; CHECK_PERIOD = 20; CHECK_TIMEOUT = 15
import time
from twisted.application import internet, service
from twisted.internet import protocol
from twisted.python import log
class Receiver(protocol.DatagramProtocol):
"""Receive UDP packets and log them in the clients dictionary"""
def datagramReceived(self, data, (ip, port)):
if data == 'PyHB':
self.callback(ip)
class DetectorService(internet.TimerService):
"""Detect clients not sending heartbeats for too long"""
def __init__(self):
internet.TimerService.__init__(self, CHECK_PERIOD, self.detect)
self.beats = {}
def update(self, ip):
self.beats[ip] = time.time()
def detect(self):
"""Log a list of clients with heartbeat older than CHECK_TIMEOUT"""
limit = time.time() - CHECK_TIMEOUT
silent = [ip for (ip, ipTime) in self.beats.items() if ipTime < limit]
log.msg('Silent clients: %s' % silent)
application = service.Application('Heartbeat')
# define and link the silent clients' detector service
detectorSvc = DetectorService()
detectorSvc.setServiceParent(application)
# create an instance of the Receiver protocol, and give it the callback
receiver = Receiver()
receiver.callback = detectorSvc.update
# define and link the UDP server service, passing the receiver in
udpServer = internet.UDPServer(UDP_PORT, receiver)
udpServer.setServiceParent(application)
# each service is started automatically by Twisted at launch time
log.msg('Asynchronous heartbeat server listening on port %d\n'
'press Ctrl-C to stop\n' % UDP_PORT)
This heartbeat server runs as a daemon in background.
Now my Problem:
I need to be able to run a script "externally" to print the number of offline/online clients on the console, which the Receiver gathers during his lifetime (self.beats). Like this:
$ pyhb showactiveclients
3 clients online
$ pyhb showofflineclients
1 client offline
So I need to add some kind of additional server (Socket, Tcp, RPC - it doesn't matter. the main point is that i'm able to build a client-script with the above behavior) to my DetectorService, which allows to connect to it from outside. It should just give a response to a request.
This server needs to have access to the internal variables of the running detectorservice instance, so my guess is that I have to extend the DetectorService with some kind of additionalservice.
After some hours of trying to combine the detectorservice with several other services, I still don't have an idea what's the best way to realize that behavior. So I hope that somebody can give me at least the essential hint how to start to solve this problem.
Thanks in advance!!!
I think you already have the general idea of the solution here, since you already applied it to an interaction between Receiver and DetectorService. The idea is for your objects to have references to other objects which let them do what they need to do.
So, consider a web service that responds to requests with a result based on the beats data:
from twisted.web.resource import Resource
class BeatsResource(Resource):
# It has no children, let it respond to the / URL for brevity.
isLeaf = True
def __init__(self, detector):
Resource.__init__(self)
# This is the idea - BeatsResource has a reference to the detector,
# which has the data needed to compute responses.
self._detector = detector
def render_GET(self, request):
limit = time.time() - CHECK_TIMEOUT
# Here, use that data.
beats = self._detector.beats
silent = [ip for (ip, ipTime) in beats.items() if ipTime < limit]
request.setHeader('content-type', 'text/plain')
return "%d silent clients" % (len(silent),)
# Integrate this into the existing application
application = service.Application('Heartbeat')
detectorSvc = DetectorService()
detectorSvc.setServiceParent(application)
.
.
.
from twisted.web.server import Site
from twisted.application.internet import TCPServer
# The other half of the idea - make sure to give the resource that reference
# it needs.
root = BeatsResource(detectorSvc)
TCPServer(8080, Site(root)).setServiceParent(application)