Mega Code Archive

 
Categories / Delphi / Examples
 

Data-serialport

>I have to make a program for a machine that >prints directly to the printer. >The machine will be connected to the computer >instead of the printer. And the computer has to >use the information to put things into a database. >My question, does anybody has an idea how I >have to recieve the information from the port? Or >is there a component on the internet for this? There are a lot of components for serial I/O. Look on Torry's Delphi site (http://www.torry.ru/) for some freeware. There are also commercial packages available. But you don't realy need a component :-) You may use win32 API to use serial port. You open serial port like a file. FComName := 'COM1'; FComHandle := CreateFile(PChar(FComName), GENERIC_READ+GENERIC_WRITE, 0, // Not shared nil, // Security OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); // No template Then you can get current (default) settings: GetCommState(FComHandle, FDCB); GetCommProperties(FComHandle, FCommProp); GetCommTimeouts(FComHandle, FCommTimeouts); Then you may change settings according to what you need, using SetCommXXXX functions. You'll find baudrate, parity, handshaking and so on in FDCB structure. Then you can read/write to the serial port using ReadFile and WriteFile. ReadFile and WriteFile are blocking. You may wants to use a threads. Use a reader thread blocked on ReadFile and a writer thread blocked on WriteFile. When on is unblocked, then signal the main thread, maybe using PostMessage. You may also need to use WaitCommEvent to handle all kind of communications events. When you are done, use CloseHandle to close the serial port. Delphi has on-line help (win32.hlp) for all functions I mentionned. Datatypes and structures are defined in windows.pas Hope that helps... -- francois.piette@pophost.eunet.be The author of the freeware Internet Component Suite and freeware middleware MidWare http://www.rtfm.be/fpiette/indexuk.htm **************************************************** If you don't want to see any more of these messages, send a message to: MajorDomo@Kyler.com with a message of: UNSUBSCRIBE delphi