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


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

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


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

示例1: SendData

void SendData( const void *buf, size_t bytesToWrite )
{
    if ( gDebug )
    {
        DumpMem( "Send", 0, buf, bytesToWrite );
    }
    gSerialPort.Write( buf, bytesToWrite );

} // SendData
开发者ID:JonHylands,项目名称:projects,代码行数:9,代码来源:BootHost.cpp

示例2: SendByte

void SendByte( unsigned char ch )
{
    if ( gDebug )
    {
        Log( "Sending: 0x%02x\n", ch );
    }
    gSerialPort.Write( &ch, 1 );

} // SendByte
开发者ID:JonHylands,项目名称:projects,代码行数:9,代码来源:BootHost.cpp

示例3: main

int main(int argc, char *argv[])
{
	const char default_path[] = "/dev/ttyUSB2";
	const char *path;
	if(argc < 2)
		path = default_path;
	else
		path = argv[1];
	char buf[1024];
	SerialPort sp;
	sp.Init();
	sp.Open(path, 9600, 8, 1, 'n');
	while(1)
	{
		cout<<"Please key in data"<<endl;
		cin>>buf;
		sp.Write(buf, strlen(buf)+1);
	}
	return 0;
}
开发者ID:iElaine,项目名称:mcc_old,代码行数:20,代码来源:writesp.cpp

示例4: 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

示例5: 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

示例6: bootLoader


//.........这里部分代码省略.........
                }

                case 'd':
                {
                    LogDebug( "Download\n" );

                    if ( gDownloadFileName == NULL )
                    {
                        LogError( "No filename specified to download\n" );
                        break;
                    }

                    I2C_BootLoader bootLoader( &gSerialDongle, gSlaveAddr );

                    if ( !bootLoader.DownloadFile( gDownloadInfo ))
                    {
                        LogError( "Unable to download file\n" );
                    }
                    break;
                }

                case 'i':
                {
                    LogDebug( "GetBootInfo\n" );

                    I2C_BootLoader bootLoader( &gSerialDongle, gSlaveAddr );

                    BootLoaderInfo_t    bootInfo;

                    if ( bootLoader.GetBootLoaderInfo( &bootInfo ))
                    {
                        bootLoader.PrintBootLoaderInfo( bootInfo );
                    }
                    else
                    {
                        LogError( "Unable to retrieve bootloader info\n" );
                    }
                    break;
                }

                case 'r':
                {
                    LogDebug( "Read\n" );

                    I2C_Adapter::Buffer readBuf;
                    char                readMem[ 40 ];

                    readBuf.data = (unsigned char *)readMem;
                    readBuf.dataLen = sizeof( readMem );

                    if ( gSerialDongle.Read( gSlaveAddr, BL_CMD_TEST_READ, &readBuf ))
                    {
                        printf( "Read: '%s' from 0x%02x\n", &readMem[ 1 ], readMem[ 0 ] );
                    }
                    else
                    {
                        printf( "Read: failed\n" );
                    }
                    break;
                }

                case 'w':
                {
                    LogDebug( "Write\n" );

                    I2C_Adapter::Buffer writeBuf;
                    char                writeMem[ 40 ];

                    writeMem[ 0 ] = gSerialDongleAddr;
                    sprintf( &writeMem[ 1 ], "-w-%d-w-", gCounter++ );

                    writeBuf.data = (unsigned char *)writeMem;
                    writeBuf.dataLen = strlen( writeMem ) + 2;  // +1 for i2c addr, +1 for temrinating null

                    printf( "Write: Wrote: '%s' to 0x%02x\n", &writeMem[ 1 ], writeMem[ 0 ] );

                    if ( !gSerialDongle.Write( gSlaveAddr, BL_CMD_TEST_WRITE, &writeBuf ))
                    {
                        printf( "Write: failed\n" );
                    }
                    break;
                }

                default:
                {
                    printf( "Rcvd: 0x%02x\n", ch );

                    //gSerialPort.Write( &ch, 1 );
                    break;
                }
            }
        }
        else
#endif
        {
            gSerialPort.Write( &ch, 1 );
        }
    }

} // ReadStdinThread
开发者ID:JonHylands,项目名称:projects,代码行数:101,代码来源:BootHost.cpp


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