Skip to main content

API Reference

C

C PortHandler

  • Description : Base functions for serial communication.

  • Members

MembersDescription
DEFAULT_BAUDRATE:=1000000 Default Baudrate
g_used_port_numShows how much ports are in-use
g_is_usingShows whether the port is in-use
  • Methods
MethodsDescription
portHandlerInitializes members of port data pointer struct
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 (Linux only)
readPortReads bytes from port buffer
writePortWrites bytes to port buffer
setPacketTimeoutSets timeout
setPacketTimeoutMSecSets timeout using micro second value
isPacketTimeoutChecks whether communication has been succeeded in timeout
  • Enumerator : None

Method References

C PortHandlerWindows

  • Description

    PortHandler dependent functions for serial communication on Windows OS.

  • Members

    None

  • Methods

MethodsDescription
portHandlerWindowsInitializes parameters for serial port control
openPortWindowsOpens a serial port
closePortWindowsCloses a serial port
clearPortWindowsRefreshes a serial port
setPortNameWindowsSets a device name
getPortNameWindowsGets a device name
setBaudrateWindowsSets a baudrate
getBaudrateWindowsGets a baudrate
readPortWindowsReads bytes from port buffer
writePortWindowsWrites bytes to port buffer
setPacketTimeoutWindowsSets timeout
setPacketTimeoutMSecWindowsSets timeout using micro second
isPacketTimeoutWindowsChecks whether communication has been succeeded in timeout
getCurrentTimeWindowsGets a current time
getTimeSinceStartWindowsGets a time since start
setupPortWindowsSets a port
  • Enumerator

    None

Method References

portHandlerWindows
  • Syntax
int portHandlerWindows(const char *port_name)
  • Parameters
ParametersDescription
port_namePort name
  • Detailed Description

    The function initializes the parameters for port control. At first, this checks if the port with same device name is set already in the portDataWindows pointer struct. If it exists, the function returns the port number as what it has. If not, the function finds any free port, resize portDataWindows struct and start to initialize struct members.

openPortWindows
  • Syntax
