Skip to main content

cpp_porthandler

CPP PortHandler

  • Description

Base class for serial communication.

  • Members
MembersDescription
DEFAULT_BAUDRATE_:=1000000 Default Baudrate
is_using_Shows whether the port is in-use
  • Methods
MethodsDescription
getPortHandlerGets a child class (PortHandlerLinux or PortHandlerWindows) instance
~PortHandlerNone
openPortOpens a serial port
closePortCloses a serial port
clearPortRefreshes a serial port
setPortNameSets a device name
getPortNameGets a device name
setBaudrateSets a baudrate
getBaudrateGets a baudrate
getBytesAvailableChecks how many bytes can be read in port buffer
readPortReads bytes from port buffer
writePortWrites bytes to port buffer
setPacketTimeoutSets timeout
isPacketTimeoutChecks whether communication has been succeeded in timeout
  • Enumerator

    None

Method References

Windows

Linux

Mac

CPP PortHandlerWindows

  • Description

    Child class for serial communication on Windows OS.

  • Members

    None

  • Methods

MethodsDescription
PortHandlerWindowsInitializes PortHandlerWindows instance
~PortHandlerWindowsCloses a serial port
openPortOpens a serial port
closePortCloses a serial port
clearPortRefreshes a serial port
setPortNameSets a device name
getPortNameGets a device name
setBaudrateSets a baudrate
getBaudrateGets a baudrate
getBytesAvailableChecks how many bytes can be read in port buffer
readPortReads bytes from port buffer
writePortWrites bytes to port buffer
setPacketTimeoutSets timeout
isPacketTimeoutChecks whether communication has been succeeded in timeout
  • Enumerator

    None

Method References

openPort
  • Syntax
bool openPort()
  • Parameters

    None

  • Detailed Description

    This function opens the port by setBaudRate() function using DEFAULT_BAUDRATE_. If the baudrate is needed to be changed to another baudrate value, setBaudRate() function should be called after calling openPort() function. When the port succeeds to be opened, this function will return true, and if not, then false.

closePort
  • Syntax
void closePort()
  • Parameters

    None

  • Detailed Description

    This function closes the port by closing the file descriptor.

clearPort
  • Syntax
void clearPort()
  • Parameters

    None

  • Detailed Description

    This function clears the port by flushing the file descriptor.

setPortName
  • Syntax
void setPortName(const char* port_name)
  • Parameters
ParametersDescription
port_namePort name
  • Detailed Description

    This function sets the device name as port_name.

getPortName
  • Syntax
char* getPortName()
  • Parameters

    None

  • Detailed Description

    This function returns the device name which the port is using.

setBaudrate
  • Syntax
bool setBaudRate(const int baudrate)
  • Parameters
ParametersDescription
baudrateTarget baudrate
  • Description

    This function converts baudrate to baudrate type value at first. Secondly, it closes the port with closePort() function, and opens the port with setupPort() function again. If the value of baudrate is not in the baudrate list shown in the getCFlagBaud() function, the setCustomBaudrate() function suggests the baudrate value which is closest of available baudrate value. Finally, it returns false.

getBaudrate
  • Syntax
int getBaudRate()
  • Parameters

    None

  • Description

    This function returns the baudrate value previously set.

getBytesAvailable
  • Syntax
int getBytesAvailable()
  • Parameters

    None

  • Description

    This function checks how much the data can be read, and returns its length.

readPort
  • Syntax
int readPort(UINT8_T* packet, int length)
  • Parameters
ParametersDescription
packetThe number of data bytes read previously
lengthByte length for read
  • Description

    This function gets length byte data from port buffer and returns a number of read data bytes. On end-of-file, 0 is returned, on error it returns -1.

writePort
  • Syntax
int writePort(UINT8_T* packet, int length)
  • Parameters
ParametersDescription
packetThe number of data bytes to write
lengthByte length for write
  • Description

    This function transmits length byte, and returns how much the data was written. On error, it returns -1.

setPacketTimeout
  • Syntax
void setPacketTimeout (UINT16_T packet_length)
void setPacketTimeout (double msec)
  • Parameters
ParametersDescription
Packet_lengthTarget byte length for write
msecMiliseconds
  • Description

    This function sets the start time when it transmits the packet, and set the timeout of packet transmission to be ready for deciding communication result.

isPacketTimeout
  • Syntax
bool isPacketTimeout ()
  • Parameters

    None

  • Description

    This function decides the timeover of packet communication. If the time limit is over, it returns false.

CPP PortHandlerLinux

  • Description

    Child class for serial communication on Linux OS.

  • Members

    None

  • Methods

MethodsDescription
PortHandlerLinuxInitializes PortHandlerLinux instance
~PortHandlerLinuxCloses a serial port
openPortOpens a serial port
closePortCloses a serial port
clearPortRefreshes a serial port
setPortNameSets a device name
getPortNameGets a device name
setBaudrateSets a baudrate
getBaudrateGets a baudrate
getBytesAvailableChecks how many bytes can be read in port buffer
readPortReads bytes from port buffer
writePortWrites bytes to port buffer
setPacketTimeoutSets timeout
isPacketTimeoutChecks whether communication has been succeeded in timeout
  • Enumerator

    None

Method References

openPort
  • Syntax
bool openPort()
  • Parameters

    None

  • Detailed Description

    This function opens the port by setBaudRate() function using DEFAULT_BAUDRATE_. If the baudrate is needed to be changed to another baudrate value, setBaudRate() function should be called after calling openPort() function. When the port succeeds to be opened, this function will return true, and if not, then false.

