from bin.func import *


class serial_flowMeter(serial.Serial):
    def __init__(self, port, baudrate=9600):
        super().__init__()
        self.port = port
        self.baudrate = baudrate
        self.err = []
        self.opened()

    def send(self, data):
        if not isinstance(data, str):
            self.err.append('{} | "send" type(str) Err {}'.format(time_now(),str(data)))
            print(self.err[-1])
            return False

        if len(data) % 2 != 0:
            self.err.append('{} | "send" length(data) Err {}'.format(time_now(), str(data)))
            print(self.err[-1])
            return False

        if not self.is_open:
            self.opened()

        try:
            data = unhexlify(data.encode())
            self.write(data)
            self.flush()
            return True
        except Exception as e:
            self.err.append('{} | {} Err {}'.format(time_now(), str(e), str(data)))
            return False

    def recv(self,waiting_time = 30):
        '''默认等待读取时间为6秒'''
        if not self.is_open:
            self.opened()
        try:
            for i in range(30):
                in_waitings = self.in_waiting
                if in_waitings:
                    return True
                time.sleep(1)
        except Exception as e:
            self.err.append('{} | {} Err'.format(time_now(), str(e)))

    def recv_parse(self, data):
        parse_data = ''
        try:
            parse_data = hexlify(parse_data).decode().upper()
        except: pass

        try:
            parse_data = data.decode('ascii')
        except: pass

        try:
            parse_data = data.decode('GBK').replace('\n', '').replace('\r', '')
        except: pass

        return parse_data

    def opened(self):
        if not self.is_open:
            self.open()

    def closed(self):
        if self.is_open:
            self.close()


65761e2233728602f28aff30ae5ecf2cc03bf30d
<mask token>


class serial_flowMeter(serial.Serial):

    def __init__(self, port, baudrate=9600):
        super().__init__()
        self.port = port
        self.baudrate = baudrate
        self.err = []
        self.opened()

    def send(self, data):
        if not isinstance(data, str):
            self.err.append('{} | "send" type(str) Err {}'.format(time_now(
                ), str(data)))
            print(self.err[-1])
            return False
        if len(data) % 2 != 0:
            self.err.append('{} | "send" length(data) Err {}'.format(
                time_now(), str(data)))
            print(self.err[-1])
            return False
        if not self.is_open:
            self.opened()
        try:
            data = unhexlify(data.encode())
            self.write(data)
            self.flush()
            return True
        except Exception as e:
            self.err.append('{} | {} Err {}'.format(time_now(), str(e), str
                (data)))
            return False
    <mask token>

    def recv_parse(self, data):
        parse_data = ''
        try:
            parse_data = hexlify(parse_data).decode().upper()
        except:
            pass
        try:
            parse_data = data.decode('ascii')
        except:
            pass
        try:
            parse_data = data.decode('GBK').replace('\n', '').replace('\r', '')
        except:
            pass
        return parse_data

    def opened(self):
        if not self.is_open:
            self.open()
    <mask token>

<mask token>


class serial_flowMeter(serial.Serial):

    def __init__(self, port, baudrate=9600):
        super().__init__()
        self.port = port
        self.baudrate = baudrate
        self.err = []
        self.opened()

    def send(self, data):
        if not isinstance(data, str):
            self.err.append('{} | "send" type(str) Err {}'.format(time_now(
                ), str(data)))
            print(self.err[-1])
            return False
        if len(data) % 2 != 0:
            self.err.append('{} | "send" length(data) Err {}'.format(
                time_now(), str(data)))
            print(self.err[-1])
            return False
        if not self.is_open:
            self.opened()
        try:
            data = unhexlify(data.encode())
            self.write(data)
            self.flush()
            return True
        except Exception as e:
            self.err.append('{} | {} Err {}'.format(time_now(), str(e), str
                (data)))
            return False

    def recv(self, waiting_time=30):
        """默认等待读取时间为6秒"""
        if not self.is_open:
            self.opened()
        try:
            for i in range(30):
                in_waitings = self.in_waiting
                if in_waitings:
                    return True
                time.sleep(1)
        except Exception as e:
            self.err.append('{} | {} Err'.format(time_now(), str(e)))

    def recv_parse(self, data):
        parse_data = ''
        try:
            parse_data = hexlify(parse_data).decode().upper()
        except:
            pass
        try:
            parse_data = data.decode('ascii')
        except:
            pass
        try:
            parse_data = data.decode('GBK').replace('\n', '').replace('\r', '')
        except:
            pass
        return parse_data

    def opened(self):
        if not self.is_open:
            self.open()
    <mask token>

<mask token>


