当前位置: 首页>>代码示例>>C++>>正文


C++ SerialPort::Flush方法代码示例

本文整理汇总了C++中SerialPort::Flush方法的典型用法代码示例。如果您正苦于以下问题:C++ SerialPort::Flush方法的具体用法?C++ SerialPort::Flush怎么用?C++ SerialPort::Flush使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SerialPort的用法示例。


在下文中一共展示了SerialPort::Flush方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: imuTask

static void imuTask(IMU *imu)
{
        char stream_type;
        uint16_t gyro_fsr_dps, accel_fsr, update_rate_hz;
        uint16_t q1_offset, q2_offset, q3_offset, q4_offset;
        float yaw_offset_degrees;
        uint16_t flags;
    bool stream_response_received = false;
    double last_valid_packet_time = 0.0;
    int partial_binary_packet_count = 0;
    int stream_response_receive_count = 0;
    int stream_response_timeout_count = 0;
    int timeout_count = 0;
    int discarded_bytes_count = 0;
    int port_reset_count = 0;
    double last_stream_command_sent_timestamp = 0.0;
    int updates_in_last_second = 0;
    double last_second_start_time = 0;

        SerialPort *pport = imu->GetSerialPort();
    try {
                pport->SetReadBufferSize(512);
                pport->SetTimeout(1.0);
                pport->EnableTermination('\n');
                pport->Flush();
                pport->Reset();
    } catch(std::exception ex) {
    }

    int cmd_packet_length = IMUProtocol::encodeStreamCommand( protocol_buffer,
                                                        imu->current_stream_type, imu->update_rate_hz );

    try {
        pport->Reset();
        pport->Write( protocol_buffer, cmd_packet_length );
        pport->Flush();
#ifdef DEBUG_IMU_RX
        port_reset_count++;
        SmartDashboard::PutNumber("nav6_PortResets", (double)port_reset_count);
#endif
        last_stream_command_sent_timestamp = Timer::GetFPGATimestamp();
    } catch (std::exception ex) {
    }

    while (!stop) {
        try {

            // Wait, with delays to conserve CPU resources, until
            // bytes have arrived.

            while ( !stop && ( pport->GetBytesReceived() < 1 ) ) {
                delayMillis(1000/update_rate_hz);
            }

            int packets_received = 0;
                uint32_t bytes_read = pport->Read( protocol_buffer, 256 );
            if (bytes_read > 0) {
                byte_count += bytes_read;
                uint32_t i = 0;
                // Scan the buffer looking for valid packets
                while (i < bytes_read) {

                    // Attempt to decode a packet

                    int bytes_remaining = bytes_read - i;

                        if ( protocol_buffer[i] != PACKET_START_CHAR ) {
                                /* Skip over received bytes until a packet start is detected. */
                                i++;
#ifdef DEBUG_IMU_RX
                                discarded_bytes_count++;
                                SmartDashboard::PutNumber("nav6 Discarded Bytes", (double)discarded_bytes_count);
#endif
                                continue;
                        } else {
                                if ( ( bytes_remaining > 2 ) &&
                                         ( protocol_buffer[i+1] == '#' ) ) {
                                        /* Binary packet received; next byte is packet length-2 */
                                        uint8_t total_expected_binary_data_bytes = protocol_buffer[i+2];
                                        total_expected_binary_data_bytes += 2;
                                        while ( bytes_remaining < total_expected_binary_data_bytes ) {
                                                /* This binary packet contains an embedded     */
                                                /* end-of-line character.  Continue to receive */
                                                /* more data until entire packet is received.  */
                                                uint32_t additional_received_data_length = pport->Read(additional_received_data, 256);
                                                if ( additional_received_data_length > 0 ) {
                                                                        byte_count += additional_received_data_length;
                                                                        memcpy(protocol_buffer + bytes_read, additional_received_data, additional_received_data_length);
                                                                        bytes_read += additional_received_data_length;
                                                                        bytes_remaining += additional_received_data_length;
                                                } else {
                                                        /* Timeout waiting for remainder of binary packet */
                                                        i++;
                                                        bytes_remaining--;
#ifdef DEBUG_IMU_RX
                                                partial_binary_packet_count++;
                                                SmartDashboard::PutNumber("nav6 Partial Binary Packets", (double)partial_binary_packet_count);
#endif
                                                continue;
                                                }
//.........这里部分代码省略.........
开发者ID:Chantilly612Code,项目名称:612-2015,代码行数:101,代码来源:IMU.cpp

示例2: imuTask

static void imuTask(IMU *imu) 
{
    bool stream_response_received = false;
    double last_stream_command_sent_timestamp = 0.0;
	stop = false;
	SerialPort *pport = imu->GetSerialPort();
	pport->SetReadBufferSize(512);
	pport->SetTimeout(1.0);
	pport->EnableTermination('\n');
	pport->Flush();
	pport->Reset();

	char stream_type;
	uint16_t gyro_fsr_dps, accel_fsr, update_rate_hz;
	uint16_t q1_offset, q2_offset, q3_offset, q4_offset;
	float yaw_offset_degrees;
	uint16_t flags;
	
    // Give the nav6 circuit a few seconds to initialize, then send the stream configuration command.
    int cmd_packet_length = IMUProtocol::encodeStreamCommand( protocol_buffer, imu->current_stream_type, imu->update_rate_hz ); 
    pport->Reset();
    pport->Write( protocol_buffer, cmd_packet_length );
    pport->Flush();
    last_stream_command_sent_timestamp = Timer::GetFPGATimestamp();
		
	while (!stop)
	{ 
		uint32_t bytes_read = pport->Read( protocol_buffer, 256 );
		if ( bytes_read > 0 )
		{
			int packets_received = 0;
			byte_count += bytes_read;
			uint32_t i = 0;
			// Scan the buffer looking for valid packets
			while ( i < bytes_read )
			{
				int bytes_remaining = bytes_read - i;
				int packet_length = imu->DecodePacketHandler( &protocol_buffer[i], bytes_remaining ); 
				if ( packet_length > 0 )
				{
					packets_received++;
					update_count++;
					i += packet_length;
				}
				else 
				{
					packet_length = IMUProtocol::decodeStreamResponse( &protocol_buffer[i], bytes_remaining, stream_type,
							  gyro_fsr_dps, accel_fsr, update_rate_hz,
							  yaw_offset_degrees, 
							  q1_offset, q2_offset, q3_offset, q4_offset,
							  flags );
					if ( packet_length > 0 ) 
					{
						packets_received++;
						imu->SetStreamResponse( stream_type, 
								  gyro_fsr_dps, accel_fsr, update_rate_hz,
								  yaw_offset_degrees, 
								  q1_offset, q2_offset, q3_offset, q4_offset,
								  flags );
						stream_response_received = true;
						i += packet_length;
					}
					else // current index is not the start of a valid packet we're interested in; increment
					{
						i++;
					}
				}
			}
			if ( ( packets_received == 0 ) && ( bytes_read == 256 ) ) {
				// No packets received and 256 bytes received; this
				// condition occurs in the SerialPort.  In this case,
				// reset the serial port.
				pport->Reset();
			}
			
			if ( !stream_response_received && ((Timer::GetFPGATimestamp() - last_stream_command_sent_timestamp ) > 3.0 ) ) {
				cmd_packet_length = IMUProtocol::encodeStreamCommand( protocol_buffer, imu->current_stream_type, imu->update_rate_hz ); 
				last_stream_command_sent_timestamp = Timer::GetFPGATimestamp();
				pport->Write( protocol_buffer, cmd_packet_length );
				pport->Flush();
			}	            				
		}
		else {
			// This exception typically indicates a Timeout
			stream_response_received = false;
		}
	}
}
开发者ID:CRRobotics,项目名称:Robots,代码行数:88,代码来源:IMU.cpp


注:本文中的SerialPort::Flush方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。