Serial Interfacing

Example

import wiringpi
from time import sleep
import time

class LoRaWANForwarder(object):
  def __init__(self, serialdevice):
    wiringpi.wiringPiSetup()    # Use WiringPi numbering
    self.serial = wiringpi.serialOpen(serialdevice, 115200)  # Requires device/baud and returns an ID

  def read(self):
    while wiringpi.serialDataAvail(self.serial) > 0:
      value = (wiringpi.serialGetchar(self.serial))
      if value > 0:
        print("{}".format(chr(value)), end='', flush=True)

  def write(self, data):
    wiringpi.serialPutchar(self.serial, ord('['))   # Start of packet
    for c in data:
      print(c)
      wiringpi.serialPutchar(self.serial, ord(c))

    wiringpi.serialPutchar(self.serial, ord(']'))   # End of packet


## Main app
lora = LoRaWANForwarder('/dev/ttyACM0')

# Send message via serial every 10 seconds
start = end = time.time()
while True:
  # Print available data
  lora.read()
  sleep(0.1)
  end = time.time()
  if (end - start) > 10:
    start = end
    lora.write("12AA34")

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