pub fn read_device_info(
        &mut self,
        obj_category: mei::DeviceInfoCategory,
    ) -> Result<Vec<mei::DeviceInfoObject>> {
        let mut info: Vec<mei::DeviceInfoObject> = vec![];
        let mut buff = vec![0; MODBUS_HEADER_SIZE]; // Header gets filled in later
        buff.write_u8(0x2B)?; // Modbus Encapsulated Interface (Function code 43)
        buff.write_u8(0x0E)?; // MEI Type 14 (Read Device Indentification)
        buff.write_u8(match obj_category {
            mei::DeviceInfoCategory::Basic => 0x01,
            mei::DeviceInfoCategory::Regular => 0x02,
            mei::DeviceInfoCategory::Extended => 0x03,
        })?;
        buff.write_u8(0x00)?; // Object ID

        let header = Header::new(self, buff.len() as u16 + 1u16);
        let head_buff = header.pack()?;
        {
            let mut start: Cursor<&mut Vec<u8>> = Cursor::new(buff.borrow_mut());
            start.write_all(&head_buff)?;
        }
        match self.stream.write_all(&buff) {
            Ok(_s) => {
                let reply = &mut [0; MODBUS_MAX_PACKET_SIZE];
                match self.stream.read(reply) {
                    Ok(_s) => {
                        let resp_hd = Header::unpack(reply)?;
                        Transport::validate_response_header(&header, &resp_hd)?;
                        Transport::validate_response_code(&buff, reply)?;

                        let resp_body = reply[7..(6 + resp_hd.len) as usize].to_vec();
                        let obj_count = resp_body[6] as usize;
                        let mut cursor: usize = 6;
                        for _ in 0..obj_count {
                            cursor += 1;
                            let id = resp_body[cursor];

                            cursor += 1;
                            let len = resp_body[cursor] as usize;

                            let mut val_buf: Vec<u8> = vec![];
                            for _ in 0..len {
                                cursor += 1;
                                val_buf.push(resp_body[cursor])
                            }

                            let object = mei::DeviceInfoObject::new(
                                id,
                                match String::from_utf8(val_buf) {
                                    Ok(val) => val,
                                    Err(_) => return Err(Error::ParseInfoError),
                                },
                            );
                            info.push(object)
                        }
                        Ok(())
                    }
                    Err(e) => Err(Error::Io(e)),
                }
            }
            Err(e) => Err(Error::Io(e)),
        }?;
        Ok(info)
    }
/**
    Some devices support modbus function 43 (Modbus Encasulated Interface) to read device information as strings.
    This will return an `IllegalFunction (0x01)` exception code if this request is not supported by the device.
    */
Some devices support modbus function 43 (Modbus Encasulated Interface) to read device information as strings.
This will return an `IllegalFunction (0x01)` exception code if this request is not supported by the device.
Some devices support modbus function 43 ( Modbus Encasulated Interface ) to read device information as strings . This will return an ` IllegalFunction ( 0x01 ) ` exception code if this request is not supported by the device .
pub fn read_device_info(
        &mut self,
        obj_category: mei::DeviceInfoCategory,
    ) -> Result<Vec<mei::DeviceInfoObject>> {
        let mut info: Vec<mei::DeviceInfoObject> = vec![];
        let mut buff = vec![0; MODBUS_HEADER_SIZE]; // Header gets filled in later
        buff.write_u8(0x2B)?; // Modbus Encapsulated Interface (Function code 43)
        buff.write_u8(0x0E)?; // MEI Type 14 (Read Device Indentification)
        buff.write_u8(match obj_category {
            mei::DeviceInfoCategory::Basic => 0x01,
            mei::DeviceInfoCategory::Regular => 0x02,
            mei::DeviceInfoCategory::Extended => 0x03,
        })?;
        buff.write_u8(0x00)?; // Object ID
        let header = Header::new(self, buff.len() as u16 + 1u16);
        let head_buff = header.pack()?;
        {
            let mut start: Cursor<&mut Vec<u8>> = Cursor::new(buff.borrow_mut());
            start.write_all(&head_buff)?;
        }
        match self.stream.write_all(&buff) {
            Ok(_s) => {
                let reply = &mut [0; MODBUS_MAX_PACKET_SIZE];
                match self.stream.read(reply) {
                    Ok(_s) => {
                        let resp_hd = Header::unpack(reply)?;
                        Transport::validate_response_header(&header, &resp_hd)?;
                        Transport::validate_response_code(&buff, reply)?;
                        let resp_body = reply[7..(6 + resp_hd.len) as usize].to_vec();
                        let obj_count = resp_body[6] as usize;
                        let mut cursor: usize = 6;
                        for _ in 0..obj_count {
                            cursor += 1;
                            let id = resp_body[cursor];
                            cursor += 1;
                            let len = resp_body[cursor] as usize;
                            let mut val_buf: Vec<u8> = vec![];
                            for _ in 0..len {
                                cursor += 1;
                                val_buf.push(resp_body[cursor])
                            }
                            let object = mei::DeviceInfoObject::new(
                                id,
                                match String::from_utf8(val_buf) {
                                    Ok(val) => val,
                                    Err(_) => return Err(Error::ParseInfoError),
                                },
                            );
                            info.push(object)
                        }
                        Ok(())
                    }
                    Err(e) => Err(Error::Io(e)),
                }
            }
            Err(e) => Err(Error::Io(e)),
        }?;
        Ok(info)
    }