class serial_flowMeter(serial.Serial):

    def __init__(self, port, baudrate=9600):
        super().__init__()
        self.port = port
        self.baudrate = baudrate
        self.err = []
        self.opened()

    def send(self, data):
        if not isinstance(data, str):
            self.err.append('{} | "send" type(str) Err {}'.format(time_now(
                ), str(data)))
            print(self.err[-1])
            return False
        if len(data) % 2 != 0:
            self.err.append('{} | "send" length(data) Err {}'.format(
                time_now(), str(data)))
            print(self.err[-1])
            return False
        if not self.is_open:
            self.opened()
        try:
            data = unhexlify(data.encode())
            self.write(data)
            self.flush()
            return True
        except Exception as e:
            self.err.append('{} | {} Err {}'.format(time_now(), str(e), str
                (data)))
            return False

    def recv(self, waiting_time=30):
        """默认等待读取时间为6秒"""
        if not self.is_open:
            self.opened()
        try:
            for i in range(30):
                in_waitings = self.in_waiting
                if in_waitings:
                    return True
                time.sleep(1)
        except Exception as e:
            self.err.append('{} | {} Err'.format(time_now(), str(e)))

    def recv_parse(self, data):
        parse_data = ''
        try:
            parse_data = hexlify(parse_data).decode().upper()
        except:
            pass
        try:
            parse_data = data.decode('ascii')
        except:
            pass
        try:
            parse_data = data.decode('GBK').replace('\n', '').replace('\r', '')
        except:
            pass
        return parse_data

    def opened(self):
        if not self.is_open:
            self.open()

    def closed(self):
        if self.is_open:
            self.close()

from bin.func import *


class serial_flowMeter(serial.Serial):

    def __init__(self, port, baudrate=9600):
        super().__init__()
        self.port = port
        self.baudrate = baudrate
        self.err = []
        self.opened()

    def send(self, data):
        if not isinstance(data, str):
            self.err.append('{} | "send" type(str) Err {}'.format(time_now(
                ), str(data)))
            print(self.err[-1])
            return False
        if len(data) % 2 != 0:
            self.err.append('{} | "send" length(data) Err {}'.format(
                time_now(), str(data)))
            print(self.err[-1])
            return False
        if not self.is_open:
            self.opened()
        try:
            data = unhexlify(data.encode())
            self.write(data)
            self.flush()
            return True
        except Exception as e:
            self.err.append('{} | {} Err {}'.format(time_now(), str(e), str
                (data)))
            return False

    def recv(self, waiting_time=30):
        """默认等待读取时间为6秒"""
        if not self.is_open:
            self.opened()
        try:
            for i in range(30):
                in_waitings = self.in_waiting
                if in_waitings:
                    return True
                time.sleep(1)
        except Exception as e:
            self.err.append('{} | {} Err'.format(time_now(), str(e)))

    def recv_parse(self, data):
        parse_data = ''
        try:
            parse_data = hexlify(parse_data).decode().upper()
        except:
            pass
        try:
            parse_data = data.decode('ascii')
        except:
            pass
        try:
            parse_data = data.decode('GBK').replace('\n', '').replace('\r', '')
        except:
            pass
        return parse_data

    def opened(self):
        if not self.is_open:
            self.open()

    def closed(self):
        if self.is_open:
            self.close()

from bin.func import *


class serial_flowMeter(serial.Serial):
    def __init__(self, port, baudrate=9600):
        super().__init__()
        self.port = port
        self.baudrate = baudrate
        self.err = []
        self.opened()

    def send(self, data):
        if not isinstance(data, str):
            self.err.append('{} | "send" type(str) Err {}'.format(time_now(),str(data)))
            print(self.err[-1])
            return False

        if len(data) % 2 != 0:
            self.err.append('{} | "send" length(data) Err {}'.format(time_now(), str(data)))
            print(self.err[-1])
            return False

        if not self.is_open:
            self.opened()

        try:
            data = unhexlify(data.encode())
            self.write(data)
            self.flush()
            return True
        except Exception as e:
            self.err.append('{} | {} Err {}'.format(time_now(), str(e), str(data)))
            return False

    def recv(self,waiting_time = 30):
        '''默认等待读取时间为6秒'''
        if not self.is_open:
            self.opened()
        try:
            for i in range(30):
                in_waitings = self.in_waiting
                if in_waitings:
                    return True
                time.sleep(1)
        except Exception as e:
            self.err.append('{} | {} Err'.format(time_now(), str(e)))

    def recv_parse(self, data):
        parse_data = ''
        try:
            parse_data = hexlify(parse_data).decode().upper()
        except: pass

        try:
            parse_data = data.decode('ascii')
        except: pass

        try:
            parse_data = data.decode('GBK').replace('\n', '').replace('\r', '')
        except: pass

        return parse_data

    def opened(self):
        if not self.is_open:
            self.open()

    def closed(self):
        if self.is_open:
            self.close()