closePort
  • Syntax
void closePort()
  • Parameters

    None

  • Detailed Description

    This function closes the port by closing the file descriptor.

clearPort
  • Syntax
void clearPort()
  • Parameters

    None

  • Detailed Description

    This function clears the port by flushing the file descriptor.

setPortName
  • Syntax
void setPortName(const char* port_name)
  • Parameters
ParametersDescription
port_namePort name
  • Detailed Description

    This function sets the device name as port_name.

getPortName
  • Syntax
char* getPortName()
  • Parameters

    None

  • Detailed Description

    This function returns the device name which the port is using.

setBaudrate
  • Syntax
bool setBaudRate(const int baudrate)
  • Parameters
ParametersDescription
baudrateTarget baudrate
  • Description

    This function converts baudrate to baudrate type value at first. Secondly, it closes the port with closePort() function, and opens the port with setupPort() function again. If the value of baudrate is not in the baudrate list shown in the getCFlagBaud() function, the setCustomBaudrate() function suggests the baudrate value which is closest of available baudrate value. Finally, it returns false.

getBaudrate
  • Syntax
int getBaudRate()
  • Parameters

    None

  • Description

    This function returns the baudrate value previously set.

getBytesAvailable
  • Syntax
int getBytesAvailable()
  • Parameters

    None

  • Description

    This function checks how much the data can be read, and returns its length.

readPort
  • Syntax
int readPort(UINT8_T* packet, int length)
  • Parameters
ParametersDescription
packetThe number of data bytes read previously
lengthByte length for read
  • Description

    This function gets length byte data from port buffer and returns a number of read data bytes. On end-of-file, 0 is returned, on error it returns -1.

writePort
  • Syntax
int writePort(UINT8_T* packet, int length)
  • Parameters
ParametersDescription
packetThe number of data bytes to write
lengthByte length for write
  • Description

    This function transmits length byte, and returns how much the data was written. On error, it returns -1.

setPacketTimeout
  • Syntax
void setPacketTimeout (UINT16_T packet_length)
void setPacketTimeout (double msec)
  • Parameters
ParametersDescription
Packet_lengthTarget byte length for write
msecMiliseconds
  • Description

    This function sets the start time when it transmits the packet, and set the timeout of packet transmission to be ready for deciding communication result.

isPacketTimeout
  • Syntax
bool isPacketTimeout ()
  • Parameters

    None

  • Description

    This function decides the timeover of packet communication. If the time limit is over, it returns false.

CPP PortHandlerMac

openPort
  • Syntax
bool openPort()
  • Parameters

    None

  • Detailed Description

    This function opens the port by setBaudRate() function using DEFAULT_BAUDRATE_. If the baudrate is needed to be changed to another baudrate value, setBaudRate() function should be called after calling openPort() function. When the port succeeds to be opened, this function will return true, and if not, then false.

closePort
  • Syntax
void closePort()
  • Parameters

    None

  • Detailed Description

    This function closes the port by closing the file descriptor.

clearPort
  • Syntax
void clearPort()
  • Parameters

    None

  • Detailed Description

    This function clears the port by flushing the file descriptor.

setPortName
  • Syntax
void setPortName(const char* port_name)
  • Parameters
ParametersDescription
port_namePort name
  • Detailed Description

    This function sets the device name as port_name.

getPortName
  • Syntax
char* getPortName()
  • Parameters

    None

  • Detailed Description

    This function returns the device name which the port is using.

setBaudrate
  • Syntax
bool setBaudRate(const int baudrate)
  • Parameters
ParametersDescription
baudrateTarget baudrate
  • Description

    This function converts baudrate to baudrate type value at first. Secondly, it closes the port with closePort() function, and opens the port with setupPort() function again. If the value of baudrate is not in the baudrate list shown in the getCFlagBaud() function, the setCustomBaudrate() function suggests the baudrate value which is closest of available baudrate value. Finally, it returns false.

getBaudrate
  • Syntax
int getBaudRate()
  • Parameters

    None

  • Description

    This function returns the baudrate value previously set.

getBytesAvailable
  • Syntax
int getBytesAvailable()
  • Parameters

    None

  • Description

    This function checks how much the data can be read, and returns its length.

readPort
  • Syntax
int readPort(UINT8_T* packet, int length)
  • Parameters
ParametersDescription
packetThe number of data bytes read previously
lengthByte length for read
  • Description

    This function gets length byte data from port buffer and returns a number of read data bytes. On end-of-file, 0 is returned, on error it returns -1.

writePort
  • Syntax
int writePort(UINT8_T* packet, int length)
  • Parameters
ParametersDescription
packetThe number of data bytes to write
lengthByte length for write
  • Description

    This function transmits length byte, and returns how much the data was written. On error, it returns -1.

setPacketTimeout
  • Syntax
void setPacketTimeout (UINT16_T packet_length)
void setPacketTimeout (double msec)
  • Parameters
ParametersDescription
Packet_lengthTarget byte length for write
msecMiliseconds
  • Description

    This function sets the start time when it transmits the packet, and set the timeout of packet transmission to be ready for deciding communication result.

isPacketTimeout
  • Syntax
bool isPacketTimeout ()
  • Parameters

    None

  • Description

    This function decides the timeover of packet communication. If the time limit is over, it returns false.