Skip to main content

matlab_porthandler

  • Description

    PortHandler dependent functions for serial communication.

  • Members

    None

  • Methods

MethodsDescription
portHandleInitializes parameters for serial port control
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

portHandler
  • Syntax
int portHandler(string 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 portData pointer struct. If it exists, the function returns the port number as what it has. If not, the function finds any free port, resize portData struct and start to initialize struct members.

openPort
  • Syntax
bool openPort(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function opens the port by setBaudRate function using default baudrate(1M). If the baudrate is needed to be changed to another baudrate value, setBaudRate function should be called again 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(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

    This function closes the port by closing the file descriptor.

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

    This function clears the port by flushing the file descriptor.

setPortName
  • Syntax
void setPortName(int port_num, string port_name)
  • Parameters
ParametersDescription
port_numPort number
port_namePort name
  • Detailed Description

    This function sets the device name as port_name.

getPortName
  • Syntax
string getPortName(int port_num)
  • Parameters
ParametersDescription
port_numPort number
  • Detailed Description

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

setBaudrate
  • Syntax
bool setBaudrate(int port_num, 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. If the value of baudrate is not in the compatible baudrate list, the setCustomBaudrate function suggests the baudrate value which is closest of available baudrate value. Finally, it returns false.

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

    This function returns the baudrate value previously set.

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

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

readPort
  • Syntax
int readPort(int port_num, int[] 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.

writePort
  • Syntax
int writePort(int port_num, int[] 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.

setPacketTimeout
  • Syntax
void setPacketTimeout (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.

setPacketTimeoutMSec
  • Syntax
void setPacketTimeoutMSec (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.

isPacketTimeout
  • Syntax
bool isPacketTimeout(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.