Matlab PortHandler
-
Description
PortHandler dependent functions for serial communication.
-
Members
None
-
Methods
Methods | Description |
---|---|
portHandle | Initializes parameters for serial port control |
openPort | Opens a serial port |
closePort | Closes a serial port |
clearPort | Refreshes a serial port |
setPortName | Sets a device name |
getPortName | Gets a device name |
setBaudrate | Sets a baudrate |
getBaudrate | Gets a baudrate |
getBytesAvailable | Checks how many bytes can be read in port buffer |
readPort | Reads bytes from port buffer |
writePort | Writes bytes to port buffer |
setPacketTimeout | Sets timeout |
isPacketTimeout | Checks whether communication has been succeeded in timeout |
-
Enumerator
None
Method References
portHandler
- Syntax
int portHandler(string port_name)
- Parameters
Parameters | Description |
---|---|
port_name | Port 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, resizeportData
struct and start to initialize struct members.
openPort
- Syntax
bool openPort(int port_num)
- Parameters
Parameters | Description |
---|---|
port_num | Port 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 callingopenPort
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
Parameters | Description |
---|---|
port_num | Port number |
-
Detailed Description
This function closes the port by closing the file descriptor.
clearPort
- Syntax
void clearPort(int port_num)
- Parameters
Parameters | Description |
---|---|
port_num | Port number |
-
Detailed Description
This function clears the port by flushing the file descriptor.
setPortName
- Syntax
void setPortName(int port_num, string port_name)
- Parameters
Parameters | Description |
---|---|
port_num | Port number |
port_name | Port name |
-
Detailed Description
This function sets the device name as port_name.
getPortName
- Syntax
string getPortName(int port_num)
- Parameters
Parameters | Description |
---|---|
port_num | Port number |
-
Detailed Description
This function returns the device name which the port is using.
setBaudrate
- Syntax
bool setBaudrate(int port_num, int baudrate)
- Parameters
Parameters | Description |
---|---|
port_num | Port number |
baudrate | Target baudrate |
-
Description
This function converts
baudrate
to baudrate type value at first. Secondly, it closes the port withclosePort
function, and opens the port withsetupPort
function again. If the value ofbaudrate
is not in the compatible baudrate list, thesetCustomBaudrate
function suggests the baudrate value which is closest of available baudrate value. Finally, it returns false.
getBaudrate
- Syntax
int getBaudrate(int port_num)
- Parameters
Parameters | Description |
---|---|
port_num | Port number |
-
Description
This function returns the baudrate value previously set.
getBytesAvailable
- Syntax
int getBytesAvailable(int port_num)
- Parameters
Parameters | Description |
---|---|
port_num | Port 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
Parameters | Description |
---|---|
port_num | Port number |
packet | The number of data bytes read previously |
length | Byte 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
Parameters | Description |
---|---|
port_num | Port number |
packet | The number of data bytes to write |
length | Byte 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
Parameters | Description |
---|---|
port_num | Port number |
Packet_length | Target 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
Parameters | Description |
---|---|
port_num | Port number |
msec | Miliseconds |
-
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
Parameters | Description |
---|---|
port_num | Port number |
-
Description
This function decides the timeover of packet communication. If the time limit is over, it returns false.