This repository contains the code to generate a dll file used to communicate with remote devices through UART.
NOTE: currently this code can run only on Windows OS.
This is the list of functions provided by the DLL:
- CreateComm
- SendCommand
- ReadCommand
- SRCommand
- ModbusCalcCRC
- CloseComm
- GetAvailableCom
Functions and parameters are described below.
This function opens a serial port and returns a handler for the port. If something goes wrong, the function returns an error (for example whether the port is already open by another application).
char *nCom // COM1, COM2, etc.
int baud // Baud rate (list of available baud rates below)
char *conf // Configuration for the communication (description below)
List of Available baud rates:
- 110
- 300
- 600
- 1200
- 2400
- 4800
- 9600
- 14400
- 19200
- 38400
- 56000
- 57600
- 115200
- 128000
- 256000
- 500000
- 1000000
Configuration for the communication:
- N_BITS (5,6,7,8)
- PARITY (n, e, o)
- STOP_BITS (1, 2)
Example: 8n1
This function sends a message to the connected device and returns the number of bytes sent.
HWND hPort // Handler of the serial port (returned by CreateComm function)
unsigned char *send // The message to send
unsigned int lenSend // The length of the message to send
This function reads the buffer and returns the number of byte received.
HWND hPort // Handler of the serial port (returned by CreateComm function)
unsigned char *recv // The pointer of the buffer where to save the response
unsigned int lenFrame // The maximum number of bytes to receive
This function combines the functions described above (SendCommand and ReceiveCommand).
HWND hPort // Handler of the serial port (returned by CreateComm function)
unsigned char *send // The message to send
unsigned int lenS // The length of the message to send
unsigned char *recv // The pointer of the buffer where to save the response
unsigned int lenR // The maximum number of bytes to receive
unsigned int delay // The delay in milliseconds between sending and receiving
This function calculate the CRC of a buffer using CRC-16/MODBUS algorithm. It returns a 16 bit value, two bytes that must be swapped.
unsigned char *Frame // The pointer of the buffer
int lenFrame // The length of the message
This function close the connection with the serial port.
HWND hPort // Handler of the serial port (returned by CreateComm function)
This function gets the list of available serial ports, checking the ports between COM1 and COM100. It returns the number of available ports.
char *ncom // The buffer where to save the list of the available serial ports.