Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 255 → Rev 256

/bioloid/cli/SerialBus.h
58,60 → 58,21
 
virtual bool ReadByte( uint8_t *ch );
 
//------------------------------------------------------------------------
// Reads a packet. Returns true if a packet was read successfully,
// false if a timeout or error occurred.
protected:
 
virtual bool ReadStatusPacket( BioloidPacket *pkt );
 
//------------------------------------------------------------------------
// Sends a byte. This will automatically accumulate the byte into
// the checksum
// Core portion of the WriteBuffer routine, which writes the data
// out the serial port.
 
virtual void SendByte( uint8_t data );
virtual void WriteBufferedData( void *data, size_t numBytes );
 
//------------------------------------------------------------------------
// Send the checksum. Since the checksum byte is the last byte of the
// packet, this function is made virtual to allow bus drivers to
// buffer the packet bytes until the entire packet is ready to send.
 
virtual void SendCheckSum();
 
//------------------------------------------------------------------------
// Sends the command header, which is common to all of the commands.
// 2 is added to paramLen (to cover the length and cmd bytes). This
// way the caller is only responsible for figuring out how many extra
// parameter bytes are being sent.
 
virtual void SendCmdHeader( Bioloid::ID_t id, uint8_t paramLen, Bioloid::Command cmd );
 
//------------------------------------------------------------------------
// Sets the debug mode
 
void SetDebug( bool debug ) { m_debug = debug; }
 
private:
 
//------------------------------------------------------------------------
// Adds a byte to the buffer of data to send.
 
void BufferByte( uint8_t data );
 
//------------------------------------------------------------------------
// Writes all of the buffered bytes to the serial port.
 
void WriteBuffer();
 
//------------------------------------------------------------------------
 
SerialPort *m_serialPort;
 
bool m_debug;
 
int m_fd;
size_t m_dataBytes;
uint8_t m_data[ 128 ];
 
};
 
/** @} */
/bioloid/cli/NetBus.cpp
24,7 → 24,6
// ---- Include Files -------------------------------------------------------
 
#include "Log.h"
#include "DumpMem.h"
#include "NetBus.h"
#include "Str.h"
#include "StrToken.h"
51,9 → 50,7
*/
 
NetBus::NetBus()
: m_debug( false ),
m_initialized( false ),
m_dataBytes( 0 )
: m_initialized( false )
{
}
 
70,17 → 67,18
 
//***************************************************************************
/**
* Adds a byte to the buffer of data to send.
* Reads a byte.
*
* virtual
*/
 
void NetBus::BufferByte( uint8_t data )
bool NetBus::ReadByte( uint8_t *ch )
{
m_data[ m_dataBytes++ ] = data;
 
if ( m_dataBytes >= sizeof( m_data ))
{
WriteBuffer();
}
#if 1
return true;
#else
return m_serialPort->Read( ch, 1 ) == 1;
#endif
}
 
//***************************************************************************
279,17 → 277,17
 
//***************************************************************************
/**
* Writes all of the buffered bytes to the serial port.
* Writes all of the buffered bytes to the network connection.
*
* virtual
*/
 
void NetBus::WriteBuffer()
void NetBus::WriteBufferedData( void *data, size_t numBytes )
{
// size_t bytesWritten;
 
if ( m_debug )
{
DumpMem( "W", 0, m_data, m_dataBytes );
}
(void)data;
(void)numBytes;
 
#if 0
if (( bytesWritten = m_serialPort->Write( m_data, m_dataBytes )) != m_dataBytes )
297,123 → 295,7
LogError( "Error writing %d bytes to serial port", m_dataBytes );
}
#endif
 
m_dataBytes = 0;
}
 
//***************************************************************************
/**
* Reads a byte.
*
* virtual
*/
 
bool NetBus::ReadByte( uint8_t *ch )
{
bool rc;
 
#if 1
rc = true;
#else
rc = m_serialPort->Read( ch, 1 ) == 1;
#endif
 
if ( rc )
{
if ( m_dataBytes < sizeof( m_data ))
{
m_data[ m_dataBytes++ ] = *ch;
}
}
 
return rc;
}
 
//***************************************************************************
/**
* Reads a packet. Returns true if a packet was read successfully,
* false if a timeout or error occurred.
*
* virtual
*/
 
