Delphi Serial Port Tutorial

admin
Delphi Serial Port Tutorial 4,0/5 6360reviews

RS-232 is one of the most widely used communications port and can be used to send data from one device to another. It is common that computer systems, and interface devices have serial ports. The standard RS-232 ports on a PC are COM1:, COM2:, COM3: and COM4. These ports are normally configured by the operating system from Control Panel Modems (or from BIOS). Delphi can interface to the RS-232 ports using a Win32 API call. This call is named CreateFile, which allow the serial port to be treated as a normal file. The format of CreateFile is: HANDLE CreateFile(LPCTSTC Name, DWORD Access, DWORD ShareMode, LPSECURITY_ATTRIBUTES Attr, DWORD Create, DWORD AttrAndFlags, HANDLE Template); Where: - Name.

This is the name of the file. This is the access mode. Valid types are GENERIC_READ and GENERIC_WRITE. This is the shared mode. 0 - Prevents sharing, FILE_SHARE_READ - read-only, FILE_SHARE_WRITE - any operations. This is the create mode. Descargar Manual De Instalacion De Windows Xp En Pdf. CREATE_NEW - create new file, CREATE_ALWAYS - always create, OPEN_EXISTING - open existing file, OPEN_ALWAYS and TRUNCATE_EXISTING.

- AttrAndFlags. The attributes include: FILE_ATTRIBUTE_ARCHIVE (archive attribute), FILE_ATTRIBUTE_NORMAL (normal file attribute), FILE_ATTRIBUTE_HIDDEN (hid-den file attribute), FILE_ATTRIBUTE_READONLY (read only file attribute, FILE_ATTRIBUTE_SYSTEM (system file attribute). This defines a template. The communication port can be defined by its name (such as COM1, COM2, and so on). Sample code which creates the file handle for the serial port (COM3) is.

Defining a communications port const CommPort = 'COM3'; var ComFile: THandle; NumberWritten: cardinal; begin ComFile:= CreateFile(PChar(CommPort), GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0); The ReadFile and WriteFile routines can then be used to read and write data to and from the serial port, respectively. The format of WriteFile is: BOOL WriteFile(HANDLE File, LPCVOID Buffer, DWORD NumberOfBytesToWrite, LPDWORD NumberOfBytesWritten, LPOVERLAPPED Overlap); Where: ' File.

Pointer to data to be written. ' NumberOfBytesToWrite. Bytes to write. ' NumberOfByesWritten.

Delphi Serial Port Tutorial

Jul 18, 2009. Delphi 7 XP prof. Hi - I'm using the free Tcomport component for Delphi. Although I do not usually have problems, there is one bugbear that I cant seem to solve. If you have a couple of USB serial.

Returns the number of bytes written. Defines an overlapping structure. The overall return from the function is a True or a False (which determines the success or failure of the write). For example, after the file has been created, the following will send the string 'HELLO' to the COM3 serial port. Writing to a port Str:='Hello'; rtn:=WriteFile(ComFile, PChar(Str), Length(Str), NumberWritten, nil) Data can be read from the serial port using the ReadFile routine.

Its format is: BOOL ReadFile(HANDLE File, LPVOID Buffer, DWORD NumberOfBytesToRead, LPDWORD NumberOfBytesRead, LPOVERLAPPED Overlap); Where: - File. Pointer to data to be read into. - NumberOfBytesToRead. Bytes to read. Drivers Tacho Card. - NumberOfByesRead. Returns the number of bytes read.

Defines an overlapping structure.