uint8_t openPortWindows(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function opens the port by SetBaudRateWindows function using DEFAULT_BAUDRATE. If the baudrate is needed to be changed to another baudrate value, SetBaudRateWindows function should be called again after calling openPortWindows function. When the port succeeds to be opened, this function will return true, and if not, then false.

closePortWindows
  • Syntax
void closePortWindows(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function sets serial_handle_ member of portDataWindows struct to INVALID_HANDLE_VALUE so that the #port_num port won't be activated.

clearPortWindows
  • Syntax
void clearPortWindows(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function clears the port by purging the RX buffer.

setPortNameWindows
  • Syntax
void setPortNameWindows(int port_num, const char *port_name)
  • Parameters
ParametersDescription
port_numPort number
port_namePort name
  • Detailed Description

    This function sets the device name of the #port_num port as port_name.

getPortNameWindows
  • Syntax
char *getPortNameWindows(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

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

setBaudrateWindows
  • Syntax
uint8_t setBaudrateWindows(int port_num, const int baudrate)
  • Parameters
ParametersDescription
port_numPort number
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. Finally, it returns port setup result.

getBaudrateWindows
  • Syntax
int getBaudrateWindows(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

    This function returns the baudrate value previously set.

readPortWindows
  • Syntax
int readPortWindows(int port_num, uint8_t *packet, int length)
  • Parameters
ParametersDescription
port_numPort number
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.

writePortWindows
  • Syntax
int writePortWindows(int port_num, uint8_t *packet, int length)
  • Parameters
ParametersDescription
port_numPort number
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.

setPacketTimeoutWindows
  • Syntax
void setPacketTimeoutWindows (int port_num, uint16_t packet_length)
  • Parameters
ParametersDescription
port_numPort number
packet_lengthTarget byte length for write
  • 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.

SetPacketTimeoutMSecWindows
  • Syntax
void SetPacketTimeoutMSecWindows (int port_num, double msec)
  • Parameters
ParametersDescription
port_numPort number
msecMiliseconds
  • Description

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

isPacketTimeoutWindows
  • Syntax
uint8_t isPacketTimeoutWindows (int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

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

getCurrentTimeWindows
  • Syntax
double getCurrentTimeWindows(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

    This function gets a current time.

getTimeSinceStartWindows
  • Syntax
double getTimeSinceStartWindows(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

    This function gets a time since start.

setupPortWindows
  • Syntax
uint8_t setupPortWindows (int port_num, const int baudrate)
  • Parameters
ParametersDescription
port_numPort number
baudrateTarget baudrate
  • Description

    This function sets the port.

C PortHandlerLinux

  • Description

    PortHandler dependent functions for serial communication on Linux OS.

  • Members

    None

  • Methods

MethodsDescription
portHandleLinuxInitializes parameters for serial port control
openPortLinuxOpens a serial port
closePortLinuxCloses a serial port
clearPortLinuxRefreshes a serial port
setPortNameLinuxSets a device name
getPortNameLinuxGets a device name
setBaudrateLinuxSets a baudrate
getBaudrateLinuxGets a baudrate
getBytesAvailableLinuxChecks how many bytes can be read in port buffer
readPortLinuxReads bytes from port buffer
writePortLinuxWrites bytes to port buffer
setPacketTimeoutLinuxSets timeout
setPacketTimeoutMSecLinuxSets timeout using micro second
isPacketTimeoutLinuxChecks whether communication has been succeeded in timeout
getCurrentTimeLinuxGets a current time
getTimeSinceStartLinuxGets a time since start
setupPortLinuxSets a port
setCustomBaudrateLinuxSets a custom baudrate
getCFlagBaudGets a baudrate
  • Enumerator

    None

Method References

portHandlerLinux
  • Syntax
int portHandlerLinux(const char *port_name)
  • Parameters
ParametersDescription
port_namePort name
  • Detailed Description

    The function initializes the parameters for port control. At first, this checks if the port with same device name is set already in the portDataLinux pointer struct. If it exists, the function returns the port number as what it has. If not, the function finds any free port, resize portDataLinux struct and start to initialize struct members.

openPortLinux
  • Syntax
uint8_t openPortLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function opens the port by SetBaudRateLinux function using DEFAULT_BAUDRATE. If the baudrate is needed to be changed to another baudrate value, SetBaudRateLinux function should be called again after calling OpenPortLinux function. When the port succeeds to be opened, this function will return true, and if not, then false.

closePortLinux
  • Syntax
void closePortLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function closes the port by closing the file descriptor.

clearPortLinux
  • Syntax
void clearPortLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function clears the port by flushing the file descriptor.

setPortNameLinux
  • Syntax
void setPortNameLinux(int port_num, const char *port_name)
  • Parameters
ParametersDescription
port_numPort number
port_namePort name
  • Detailed Description

    This function sets the device name as port_name.

getPortNameLinux
  • Syntax
char *getPortNameLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

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

setBaudrateLinux
  • Syntax
uint8_t setBaudrateLinux(int port_num, const int baudrate)
  • Parameters
ParametersDescription
port_numPort number
baudrateTarget baudrate
  • Description

    This function converts baudrate to baudrate type value at first. Secondly, it closes the port with ClosePortLinux function, and opens the port with SetupPortLinux function again. If the value of baudrate is not in the compatible baudrate list, the SetCustomBaudrateLinux function suggests the baudrate value which is closest of available baudrate value. Finally, it returns false.

getBaudrateLinux
  • Syntax
int getBaudrateLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

    This function returns the baudrate value previously set.

getBytesAvailableLinux
  • Syntax
int getBytesAvailableLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

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

readPortLinux
  • Syntax
int readPortLinux(int port_num, uint8_t *packet, int length)
  • Parameters
ParametersDescription
port_numPort number
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.

writePortLinux
  • Syntax
int writePortLinux(int port_num, uint8_t *packet, int length)
  • Parameters
ParametersDescription
port_numPort number
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.

setPacketTimeoutLinux
  • Syntax
void setPacketTimeoutLinux (int port_num, uint16_t packet_length)
  • Parameters
ParametersDescription
port_numPort number
Packet_lengthTarget byte length for write
  • 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.

setPacketTimeoutMSecLinux
  • Syntax
void setPacketTimeoutMSecLinux (int port_num, double msec)
  • Parameters
ParametersDescription
port_numPort number
msecMiliseconds
  • Description

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

isPacketTimeoutLinux
  • Syntax
uint8_t isPacketTimeoutLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

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

getCurrentTimeLinux
  • Syntax
double getCurrentTimeLinux()
  • Parameters

    None

  • Description

    This function gets a current time.

getTimeSinceStartLinux
  • Syntax
double getTimeSinceStartLinux(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

    This function gets a time since start.

setupPortLinux
  • Syntax
uint8_t setupPortLinux(int port_num, int cflag_baud)
  • Parameters
ParametersDescription
port_numPort number
cflag_baudTarget baudrate
  • Description

    This function sets the port.

setCustomBaudrateLinux
  • Syntax
uint8_t setCustomBaudrateLinux(int port_num, int speed)
  • Parameters
ParametersDescription
port_numPort number
speedcommunication speed
  • Description

    This function sets a custom baudrate.

getCFlagBaud
  • Syntax
int getCFlagBaud(int baudrate)
  • Parameters
ParametersDescription
baudrateTarget baudrate
  • Description

    This function gets the baudrate.

C PortHandlerMac

  • Description

    PortHandler dependent functions for serial communication on Mac OS.

  • Members

    None

  • Methods

MethodsDescription
portHandleMacInitializes parameters for serial port control
openPortMacOpens a serial port
closePortMacCloses a serial port
clearPortMacRefreshes a serial port
setPortNameMacSets a device name
getPortNameMacGets a device name
setBaudrateMacSets a baudrate
getBaudrateMacGets a baudrate
getBytesAvailableMacChecks how many bytes can be read in port buffer
readPortMacReads bytes from port buffer
writePortMacWrites bytes to port buffer
setPacketTimeoutMacSets timeout
setPacketTimeoutMSecMacSets timeout using micro
isPacketTimeoutMacChecks whether communication has been succeeded in timeout
getCurrentTimeMacGets a current time
getTimeSinceStartMacGets a time since start
setupPortMacSets a port
setCustomBaudrateMacSets a custom baudrate
getCFlagBaudGets a baudrate
  • Enumerator

    None

Method References

portHandlerMac
  • Syntax
int portHandlerMac(const char *port_name)
  • Parameters
ParametersDescription
port_namePort name
  • Detailed Description

    The function initializes the parameters for port control. At first, this checks if the port with same device name is set already in the portDataMac pointer struct. If it exists, the function returns the port number as what it has. If not, the function finds any free port, resize portDataMac struct and start to initialize struct members.

openPortMac
  • Syntax
uint8_t openPortMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function opens the port by SetBaudRateMac function using DEFAULT_BAUDRATE. If the baudrate is needed to be changed to another baudrate value, SetBaudRateMac function should be called again after calling OpenPortMac function. When the port succeeds to be opened, this function will return true, and if not, then false.

closePortMac
  • Syntax
void closePortMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function closes the port by closing the file descriptor.

clearPortMac
  • Syntax
void clearPortMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function clears the port by flushing the file descriptor.

setPortNameMac
  • Syntax
void setPortNameMac(int port_num, const char *port_name)
  • Parameters
ParametersDescription
port_numPort number
port_namePort name
  • Detailed Description

    This function sets the device name as port_name.

getPortNameMac
  • Syntax
char *getPortNameMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

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

setBaudrateMac
  • Syntax
uint8_t setBaudrateMac(int port_num, const int baudrate)
  • Parameters
ParametersDescription
port_numPort number
baudrateTarget baudrate
  • Description

    This function converts baudrate to baudrate type value at first. Secondly, it closes the port with ClosePortMac function, and opens the port with SetupPortMac function again. If the value of baudrate is not in the compatible baudrate list, the SetCustomBaudrateMac function suggests the baudrate value which is closest of available baudrate value. Finally, it returns false.

getBaudrateMac
  • Syntax
int getBaudrateMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

    This function returns the baudrate value previously set.

getBytesAvailableMac
  • Syntax
int getBytesAvailableMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

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

readPortMac
  • Syntax
int readPortMac(int port_num, uint8_t *packet, int length)
  • Parameters
ParametersDescription
port_numPort number
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.

writePortMac
  • Syntax
int writePortMac(int port_num, uint8_t *packet, int length)
  • Parameters
ParametersDescription
port_numPort number
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.

setPacketTimeoutMac
  • Syntax
void setPacketTimeoutMac (int port_num, uint16_t packet_length)
  • Parameters
ParametersDescription
port_numPort number
Packet_lengthTarget byte length for write
  • 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.

setPacketTimeoutMSecMac
  • Syntax
void setPacketTimeoutMSecMac (int port_num, double msec)
  • Parameters
ParametersDescription
port_numPort number
msecMiliseconds
  • Description

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

isPacketTimeoutMac
  • Syntax
uint8_t isPacketTimeoutMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

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

getCurrentTimeMac
  • Syntax
double getCurrentTimeMac()
  • Parameters

    None

  • Description

    This function gets a current time.

getTimeSinceStartMac
  • Syntax
double getTimeSinceStartMac(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Description

    This function gets a time since start.

setupPortMac
  • Syntax
uint8_t setupPortMac(int port_num, int cflag_baud)
  • Parameters
ParametersDescription
port_numPort number
cflag_baudTarget baudrate
  • Description

    This function sets the port.

setCustomBaudrateMac
  • Syntax
uint8_t setCustomBaudrateMac(int port_num, int speed)
  • Parameters
ParametersDescription
port_numPort number
speedcommunication speed
  • Description

    This function sets a custom baudrate.

getCFlagBaud
  • Syntax
int getCFlagBaud(int baudrate)
  • Parameters
ParametersDescription
baudrateTarget baudrate
  • Description

    This function gets the baudrate.