def serial_get(self): """ Create the serial connection from the framework settings and return it, setting the framework instance in the process. """ frmwk_c1218_settings = { 'nbrpkts': self.advanced_options['NBRPKTS'], 'pktsize': self.advanced_options['PKTSIZE'] } frmwk_serial_settings = get_default_serial_settings() frmwk_serial_settings['baudrate'] = self.advanced_options['BAUDRATE'] frmwk_serial_settings['bytesize'] = self.advanced_options['BYTESIZE'] frmwk_serial_settings['stopbits'] = self.advanced_options['STOPBITS'] self.logger.info('opening serial device: ' + self.options['CONNECTION']) try: self.serial_connection = Connection( self.options['CONNECTION'], c1218_settings=frmwk_c1218_settings, serial_settings=frmwk_serial_settings, enable_cache=self.advanced_options['CACHETBLS']) except Exception as error: self.logger.error('could not open the serial device') raise error return self.serial_connection
def serial_connect(self): """ Connect to the serial device and then verifies that the meter is responding. Once the serial device is opened, this function attempts to retreive the contents of table #0 (GEN_CONFIG_TBL) to configure the endianess it will use. Returns True on success. """ username = self.options['USERNAME'] userid = self.options['USERID'] if len(username) > 10: self.logger.error('username cannot be longer than 10 characters') raise FrameworkConfigurationError('username cannot be longer than 10 characters') if not (0 <= userid <= 0xffff): self.logger.error('user id must be between 0 and 0xffff') raise FrameworkConfigurationError('user id must be between 0 and 0xffff') frmwk_c1218_settings = { 'nbrpkts': self.advanced_options['NBRPKTS'], 'pktsize': self.advanced_options['PKTSIZE'] } frmwk_serial_settings = GetDefaultSerialSettings() frmwk_serial_settings['baudrate'] = self.advanced_options['BAUDRATE'] frmwk_serial_settings['bytesize'] = self.advanced_options['BYTESIZE'] frmwk_serial_settings['stopbits'] = self.advanced_options['STOPBITS'] self.logger.info('opening serial device: ' + self.options['CONNECTION']) try: self.serial_connection = Connection(self.options['CONNECTION'], c1218_settings = frmwk_c1218_settings, serial_settings = frmwk_serial_settings, enable_cache = self.advanced_options['CACHETBLS']) except Exception as error: self.logger.error('could not open the serial device') raise error try: self.serial_connection.start() if not self.serial_connection.login(username, userid): self.logger.error('the meter has rejected the username and userid') raise FrameworkConfigurationError('the meter has rejected the username and userid') except C1218IOError as error: self.logger.error('serial connection has been opened but the meter is unresponsive') raise error try: general_config_table = self.serial_connection.get_table_data(0) except C1218ReadTableError as error: self.logger.error('serial connection as been opened but the general configuration table (table #0) could not be read') raise error if (ord(general_config_table[0]) & 1): self.logger.info('setting the connection to use big-endian for C1219 data') self.serial_connection.c1219_endian = '>' else: self.logger.info('setting the connection to use little-endian for C1219 data') self.serial_connection.c1219_endian = '<' try: self.serial_connection.stop() except C1218IOError as error: self.logger.error('serial connection has been opened but the meter is unresponsive') raise error self.__serial_connected__ = True self.logger.warning('the serial interface has been connected') return True