-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoraConnection.py
73 lines (62 loc) · 2.4 KB
/
LoraConnection.py
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
#!/usr/bin/env python
##
# @package LoraConnection Module contains methods to connect to a serial port
# on which LORA module is connected with the provided parameters
# and return an instance of the serial connection
#
import serial
from ModbusConsts import ModbusConsts
import logging
class LoraConnection:
LOG = logging.getLogger(__name__)
## LORA Serial port for communicating with the LORA devices
SERIAL_PORT = '/dev/ttyS3'
##
# Function to get the serial connection with the specified connection parameters
# @param baudratem int: Baudrate for the connection
# @param paritym string: Parity for the connection
# @param stopbitsm float: Stopbits for the connection
# @param bytesizem int: Byte size for the connection
#
# @return ser Serial connection instance with provided parameters
#
def getLoraConnection( self, baudratem, paritym, stopbitsm, bytesizem ):
if paritym is ModbusConsts.PARITY_NONE:
paritym = serial.PARITY_NONE
elif paritym is ModbusConsts.PARITY_EVEN:
paritym = serial.PARITY_EVEN
elif paritym is ModbusConsts.PARITY_ODD:
paritym = serial.PARITY_ODD
elif paritym is ModbusConsts.PARITY_MARK:
paritym = serial.PARITY_MARK
elif paritym is ModbusConsts.PARITY_SPACE:
paritym = serial.PARITY_SPACE
else:
paritym = serial.PARITY_NONE
if stopbitsm == ModbusConsts.SB_ONE:
stopbitsm = serial.STOPBITS_ONE
elif stopbitsm == ModbusConsts.SB_ONE_POINT_FIVE:
stopbitsm = serial.STOPBITS_ONE_POINT_FIVE
elif stopbitsm == ModbusConsts.SB_TWO:
stpbitsm = serial.STOPBITS_TWO
else:
stopbitsm = serial.STOPBITS_ONE
if bytesizem == ModbusConsts.BYTE_FIVE:
bytesizem = serial.FIVEBITS
elif bytesizem == ModbusConsts.BYTE_SIX:
bytesizem = serial.SIXBITS
elif bytesizem == ModbusConsts.BYTE_SEVEN:
bytesizem = serial.SEVENBITS
elif bytesizem == ModbusConsts.BYTE_EIGHT:
bytesizem = serial.EIGHTBITS
else:
bytesizem = serial.EIGHTBITS
ser = serial.Serial(
port=self.SERIAL_PORT,
baudrate = baudratem,
parity=paritym,
stopbits=stopbitsm,
bytesize=bytesizem,
timeout=1
)
return ser