Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Mar : Using Win API to access multi commports in a thread

www.cryer.info
Managed Newsgroup Archive

Using Win API to access multi commports in a thread

Subject:Using Win API to access multi commports in a thread
Posted by:"Daniel" (danie..@esands.com)
Date:18 Mar 2007 16:35:37 -0800

Hi,
I am new in Delphi.
I wrote a Windows application with plain C and native API a few years ago,one function of the application is that it reads data
string from four serial commports, and it is defined in a thread. I want to know how to do the same thing in Delphi, mainly it covers two issues, create a thread and call Windows API.

For my cass, how can I translater following C code to Delphi
(1) Create handle to commport, like
    HANDLE hCom1 = CreateFile ("COM1", ENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,NULL)

(2) Create a thread
    HANDLE hReadThread = CreateThread (NULL,  0, (LPTHREAD_START_ROUTINE)ReadThreadProc, NULL, 0, &id)

(3) Create thread function, it can reads commport,
    DWORD  ReadThreadProc (LPDWORD lpdwParam1)
    {
       ...
       while (bReading)
       {
        // read the bytes
       if (ReadFile(hCom1, inbuff, nToRead, &nBytesRead, NULL))
          if (nBytesRead)
             ProcessSensorReadBytes(inbuff, nBytesRead);
      }
      .....        
    }    

Thanks in advance

Daniel

Info

Glossary

Replies:

www.cryer.info
Managed Newsgroup Archive