예제 #1
0
파일: mymodbus.py 프로젝트: mattm55/genmon
    def __init__(self,
                 updatecallback,
                 address=0x9d,
                 name="/dev/serial",
                 rate=9600,
                 loglocation="/var/log/"):

        self.Address = address
        self.Threads = {}  # Dict of mythread objects
        self.DeviceInit = False
        self.CommAccessLock = threading.RLock(
        )  # lock to synchronize access to the serial port comms
        self.UpdateRegisterList = updatecallback
        # log errors in this module to a file
        self.log = mylog.SetupLogger("mymodbus", loglocation + "mymodbus.log")

        try:
            #Starting serial connection
            self.Slave = myserial.SerialDevice(name, rate, loglocation)
            self.DeviceInit = True

        except Exception as e1:
            self.FatalError("Error opening serial device: " + str(e1))
            return None

        try:
            # CRCMOD library, used for CRC calculations
            self.ModbusCrc = crcmod.predefined.mkCrcFun('modbus')
        except Exception as e1:
            self.FatalError("Unable to find crcmod package: " + str(e1))
예제 #2
0
    def __init__(self,
                 updatecallback,
                 address=0x9d,
                 name="/dev/serial",
                 rate=9600,
                 loglocation="/var/log/",
                 Parity=None,
                 OnePointFiveStopBits=None,
                 slowcpuoptimization=False):
        super(ModbusProtocol, self).__init__(updatecallback=updatecallback,
                                             address=address,
                                             name=name,
                                             rate=rate,
                                             loglocation=loglocation)

        try:

            self.SlowCPUOptimization = slowcpuoptimization
            # ~3000 for 9600               bit time * 10 bits * 10 char * 2 packets + wait time(3000) (convert to ms * 1000)
            self.ModBusPacketTimoutMS = (
                ((((1 / rate) * 10) * 10 * 2) * 1000) + 3000)  # .00208
            #Starting serial connection
            self.Slave = myserial.SerialDevice(
                name,
                rate,
                loglocation,
                Parity=Parity,
                OnePointFiveStopBits=OnePointFiveStopBits)
            self.Threads = self.MergeDicts(self.Threads, self.Slave.Threads)
            self.InitComplete = True

        except Exception as e1:
            self.FatalError("Error opening serial device: " + str(e1))
            return None

        try:
            # CRCMOD library, used for CRC calculations
            self.ModbusCrc = crcmod.predefined.mkCrcFun('modbus')
        except Exception as e1:
            self.FatalError("Unable to find crcmod package: " + str(e1))
예제 #3
0
    def __init__(self,
        updatecallback,
        address = 0x9d,
        name = "/dev/serial0",
        rate=9600,
        Parity = None,
        OnePointFiveStopBits = None,
        config = None):

        super(ModbusProtocol, self).__init__(updatecallback = updatecallback, address = address, name = name, rate = rate, config = config)

        try:

            # ~3000 for 9600               bit time * 10 bits * 10 char * 2 packets + wait time(3000) (convert to ms * 1000)
            self.ModBusPacketTimoutMS = (((((1/float(rate)) * 10.0) * 10.0 * 2.0) *1000.0)  + 3000.0)     # .00208

            self.ModBusPacketTimoutMS += self.AdditionalModbusTimeout * 1000.0


            if self.UseTCP:
                self.ModBusPacketTimoutMS = self.ModBusPacketTimoutMS
            #Starting serial connection
            if self.UseTCP:
                self.Slave = myserialtcp.SerialTCPDevice(config = self.config)
            else:
                self.Slave = myserial.SerialDevice(name = name, rate = rate, Parity = Parity, OnePointFiveStopBits = OnePointFiveStopBits, config = self.config)
            self.Threads = self.MergeDicts(self.Threads, self.Slave.Threads)


        except Exception as e1:
            self.LogErrorLine("Error opening serial device: " + str(e1))
            sys.exit(1)

        try:
            # CRCMOD library, used for CRC calculations
            self.ModbusCrc = crcmod.predefined.mkCrcFun('modbus')
            self.InitComplete = True
        except Exception as e1:
            self.FatalError("Unable to find crcmod package: " + str(e1))
예제 #4
0
    def __init__(self,
                 port="/dev/ttyAMA0",
                 rate=115200,
                 loglocation="/var/log/",
                 log=None,
                 localinit=False,
                 ConfigFilePath=None,
                 recipient=None):
        super(MyModem, self).__init__()

        self.MessagesSent = 0
        self.Errors = 0
        self.SendActive = False
        self.ModemLock = threading.RLock()
        self.Sending = False
        self.SendQueue = []

        if ConfigFilePath == None:
            self.ConfigFilePath = "/etc/"
        else:
            self.ConfigFilePath = ConfigFilePath

        # log errors in this module to a file
        if localinit == True:
            self.configfile = "mymodem.conf"
        else:
            self.configfile = self.ConfigFilePath + "mymodem.conf"

        # log errors in this module to a file
        if log == None:
            self.log = mylog.SetupLogger("mymodem",
                                         loglocation + "mymodem.log")
        else:
            self.log = log

        self.console = mylog.SetupLogger("mymodem_console",
                                         log_file="",
                                         stream=True)

        try:
            self.config = myconfig.MyConfig(filename=self.configfile,
                                            section="MyModem",
                                            log=self.log)

            if self.config.HasOption('log_at_commands'):
                self.LogAtCommands = self.config.ReadValue('log_at_commands',
                                                           return_type=bool)
            else:
                self.LogAtCommands = False

            if self.config.HasOption('message_level'):
                self.MessageLevel = self.config.ReadValue('message_level')
            else:
                self.MessageLevel = "error"

            if self.config.HasOption('rate'):
                self.Rate = self.config.ReadValue('rate', return_type=int)
            else:
                self.Rate = rate

            if self.config.HasOption('port'):
                self.Port = self.config.ReadValue('port')
            else:
                self.Port = port

            if self.config.HasOption('recipient'):
                self.Recipient = self.config.ReadValue('recipient')
            else:
                self.Recipient = recipient

            if self.config.HasOption('modem_type'):
                self.ModemType = self.config.ReadValue('modem_type')
            else:
                self.ModemType = modem_type

        except Exception as e1:
            self.LogErrorLine("Error reading config file: " + str(e1))
            self.LogConsole("Error reading config file: " + str(e1))
            return

        # rate * 10 bits then convert to MS
        self.CharacterTimeMS = (((1 / self.Rate) * 10) * 1000)

        self.InitComplete = False

        try:
            self.SerialDevice = myserial.SerialDevice(port,
                                                      rate=rate,
                                                      loglocation=loglocation,
                                                      log=self.log)
            self.Threads = self.MergeDicts(self.Threads,
                                           self.SerialDevice.Threads)

            self.Threads["SendMessageThread"] = mythread.MyThread(
                self.SendMessageThread, Name="SendMessageThread")

        except Exception as e1:
            self.LogErrorLine("Error opening serial device in MyModem: " +
                              str(e1))