bool NetBus::ReadStatusPacket( BioloidPacket *pkt )
{
bool rc;
 
m_dataBytes = 0;
 
rc = BioloidBus::ReadStatusPacket( pkt );
 
if ( m_debug )
{
if ( m_dataBytes > 0 )
{
DumpMem( "R", 0, m_data, m_dataBytes );
}
#if 0
if ( !rc )
{
LogError( "Packet Error\n" );
}
#endif
}
 
return rc;
}
 
//***************************************************************************
/**
* Sends a byte. This will automatically accumulate the byte into
* the checksum)
*
* virtual
*/
 
void NetBus::SendByte( uint8_t data )
{
m_checksum += data;
 
BufferByte( data );
}
 
//***************************************************************************
/**
* Send the checksum. Since the checksum byte is the last byte of the
* packet, this function is made virtual to allow bus drivers to
* buffer the packet bytes until the entire packet is ready to send.
*
* virtual
*/
 
void NetBus::SendCheckSum()
{
SendByte( ~m_checksum );
 
WriteBuffer();
}
 
//***************************************************************************
/**
* Sends the command header, which is common to all of the commands.
* 2 is added to paramLen (to cover the length and cmd bytes). This
* way the caller is only responsible for figuring out how many extra
* parameter bytes are being sent.
*
* virtual
*/
 
void NetBus::SendCmdHeader
(
Bioloid::ID_t id,
uint8_t paramLen,
Bioloid::Command cmd
)
{
m_dataBytes = 0;
 
BioloidBus::SendCmdHeader( id, paramLen, cmd );
}
 
/** @} */
 
/bioloid/cli/NetBus.h
64,54 → 64,16
 
virtual bool ReadByte( uint8_t *ch );
 
//------------------------------------------------------------------------
// Reads a packet. Returns true if a packet was read successfully,
// false if a timeout or error occurred.
 
virtual bool ReadStatusPacket( BioloidPacket *pkt );
 
//------------------------------------------------------------------------
// Sends a byte. This will automatically accumulate the byte into
// the checksum
 
virtual void SendByte( uint8_t data );
 
//------------------------------------------------------------------------
// Send the checksum. Since the checksum byte is the last byte of the
// packet, this function is made virtual to allow bus drivers to
// buffer the packet bytes until the entire packet is ready to send.
 
virtual void SendCheckSum();
 
//------------------------------------------------------------------------
// Sends the command header, which is common to all of the commands.
// 2 is added to paramLen (to cover the length and cmd bytes). This
// way the caller is only responsible for figuring out how many extra
// parameter bytes are being sent.
 
virtual void SendCmdHeader( Bioloid::ID_t id, uint8_t paramLen, Bioloid::Command cmd );
 
//------------------------------------------------------------------------
// Sets the debug mode
 
void SetDebug( bool debug ) { m_debug = debug; }
 
private:
 
//------------------------------------------------------------------------
// Adds a byte to the buffer of data to send.
// Core portion of the WriteBuffer routine, which writes the data
// out the serial port.
 
void BufferByte( uint8_t data );
virtual void WriteBufferedData( void *data, size_t numBytes );
 
//------------------------------------------------------------------------
// Writes all of the buffered bytes to the serial port.
 
void WriteBuffer();
 
//------------------------------------------------------------------------
 
bool m_debug;
 
bool m_initialized;
 
SOCKET m_socket; ///< Socket handle
/bioloid/cli/bioloid.cpp
372,7 → 372,7
{
exit( 1 );
}
gNetBus.SetDebug( gDebug != 0 );
gNetBus.SetShowPackets( gDebug != 0 );
cmdLine.SetBus( &gNetBus );
}
else
385,7 → 385,7
exit( 1 );
}
gSerialBus.SetSerialPort( &gSerialPort );
gSerialBus.SetDebug( gDebug != 0 );
gSerialBus.SetShowPackets( gDebug != 0 );
cmdLine.SetBus( &gSerialBus );
}
 
/bioloid/cli/SerialBus.cpp
25,7 → 25,6
// ---- Include Files -------------------------------------------------------
 
#include "Log.h"
#include "DumpMem.h"
#include "SerialBus.h"
#include "Error.h"
 
46,9 → 45,7
*/
 
