-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
55 lines (48 loc) · 1.71 KB
/
__init__.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
# -*- coding: utf-8 -*-
import os,sys
import serial #for port opening
import sys #for exceptions
import time
#from app import config
class Serial:
def __init__(self, port=None, baudrate=None, timeout=20,stopbits=None,bytesize=None,parity=None,waiting=False):
''' Serial connection with pyserial
:input: port,baudrate,timeout
:output: connection
'''
try:
if waiting is False:
self.port = serial.Serial(port = port, baudrate=baudrate,
timeout=timeout, writeTimeout=timeout,stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE)
else:
self.port = serial.Serial(port = port, baudrate=baudrate,
timeout=timeout, writeTimeout=timeout,stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE)
self.port.in_waiting
except Exception as e:
self.error = e
self.port = None
def open(self):
''' Open the serial port.'''
if self.port = None:
return self.error
self.port.open()
def close(self):
''' Close the serial port.'''
if self.port = None:
return self.error
self.port.close()
def send(self, msg):
''' Send message to serial port '''
if self.port = None:
return self.error
self.port.write(msg)
def receive(self,numberline=None):
''' Receive message from serial port '''
if self.port = None:
return self.error
if numberline is None:
return self.port.readline()
else:
return self.port.read(numberline)