pub fn read_device_info ( & mut self , obj_category : mei :: DeviceInfoCategory , ) -> Result < Vec < mei :: DeviceInfoObject > > { let mut info : Vec < mei :: DeviceInfoObject > = vec ! [ ] ; let mut buff = vec ! [ 0 MODBUS_HEADER_SIZE ] ; // Header gets filled in later buff . write_u8 ( 0x2B ) ? ; // Modbus Encapsulated Interface (Function code 43) buff . write_u8 ( 0x0E ) ? ; // MEI Type 14 (Read Device Indentification) buff . write_u8 ( match obj_category { mei :: DeviceInfoCategory :: Basic => 0x01 , mei :: DeviceInfoCategory :: Regular => 0x02 , mei :: DeviceInfoCategory :: Extended => 0x03 , } ) ? ; buff . write_u8 ( 0x00 ) ? ; // Object ID let header = Header :: new ( self , buff . len ( ) as u16 + 1u16 ) ; let head_buff = header . pack ( ) ? ; { let mut start : Cursor < & mut Vec < u8 > > = Cursor :: new ( buff . borrow_mut ( ) ) ; start . write_all ( & head_buff ) ? ; } match self . stream . write_all ( & buff ) { Ok ( _s ) => { let reply = & mut [ 0 ; MODBUS_MAX_PACKET_SIZE ] ; match self . stream . read ( reply ) { Ok ( _s ) => { let resp_hd = Header :: unpack ( reply ) ? ; Transport :: validate_response_header ( & header , & resp_hd ) ? ; Transport :: validate_response_code ( & buff , reply ) ? ; let resp_body = reply [ 7 .. ( 6 + resp_hd . len ) as usize ] . to_vec ( ) ; let obj_count = resp_body [ 6 ] as usize ; let mut cursor : usize = 6 ; for _ in 0 .. obj_count { cursor += 1 ; let id = resp_body [ cursor ] ; cursor += 1 ; let len = resp_body [ cursor ] as usize ; let mut val_buf : Vec < u8 > = vec ! [ ] ; for _ in 0 .. len { cursor += 1 ; val_buf . push ( resp_body [ cursor ] ) } let object = mei :: DeviceInfoObject :: new ( id , match String :: from_utf8 ( val_buf ) { Ok ( val ) => val , Err ( _ ) => return Err ( Error :: ParseInfoError ) , } , ) ; info . push ( object ) } Ok ( ( ) ) } Err ( e ) => Err ( Error :: Io ( e ) ) , } } Err ( e ) => Err ( Error :: Io ( e ) ) , } ? ; Ok ( info ) }
Some devices support modbus function 43 (Modbus Encasulated Interface) to read device information as strings.
Some devices support modbus function 43 ( Modbus Encasulated Interface ) to read device information as strings .
&mut self
obj_category
mei::DeviceInfoCategory
&mut self
obj_category
mei::DeviceInfoCategory