ntrip_serial_device.py
Go to the documentation of this file.
1 
2 #!/usr/bin/env python
3 
4 import logging
5 import serial
6 
7 from .ntrip_base import NTRIPBase
8 
10 
11  def __init__(self, port, baudrate, logerr=logging.error, logwarn=logging.warning, loginfo=logging.info, logdebug=logging.debug):
12  # Call the parent constructor
13  super().__init__(logerr, logwarn, loginfo, logdebug)
14 
15  # Save the connection info
16  self._port = port
17  self._baudrate = baudrate
18 
19  # Initialize this so we don't throw an exception when closing
20  self._device = None
21 
22  def connect(self):
23  # Attempt to open the serial port
24  try:
25  self._device = serial.Serial(self._port, self._baudrate)
26  except Exception as e:
27  self._logerr('Unable to open serial port {} at baudrate {}'.format(self._port, self._baudrate))
28  self._logerr('Exception: {}'.format(str(e)))
29  return False
30 
31  # Right now, we can't check anything else, so assuming that the port is open, we succeeded.
32  self._loginfo('Connected to serial port {} at baudrate {}'.format(self._port, self._baudrate))
33  self._connected = True
34  return True
35 
36  def disconnect(self):
37  # Disconnect the serial port
38  self._connected = False
39  try:
40  if self._device:
41  self._device.close()
42  except Exception as e:
43  self._logdebug('Encountered exception when closing the serial port. This can likely be ignored.')
44  self._logdebug('Exception: {}'.format(str(e)))
45 
46  def send_nmea(self, sentence):
47  if not self._connected:
48  self._logwarn('NMEA sent before port was connected, discarding NMEA')
49  return
50 
51  # Not sure if this is the right thing to do, but python will escape the return characters at the end of the string, so do this manually
52  if sentence[-4:] == '\\r\\n':
53  sentence = sentence[:-4] + '\r\n'
54  elif sentence[-2:] != '\r\n':
55  sentence = sentence + '\r\n'
56 
57  # Check if it is a valid NMEA sentence
58  if not self.nmea_parser.is_valid_sentence(sentence):
59  self._logwarn("Invalid NMEA sentence, not sending to server")
60  return
61 
62  # Encode the data and send it to the device
63  try:
64  self._device.write(sentence.encode('utf-8'))
65  except Exception as e:
66  self._logerr('Unable to send NMEA sentence to device, reconnecting...')
67  self.reconnect()
68  self.send_nmea(sentence)
69 
70  def recv_rtcm(self):
71  if not self._connected:
72  self._logwarn('RTCM requested before device was connected, returning empty list')
73  return []
74 
75  # Check how much data is available on the device
76  if self._device.in_waiting:
77  try:
78  data = self._device.read_all()
79  self._logdebug('Read {} bytes'.format(len(data)))
80  return self.rtcm_parser.parse(data) if data else []
81  except Exception as e:
82  self._logerr('Unable to read RTCM from device, reconnecting...')
83  self.reconnect()
84  return []
85  else:
86  return []
ntrip_client.ntrip_serial_device.NTRIPSerialDevice._port
_port
Definition: ntrip_serial_device.py:16
ntrip_client.ntrip_base.NTRIPBase
Definition: ntrip_base.py:7
ntrip_client.ntrip_serial_device.NTRIPSerialDevice.recv_rtcm
def recv_rtcm(self)
Definition: ntrip_serial_device.py:70
ntrip_client.ntrip_base.NTRIPBase.reconnect
def reconnect(self)
Definition: ntrip_base.py:48
ntrip_client.ntrip_base.NTRIPBase.rtcm_parser
rtcm_parser
Definition: ntrip_base.py:21
ntrip_client.ntrip_serial_device.NTRIPSerialDevice._device
_device
Definition: ntrip_serial_device.py:20
ntrip_client.ntrip_serial_device.NTRIPSerialDevice.send_nmea
def send_nmea(self, sentence)
Definition: ntrip_serial_device.py:46
ntrip_client.ntrip_serial_device.NTRIPSerialDevice.disconnect
def disconnect(self)
Definition: ntrip_serial_device.py:36
ntrip_client.ntrip_serial_device.NTRIPSerialDevice
Definition: ntrip_serial_device.py:9
ntrip_client.ntrip_base.NTRIPBase._connected
_connected
Definition: ntrip_base.py:36
ntrip_client.ntrip_base.NTRIPBase._loginfo
_loginfo
Definition: ntrip_base.py:17
ntrip_client.ntrip_serial_device.NTRIPSerialDevice._baudrate
_baudrate
Definition: ntrip_serial_device.py:17
ntrip_client.ntrip_base.NTRIPBase._logerr
_logerr
Definition: ntrip_base.py:15
ntrip_client.ntrip_base.NTRIPBase.send_nmea
def send_nmea(self)
Definition: ntrip_base.py:66
ntrip_client.ntrip_serial_device.NTRIPSerialDevice.__init__
def __init__(self, port, baudrate, logerr=logging.error, logwarn=logging.warning, loginfo=logging.info, logdebug=logging.debug)
Definition: ntrip_serial_device.py:11
ntrip_client.ntrip_base.NTRIPBase._logwarn
_logwarn
Definition: ntrip_base.py:16
ntrip_client.ntrip_base.NTRIPBase.nmea_parser
nmea_parser
Definition: ntrip_base.py:27
ntrip_client.ntrip_serial_device.NTRIPSerialDevice.connect
def connect(self)
Definition: ntrip_serial_device.py:22
ntrip_client.ntrip_base.NTRIPBase._logdebug
_logdebug
Definition: ntrip_base.py:18


ntrip_client
Author(s): Parker Hannifin Corp
autogenerated on Sat Dec 21 2024 03:32:07