SerialBus::SerialBus()
: m_serialPort( NULL ),
m_debug( false ),
m_dataBytes( 0 )
: m_serialPort( NULL )
{
}
 
65,43 → 62,6
 
//***************************************************************************
/**
* Adds a byte to the buffer of data to send.
*/
 
void SerialBus::BufferByte( uint8_t data )
{
m_data[ m_dataBytes++ ] = data;
 
if ( m_dataBytes >= sizeof( m_data ))
{
WriteBuffer();
}
}
 
//***************************************************************************
/**
* Writes all of the buffered bytes to the serial port.
*/
 
void SerialBus::WriteBuffer()
{
size_t bytesWritten;
 
if ( m_debug )
{
DumpMem( "W", 0, m_data, m_dataBytes );
}
 
if (( bytesWritten = m_serialPort->Write( m_data, m_dataBytes )) != m_dataBytes )
{
LogError( "Error writing %d bytes to serial port", m_dataBytes );
}
 
m_dataBytes = 0;
}
 
//***************************************************************************
/**
* Reads a byte.
*
* virtual
109,122 → 69,41
 
bool SerialBus::ReadByte( uint8_t *ch )
{
bool rc;
 
rc = m_serialPort->Read( ch, 1 ) == 1;
 
if ( rc )
{
if ( m_dataBytes < sizeof( m_data ))
{
m_data[ m_dataBytes++ ] = *ch;
}
}
 
return rc;
return m_serialPort->Read( ch, 1 ) == 1;
}
 
//***************************************************************************
/**
* Reads a packet. Returns true if a packet was read successfully,
* false if a timeout or error occurred.
*
* virtual
* Sets the serial port that will be used for talking with the bioloid
* devices.
*/
 
bool SerialBus::ReadStatusPacket( BioloidPacket *pkt )
void SerialBus::SetSerialPort( SerialPort *serPort )
{
bool rc;
m_serialPort = serPort;
 
m_dataBytes = 0;
// 15 gives intermittent failures on my Dell laptop. 50 seems
// to work reliably
 
rc = BioloidBus::ReadStatusPacket( pkt );
 
if ( m_debug )
{
if ( m_dataBytes > 0 )
{
DumpMem( "R", 0, m_data, m_dataBytes );
}
#if 0
if ( !rc )
{
LogError( "Packet Error\n" );
}
#endif
}
 
return rc;
m_serialPort->SetTimeout( 50 );
}
 
//***************************************************************************
/**
* Sends a byte. This will automatically accumulate the byte into
* the checksum)
* Writes all of the buffered bytes to the serial port.
*
* virtual
*/
 
void SerialBus::SendByte( uint8_t data )
void SerialBus::WriteBufferedData( void *data, size_t numBytes )
{
m_checksum += data;
size_t bytesWritten;
 
BufferByte( data );
if (( bytesWritten = m_serialPort->Write( data, numBytes )) != numBytes )
{
LogError( "Error writing %d bytes to serial port", numBytes );
}
}
 
//***************************************************************************
/**
* Send the checksum. Since the checksum byte is the last byte of the
* packet, this function is made virtual to allow bus drivers to
* buffer the packet bytes until the entire packet is ready to send.
*
* virtual
*/
 
void SerialBus::SendCheckSum()
{
SendByte( ~m_checksum );
 
WriteBuffer();
}
 
//***************************************************************************
/**
* Sends the command header, which is common to all of the commands.
* 2 is added to paramLen (to cover the length and cmd bytes). This
* way the caller is only responsible for figuring out how many extra
* parameter bytes are being sent.
*
* virtual
*/
 
void SerialBus::SendCmdHeader
(
Bioloid::ID_t id,
uint8_t paramLen,
Bioloid::Command cmd
)
{
m_dataBytes = 0;
 
BioloidBus::SendCmdHeader( id, paramLen, cmd );
}
 
//***************************************************************************
/**
* Sets the serial port that will be used for talking with the bioloid
* devices.
*/
 
void SerialBus::SetSerialPort( SerialPort *serPort )
{
m_serialPort = serPort;
 
// 15 gives intermittent failures on my Dell laptop. 50 seems
// to work reliably
 
m_serialPort->SetTimeout( 50 );
}
 
/** @} */