1425bb815Sopenharmony_ci#!/usr/bin/env python 2425bb815Sopenharmony_ci 3425bb815Sopenharmony_ci# Copyright JS Foundation and other contributors, http://js.foundation 4425bb815Sopenharmony_ci# 5425bb815Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 6425bb815Sopenharmony_ci# you may not use this file except in compliance with the License. 7425bb815Sopenharmony_ci# You may obtain a copy of the License at 8425bb815Sopenharmony_ci# 9425bb815Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 10425bb815Sopenharmony_ci# 11425bb815Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 12425bb815Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS 13425bb815Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14425bb815Sopenharmony_ci# See the License for the specific language governing permissions and 15425bb815Sopenharmony_ci# limitations under the License. 16425bb815Sopenharmony_ci 17425bb815Sopenharmony_ciimport select 18425bb815Sopenharmony_ciimport serial 19425bb815Sopenharmony_ci 20425bb815Sopenharmony_ciclass Serial(object): 21425bb815Sopenharmony_ci """ Create a new socket using the given address family, socket type and protocol number. """ 22425bb815Sopenharmony_ci def __init__(self, serial_config): 23425bb815Sopenharmony_ci config = serial_config.split(',') 24425bb815Sopenharmony_ci config_size = len(config) 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_ci port = config[0] if config_size > 0 else "/dev/ttyUSB0" 27425bb815Sopenharmony_ci baudrate = config[1] if config_size > 1 else 115200 28425bb815Sopenharmony_ci bytesize = int(config[2]) if config_size > 2 else 8 29425bb815Sopenharmony_ci parity = config[3] if config_size > 3 else 'N' 30425bb815Sopenharmony_ci stopbits = int(config[4]) if config_size > 4 else 1 31425bb815Sopenharmony_ci 32425bb815Sopenharmony_ci self.ser = serial.Serial(port=port, baudrate=baudrate, parity=parity, 33425bb815Sopenharmony_ci stopbits=stopbits, bytesize=bytesize, timeout=1) 34425bb815Sopenharmony_ci 35425bb815Sopenharmony_ci def connect(self): 36425bb815Sopenharmony_ci """ Connect to the server, write a 'c' to the serial port """ 37425bb815Sopenharmony_ci self.send_data('c') 38425bb815Sopenharmony_ci 39425bb815Sopenharmony_ci def close(self): 40425bb815Sopenharmony_ci """" close the serial port. """ 41425bb815Sopenharmony_ci self.ser.close() 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_ci def receive_data(self, max_size=1024): 44425bb815Sopenharmony_ci """ The maximum amount of data to be received at once is specified by max_size. """ 45425bb815Sopenharmony_ci return self.ser.read(max_size) 46425bb815Sopenharmony_ci 47425bb815Sopenharmony_ci def send_data(self, data): 48425bb815Sopenharmony_ci """ Write data to the serial port. """ 49425bb815Sopenharmony_ci return self.ser.write(data) 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ci def ready(self): 52425bb815Sopenharmony_ci """ Monitor the file descriptor. """ 53425bb815Sopenharmony_ci result = select.select([self.ser.fileno()], [], [], 0)[0] 54425bb815Sopenharmony_ci 55425bb815Sopenharmony_ci return self.ser.fileno() in result 56