Skip to content

added TCP support and fixed a bug in setUpdateInterval #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pypozyx/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ def setUpdateInterval(self, ms, remote_id=None):
"""
if not dataCheck(ms):
ms = SingleRegister(ms, size=2)
assert ms[0] > 100 and ms[
0] <= 600000, 'setUpdateInterval: ms not 100<ms<60000'
assert ms[0] > 10 and ms[
0] <= 600000, 'setUpdateInterval: ms not 10<ms<60000'
return self.setWrite(POZYX_POS_INTERVAL, ms, remote_id)

def setCoordinates(self, coordinates, remote_id=None):
Expand Down
40 changes: 37 additions & 3 deletions pypozyx/pozyx_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@
from pypozyx.lib import PozyxLib
from pypozyx.structures.generic import Data, SingleRegister
from serial import Serial
# os is used to get the wlan0 ip address
import os
# serial_for_url is needed for tcp socket support
from serial import serial_for_url
# weakref is used for retrieve all instances of pozyx devices
import weakref
# RLock based locking for multithread operations
import threading
from serial.tools.list_ports import comports

## \addtogroup auxiliary_serial
# @{

def get_wlan0_ip():
"""Return the wlan0 ip used for the TCP bridge"""
import os
ip_wlan0 = os.popen('ip addr show wlan0 | grep "\<inet\>" | awk \'{ print $2 }\' | awk -F "/" \'{ print $1 }\'').read().strip()
return ip_wlan0

def list_serial_ports():
"""Prints the open serial ports line per line"""
ports = comports()
Expand Down Expand Up @@ -66,14 +80,28 @@ class PozyxSerial(PozyxLib):
>>> pozyx = PozyxSerial(serial.tools.list_ports.comports()[0])
"""

# builds a list of all pozyx devices used in one script
instances = []

## \addtogroup core
# @{
def __init__(self, port, baudrate=115200, timeout=0.1, write_timeout=0.1, print_output=False, debug_trace=False):
def __init__(self, port=9001, baudrate=115200, timeout=0.1, write_timeout=0.1, print_output=False, debug_trace=False, tcp=False, ip=get_wlan0_ip()):
"""Initializes the PozyxSerial object. See above for details."""
self.__class__.instances.append(weakref.proxy(self))
self.print_output = print_output
self.baudrate = baudrate
self.ip = ip
self.port = port
self.url = "socket://" + self.ip + ":" + self.port
# threading lock for the serial exchange
self.rlock = threading.RLock()
try:
self.ser = Serial(port, baudrate, timeout=timeout,
write_timeout=write_timeout)
# adding TCP support
if tcp == True:
self.ser = serial_for_url(url=self.url, baudrate=self.baudrate, )
else:
self.ser = Serial(port, baudrate, timeout=timeout,
write_timeout=write_timeout)
except:
print(
"Couldn't connect with Pozyx, wrong/busy serial port, or pySerial not installed.")
Expand Down Expand Up @@ -144,8 +172,14 @@ def serialExchange(self, s):
Serial message the Pozyx returns, stripped from 'D,' at its start
and NL+CR at the end.
"""
self.rlock.acquire()
self.ser.write(s.encode())
self.rlock.release()

self.rlock.aqcuire()
response = self.ser.readline().decode()
self.rlock.release()

if self.print_output:
print('The response to %s is %s.' % (s.strip(), response.strip()))
if len(response) == 0:
Expand Down