This commit is contained in:
commit
647a5a2ce9
|
@ -0,0 +1,35 @@
|
|||
import os, subprocess, serial, time
|
||||
|
||||
# this script lets you emulate a serial device
|
||||
# the client program should use the serial port file specifed by client_port
|
||||
|
||||
# if the port is a location that the user can't access (ex: /dev/ttyUSB0 often),
|
||||
# sudo is required
|
||||
|
||||
class SerialEmulator(object):
|
||||
def __init__(self, device_port='./ttydevice', client_port='./ttyclient'):
|
||||
self.device_port = device_port
|
||||
self.client_port = client_port
|
||||
cmd=['/usr/bin/socat','-d','-d','PTY,link=%s,raw,echo=0' %
|
||||
self.device_port, 'PTY,link=%s,raw,echo=0' % self.client_port]
|
||||
self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
time.sleep(1)
|
||||
self.serial = serial.Serial(self.device_port, 9600, rtscts=True, dsrdtr=True)
|
||||
self.err = ''
|
||||
self.out = ''
|
||||
|
||||
def write(self, out):
|
||||
self.serial.write(out)
|
||||
|
||||
def read(self):
|
||||
line = ''
|
||||
while self.serial.inWaiting() > 0:
|
||||
line += self.serial.read(1)
|
||||
return line
|
||||
|
||||
def __del__(self):
|
||||
self.stop()
|
||||
|
||||
def stop(self):
|
||||
self.proc.kill()
|
||||
self.out, self.err = self.proc.communicate()
|
|
@ -0,0 +1,30 @@
|
|||
from pyLoraRFM9x import LoRa, ModemConfig
|
||||
from SerialEmulator import SerialEmulator
|
||||
emulator = SerialEmulator('./ttydevice','./ttyclient')
|
||||
|
||||
# This is our callback function that runs when a message is received
|
||||
def on_recv(payload):
|
||||
print("From:", payload.header_from)
|
||||
print("Received:", payload.message)
|
||||
print("RSSI: {}; SNR: {}".format(payload.rssi, payload.snr))
|
||||
emulator.write(payload.message)
|
||||
|
||||
|
||||
# Use chip select 1. GPIO pin 5 will be used for interrupts and set reset pin to 25
|
||||
# The address of this device will be set to 2
|
||||
lora = LoRa(1, 5, 2, reset_pin = 25, modem_config=ModemConfig.Bw125Cr45Sf128, tx_power=14, acks=True)
|
||||
lora.on_recv = on_recv
|
||||
|
||||
# Send a message to a recipient device with address 10
|
||||
# Retry sending the message twice if we don't get an acknowledgment from the recipient
|
||||
message = "Hello there!"
|
||||
while True:
|
||||
message = emulator.read()
|
||||
if message != '':
|
||||
status = lora.send_to_wait(message, 10, retries=2)
|
||||
if status is True:
|
||||
print("Message sent!")
|
||||
else:
|
||||
print("No acknowledgment from recipient")
|
||||
|
||||
emulator.stop()
|
Loading…
Reference in New Issue