forked from bensherlock/nm3-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnm3logger.py
More file actions
210 lines (164 loc) · 8.04 KB
/
nm3logger.py
File metadata and controls
210 lines (164 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#! /usr/bin/env python
#
# NM V3 Logger
#
# This file is part of NM3 Python Driver. https://github.com/bensherlock/nm3-python-driver
#
#
# MIT License
#
# Copyright (c) 2019 Benjamin Sherlock <benjamin.sherlock@ncl.ac.uk>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
"""NM3 Acoustic Modem Logger using the Nm3 driver. """
import argparse
from datetime import datetime
import serial
from nm3driver import Nm3
from nm3driver import MessagePacket
class Nm3Logger:
def __init__(self):
self._nm3_modem = None
self._filename_root = None
def start_logging(self, nm3_modem, filename_root):
self._nm3_modem = nm3_modem
self._filename_root = filename_root
# Open a new logfile with current time
file_datetime = datetime.utcnow()
dt_str = file_datetime.strftime('%Y%m%dT%H%M%S')
filename = self._filename_root + '-' + dt_str + '.csv'
# 0=no buffer, 1=Line buffering, N=bytes buffering, None=system default
bufsize = 1
logfile = open(filename, 'w', buffering=bufsize)
# Write the header row to the logfile
self.write_header_line(logfile)
# Debug Code
# Send a test request so the remote node sends a broadcast message that we'll look at below.
# bytes_count = serial_port.write('$T007'.encode('utf-8'))
# Expecting '$T002\r\n' 7 bytes
# resp = serial_port.read(7)
packet_id = 0
# Receiving unicast and broadcast messages
while True:
# Check current time. We want to start a new logfile once the day changes.
current_datetime = datetime.utcnow()
if current_datetime.day != file_datetime.day:
# Start a new log file
logfile.close()
file_datetime = current_datetime
dt_str = file_datetime.strftime('%Y%m%dT%H%M%S')
filename = self._filename_root + '-' + dt_str + '.csv'
# 0=no buffer, 1=Line buffering, N=bytes buffering, None=system default
bufsize = 1
logfile = open(filename, 'w', buffering=bufsize)
# Write the header row to the logfile
self.write_header_line(logfile)
# Reset the packet id for the new file
packet_id = 0
# Periodically poll the serial port for bytes
# nm3_modem.poll_receiver_blocking() # blocking on first byte (or timeout)
nm3_modem.poll_receiver() # non-blocking returns immediately if no bytes ready to read.
# Periodically process any bytes received
nm3_modem.process_incoming_buffer()
# Periodically check for received packets
while nm3_modem.has_received_packet():
message_packet = nm3_modem.get_received_packet()
# Write to file
self.write_packet_line(logfile, message_packet, packet_id)
# Print to console
# payload_as_string = bytes(message_packet.packet_payload).decode('utf-8')
print('Received message packet: ' +
MessagePacket.PACKETTYPE_NAMES[message_packet.packet_type] +
' src: ' + str(message_packet.source_address) +
' dest: ' + str(message_packet.destination_address))
packet_id = packet_id + 1
def write_header_line(self, the_file):
"""Write the text header line to the given file."""
# Write the header row to the logfile
# PacketId, Timestamp, PacketType (Broadcast/Unicast), Source Address, Destination Address,
# PayloadLength, PayloadBytes(hex encoded)
# New Additions for V1.3 Firmware
# LQI, Doppler, TimestampCount
# \r\n
the_file.write('PacketId,Timestamp,PacketType,SourceAddress,DestinationAddress,PayloadLength,')
# Hex encoded payload bytes
for i in range(0, 64):
the_file.write('PayloadBytes[' + str(i) + ']')
if i < 63:
the_file.write(',')
# V1.3 Additions
the_file.write(',LQI,Doppler,TimestampCount')
# Newline
the_file.write('\n')
def write_packet_line(self, the_file, the_packet, packet_id):
"""Write the text packet line to the given file."""
# Write to file
# PacketId, Timestamp, PacketType (Broadcast/Unicast), Source Address,
# Destination Address, PayloadLength, PayloadBytes(hex encoded) \n
the_file.write(str(packet_id) + ',')
dt_str = datetime.utcnow().isoformat() + 'Z'
the_file.write(dt_str + ',')
the_file.write(MessagePacket.PACKETTYPE_NAMES[the_packet.packet_type] + ',')
if the_packet.source_address:
the_file.write('{:03d}'.format(the_packet.source_address))
the_file.write(',')
if the_packet.destination_address:
the_file.write('{:03d}'.format(the_packet.destination_address))
the_file.write(',')
the_file.write('{:02d}'.format(len(the_packet.packet_payload)) + ',')
# Hex encoded payload bytes
for i in range(0, 64):
if i < len(the_packet.packet_payload):
the_file.write('0x' + '{:02x}'.format(the_packet.packet_payload[i]))
if i < 63:
the_file.write(',')
# V1.3 Additions
# the_file.write(',LQI,Doppler,TimestampCount')
the_file.write(',')
the_file.write('{:02d}'.format(the_packet.packet_lqi))
the_file.write(',')
the_file.write('{:03d}'.format(the_packet.packet_doppler))
the_file.write(',')
the_file.write('{:014d}'.format(the_packet.packet_timestamp_count))
# Newline
the_file.write('\n')
def main():
"""Main Program Entry."""
cmdline_parser = argparse.ArgumentParser(description='NM3 Logger')
# Add Command Line Arguments
# Serial Port
cmdline_parser.add_argument('port', help='The serial port to connect to the NM3.')
# Filename root for logging - Rotates at start of each day (midnight UTC)
cmdline_parser.add_argument('filenameroot', help='The filename root to log to. {filenameroot}-20191204T161500.csv')
# Parse the command line
cmdline_args = cmdline_parser.parse_args()
# Get Arguments
port = cmdline_args.port
filenameroot = cmdline_args.filenameroot
# Serial Port is opened with a 100ms timeout for reading - non-blocking.
#serial_port = serial.Serial(port, 9600, 8, serial.PARITY_NONE, serial.STOPBITS_ONE, 0.1)
with serial.Serial(port, 9600, 8, serial.PARITY_NONE, serial.STOPBITS_ONE, 0.1) as serial_port:
nm3_modem = Nm3(input_stream=serial_port, output_stream=serial_port)
# Enable System Timer
nm3_modem.enable_system_timer()
nm3_logger = Nm3Logger()
nm3_logger.start_logging(nm3_modem=nm3_modem, filename_root=filenameroot)
if __name__ == '__main__':
main()