pub fn write_instruction<F>(
		&mut self,
		packet_id: u8,
		instruction_id: u8,
		parameter_count: usize,
		encode_parameters: F,
	) -> Result<(), WriteError>
	where
		F: FnOnce(&mut [u8]),
	{
		// Throw away old data in the read buffer.
		// Ideally, we would also flush the kernel buffer, but the serial crate doesn't expose that.
		// We don't do this when reading a reply, because we might multiple replies for one instruction,
		// and read() can potentially read more than one reply per syscall.
		self.read_len = 0;

		let buffer = self.write_buffer.as_mut();
		if buffer.len() < HEADER_SIZE + parameter_count + 2 {
			// TODO: return proper error.
			panic!("write buffer not large enough for outgoing mesage");
		}

		// Add the header, with a placeholder for the length field.
		buffer[4] = packet_id;
		buffer[5] = 0;
		buffer[6] = 0;
		buffer[7] = instruction_id;
		encode_parameters(&mut buffer[HEADER_SIZE..][..parameter_count]);

		// Perform bitstuffing on the body.
		// The header never needs stuffing.
		// TODO: properly propagate error.
		let stuffed_body_len = bytestuff::stuff_inplace(&mut buffer[HEADER_SIZE..], parameter_count).unwrap();

		write_u16_le(&mut buffer[5..], stuffed_body_len as u16 + 3);

		// Add checksum.
		let checksum_index = HEADER_SIZE + stuffed_body_len;
		let checksum = calculate_checksum(0, &buffer[..checksum_index]);
		write_u16_le(&mut buffer[checksum_index..], checksum);

		// Send message.
		let stuffed_message = &buffer[..checksum_index + 2];
		trace!("sending instruction: {:02X?}", stuffed_message);
		self.serial_port.discard_input_buffer()
			.map_err(WriteError::DiscardBuffer)?;
		self.serial_port.write_all(stuffed_message)
			.map_err(WriteError::Write)?;
		Ok(())
	}
/// Write an instruction message to the bus.
Write an instruction message to the bus.
Write an instruction message to the bus .
pub fn write_instruction<F>(
		&mut self,
		packet_id: u8,
		instruction_id: u8,
		parameter_count: usize,
		encode_parameters: F,
	) -> Result<(), WriteError>
	where
		F: FnOnce(&mut [u8]),
	{
		// Throw away old data in the read buffer.
		// Ideally, we would also flush the kernel buffer, but the serial crate doesn't expose that.
		// We don't do this when reading a reply, because we might multiple replies for one instruction,
		// and read() can potentially read more than one reply per syscall.
		self.read_len = 0;
		let buffer = self.write_buffer.as_mut();
		if buffer.len() < HEADER_SIZE + parameter_count + 2 {
			// TODO: return proper error.
			panic!("write buffer not large enough for outgoing mesage");
		}
		// Add the header, with a placeholder for the length field.
		buffer[4] = packet_id;
		buffer[5] = 0;
		buffer[6] = 0;
		buffer[7] = instruction_id;
		encode_parameters(&mut buffer[HEADER_SIZE..][..parameter_count]);
		// Perform bitstuffing on the body.
		// The header never needs stuffing.
		// TODO: properly propagate error.
		let stuffed_body_len = bytestuff::stuff_inplace(&mut buffer[HEADER_SIZE..], parameter_count).unwrap();
		write_u16_le(&mut buffer[5..], stuffed_body_len as u16 + 3);
		// Add checksum.
		let checksum_index = HEADER_SIZE + stuffed_body_len;
		let checksum = calculate_checksum(0, &buffer[..checksum_index]);
		write_u16_le(&mut buffer[checksum_index..], checksum);
		// Send message.
		let stuffed_message = &buffer[..checksum_index + 2];
		trace!("sending instruction: {:02X?}", stuffed_message);
		self.serial_port.discard_input_buffer()
			.map_err(WriteError::DiscardBuffer)?;
		self.serial_port.write_all(stuffed_message)
			.map_err(WriteError::Write)?;
		Ok(())
	}
pub fn write_instruction < F > ( & mut self , packet_id : u8 , instruction_id : u8 , parameter_count : usize , encode_parameters : F , ) -> Result < ( ) , WriteError > where F : FnOnce ( & mut [ u8 ] ) , { // Throw away old data in the read buffer. // Ideally, we would also flush the kernel buffer, but the serial crate doesn't expose that. // We don't do this when reading a reply, because we might multiple replies for one instruction, // and read() can potentially read more than one reply per syscall. self . read_len = 0 ; let buffer = self . write_buffer . as_mut ( ) ; if buffer . len ( ) < HEADER_SIZE + parameter_count + 2 { // TODO: return proper error. panic ! ( " " ) ; } // Add the header, with a placeholder for the length field. buffer [ 4 ] = packet_id ; buffer [ 5 ] = 0 ; buffer [ 6 ] = 0 ; buffer [ 7 ] = instruction_id ; encode_parameters ( & mut buffer [ HEADER_SIZE .. ] [ .. parameter_count ] ) ; // Perform bitstuffing on the body. // The header never needs stuffing. // TODO: properly propagate error. let stuffed_body_len = bytestuff :: stuff_inplace ( & mut buffer [ HEADER_SIZE .. ] , parameter_count ) . unwrap ( ) ; write_u16_le ( & mut buffer [ 5 .. ] , stuffed_body_len as u16 + 3 ) ; // Add checksum. let checksum_index = HEADER_SIZE + stuffed_body_len ; let checksum = calculate_checksum ( 0 , & buffer [ .. checksum_index ] ) ; write_u16_le ( & mut buffer [ checksum_index .. ] , checksum ) ; // Send message. let stuffed_message = & buffer [ .. checksum_index + 2 ] ; trace ! ( " " stuffed_message ) ; self . serial_port . discard_input_buffer ( ) . map_err ( WriteError :: DiscardBuffer ) ? ; self . serial_port . write_all ( stuffed_message ) . map_err ( WriteError :: Write ) ? ; Ok ( ( ) ) }
Write an instruction message to the bus.
Write an instruction message to the bus .
&mut self
packet_id
u8
instruction_id
u8
parameter_count
usize
encode_parameters
F
&mut self
packet_id
u8
instruction_id
u8
parameter_count
usize
encode_parameters
F