Serial library for Windows.
- MIT Licence:
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
- The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.
Vars
udtCommOverlap: OVERLAPPED
udtCommError: COMM_ERROR
udtPorts: array[MAX_PORTS, COMM_PORT]
Consts
LINE_BREAK = 1
LINE_DTR = 2
LINE_RTS = 3
LINE_CTS = 0x00000000
LINE_DSR = 0x00000000
LINE_RING = 0x00000000
LINE_RLSD = 0x00000000
LINE_CD = 0x00000000
ERROR_IO_INCOMPLETE = 996
ERROR_IO_PENDING = 997
GENERIC_READ = 0x0000000000000000'i64
GENERIC_WRITE = 0x00000000
FILE_ATTRIBUTE_NORMAL = 0x00000000
FILE_FLAG_OVERLAPPED = 0x00000000
FORMAT_MESSAGE_FROM_SYSTEM = 0x00000000
OPEN_EXISTING = 3
MS_CTS_ON = 0x00000000
MS_DSR_ON = 0x00000000
MS_RING_ON = 0x00000000
MS_RLSD_ON = 0x00000000
PURGE_RXABORT = 0x00000000
PURGE_RXCLEAR = 0x00000000
PURGE_TXABORT = 0x00000000
PURGE_TXCLEAR = 0x00000000
CLRBREAK = 9
CLRDTR = 6
CLRRTS = 4
SETBREAK = 8
SETDTR = 5
SETRTS = 3
Procs
proc BuildCommDCB(lpDef: LPCSTR; lpDCB: LPDCB): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc: "BuildCommDCBA".}
- Fills a specified DCB structure with values specified in a device-control string.
proc ClearCommError(hFile: HANDLE; lpErrors: LPDWORD; lpStat: LPCOMSTAT): WINBOOL {...}{. stdcall, dynlib: "kernel32", importc.}
- Retrieves information about a communications error and reports the current status of a communications device. The function is called when a communications error occurs, and it clears the device's error flag to enable additional input and output (I/O) operations.
proc CloseHandle(hObject: HANDLE): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc.}
- Closes an open communications device or file handle.
proc CreateFile(lpFileName: LPCSTR; dwDesiredAccess: DWORD; dwShareMode: DWORD; lpSecurityAttributes: LPSECURITY_ATTRIBUTES; dwCreationDisposition: DWORD; dwFlagsAndAttributes: DWORD; hTemplateFile: HANDLE): HANDLE {...}{.stdcall, dynlib: "kernel32", importc: "CreateFileA".}
- Creates or opens a communications resource and returns a handle that can be used to access the resource.
proc EscapeCommFunction(hFile: HANDLE; dwFunc: DWORD): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc.}
- Directs a specified communications device to perform a function.
proc FormatMessage(dwFlags: DWORD; lpSource: LPCVOID; dwMessageId: DWORD; dwLanguageId: DWORD; lpBuffer: LPSTR; nSize: DWORD; Arguments: ptr va_list): DWORD {...}{.stdcall, dynlib: "kernel32", importc: "FormatMessageA".}
- Formats a message string such as an error string returned by anoher function.
proc GetCommModemStatus(hFile: HANDLE; lpModemStat: LPDWORD): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc.}
- Retrieves modem control-register values.
proc GetCommState(hFile: HANDLE; lpDCB: LPDCB): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc.}
- Retrieves the current control settings for a specified communications device.
proc GetLastError(): DWORD {...}{.stdcall, dynlib: "kernel32", importc.}
- Retrieves the calling thread's last-error code value.
proc GetOverlappedResult(hFile: HANDLE; lpOverlapped: LPOVERLAPPED; lpNumberOfBytesTransferred: LPDWORD; bWait: WINBOOL): WINBOOL {...}{. stdcall, dynlib: "kernel32", importc.}
- Retrieves the results of an overlapped operation on the specified file, named pipe, or communications device.
proc PurgeComm(hFile: HANDLE; dwFlags: DWORD): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc.}
- Discards all characters from the output or input buffer of a specified communications resource. It can also terminate pending read or write operations on the resource.
proc ReadFile(hFile: HANDLE; lpBuffer: LPVOID; nNumberOfBytesToRead: DWORD; lpNumberOfBytesRead: LPDWORD; lpOverlapped: LPOVERLAPPED): WINBOOL {...}{. stdcall, dynlib: "kernel32", importc.}
- Reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read, unless the file handle is created with the overlapped attribute. If the file handle is created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the read operation.
proc SetCommState(hFile: HANDLE; lpDCB: LPDCB): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc.}
- Configures a communications device according to the specifications in a device-control block (a DCB structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues.
proc SetCommTimeouts(hFile: HANDLE; lpCommTimeouts: LPCOMMTIMEOUTS): WINBOOL {...}{. stdcall, dynlib: "kernel32", importc.}
- Sets the time-out parameters for all read and write operations on a specified communications device.
proc SetupComm(hFile: HANDLE; dwInQueue: DWORD; dwOutQueue: DWORD): WINBOOL {...}{.stdcall, dynlib: "kernel32", importc.}
- Initializes the communications parameters for a specified communications device.
proc WriteFile(hFile: HANDLE; lpBuffer: LPCVOID; nNumberOfBytesToWrite: DWORD; lpNumberOfBytesWritten: LPDWORD; lpOverlapped: LPOVERLAPPED): WINBOOL {...}{. stdcall, dynlib: "kernel32", importc.}
- Writes data to a file and is designed for both synchronous and a synchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished.
proc GetSystemMessage(lngErrorCode: DWORD): string {...}{.raises: [], tags: [].}
- Gets system error text for the specified error code.
proc SetCommError(strFunction: string): DWORD {...}{.discardable, raises: [], tags: [].}
proc SetCommErrorEx(strFunction: string; lngHnd: HANDLE): DWORD {...}{.discardable, raises: [], tags: [].}
proc CommOpen(intPortID: WORD; strPort: string; strSettings: string): DWORD {...}{. discardable, raises: [], tags: [].}
-
Opens/Initializes serial port.
- Parameters:
- intPortID - Port ID used when port was opened.
- strPort - COM port name. (COM1, COM2, COM3, COM4)
- strSettings - Communication settings. Example: "baud=9600 parity=N data=8 stop=1"
- Returns:
- Error Code - 0 = No Error.
proc CommSet(intPortID: WORD; strSettings: string): DWORD {...}{.discardable, raises: [], tags: [].}
-
Modifies the serial port settings.
- Parameters:
- intPortID - Port ID used when port was opened.
- strSettings - Communication settings.
- Example: "baud=9600 parity=N data=8 stop=1"
- Returns:
- Error Code - 0 = No Error.
proc CommClose(intPortID: WORD): DWORD {...}{.discardable, raises: [], tags: [].}
-
Close the serial port.
- Parameters:
- intPortID - Port ID used when port was opened.
- Returns:
- Error Code - 0 = No Error.
proc CommFlush(intPortID: WORD): DWORD {...}{.discardable, raises: [], tags: [].}
-
Flush the send and receive serial port buffers.
- Parameters:
- intPortID - Port ID used when port was opened.
- Returns:
- Error Code - 0 = No Error.
proc CommRead(intPortID: WORD; strData: var string; lngSize: DWORD): DWORD {...}{.discardable, raises: [], tags: [].}
-
Read serial port input buffer.
- Parameters:
- intPortID - Port ID used when port was opened.
- strData - Data buffer.
- lngSize - Maximum number of bytes to be read.
- Returns:
- Error Code - 0 = No Error.
proc CommWrite(intPortID: WORD; strData: string): DWORD {...}{.discardable, raises: [], tags: [TimeEffect].}
-
Output data to the serial port.
- Parameters:
- intPortID - Port ID used when port was opened.
- strData - Data to be transmitted.
- Returns:
- Error Code - 0 = No Error.
proc CommGetLine(intPortID: WORD; intLine: WORD; blnState: var bool): DWORD {...}{. discardable, raises: [], tags: [].}
-
Get the state of selected serial port control lines.
- Parameters:
- intPortID - Port ID used when port was opened.
- intLine - Serial port line. CTS, DSR, RING, RLSD (CD)
- blnState - Returns state of line (Cleared or Set).
- Returns:
- Error Code - 0 = No Error.
proc CommSetLine(intPortID: WORD; intLine: WORD; blnState: bool): DWORD {...}{.discardable, raises: [], tags: [].}
-
Set the state of selected serial port control lines.
- Parameters:
- intPortID - Port ID used when port was opened.
- intLine - Serial port line. BREAK, DTR, RTS
- Note: BREAK actually sets or clears a "break" condition on the transmit data line.
- blnState - Sets the state of line (Cleared or Set).
- Returns:
- Error Code - 0 = No Error.
proc SendComm(intPortID: WORD; strData: string): DWORD {...}{.discardable, raises: [], tags: [TimeEffect].}
- convinience proc to send data to specified COM port