Serial Port Setup 
Author Message
 Serial Port Setup

I have the following code so far to setup communications
with the COMM1 port.  Does anyone know what header files
to include for this code? I'm using Microsoft Visual C++
6.0  I used this code from another source which said to
use #include <stdafx.h>, but it doesn't work - either does
<winbase.h>.  Also, If I have a character array with lets
say, three elements in it (the numbers 3, 4 and 5), what
is the function that would write the character array to
the COMM1 port?  Also, does the setup look okay as far as
parity, baud rate and stop bits ( I need 7 bits data,
19200 baud, 2 stop bits)???

        BOOL     m_bPortReady;
        HANDLE   m_hCom;
        CString  m_sComPort;
        DCB      m_dcb;
        COMMTIMEOUTS m_CommTimeouts;
        BOOL     bWriteRC;
        BOOL     bReadRC;
        DWORD iBytesWritten;
        DWORD iBytesRead;
        char       sBuffer[128];

        m_sComPort = "Com1";
        m_hCom = CreateFile(m_sComPort,
                GENERIC_READ | GENERIC_WRITE,
                0, // exclusive access
                NULL, // no security
                OPEN_EXISTING,
                0, // no overlapped I/O
                NULL); // null template

        m_bPortReady = SetupComm(m_hCom, 128, 128); // set
buffer sizes

        m_bPortReady = GetCommState(m_hCom, &m_dcb);
        m_dcb.BaudRate = 19200;
        m_dcb.ByteSize = 7;
        m_dcb.Parity = ODDPARITY;
        m_dcb.StopBits = TWOSTOPBITS;
        m_dcb.fAbortOnError = TRUE;

        m_bPortReady = SetCommState(m_hCom, &m_dcb);



Sun, 11 Sep 2005 00:32:57 GMT  
 Serial Port Setup
I included the header <windows.h> and that seems to work
okay with the code. I'm still not sure if I set up the
parity, baud and stop bits right. I'm still messing around
with the write to port function, but I was just wondering
if anyone had any specific suggestions or tips..

thanks..
-Lisa-

Quote:
>-----Original Message-----
>I have the following code so far to setup communications
>with the COMM1 port.  Does anyone know what header files
>to include for this code? I'm using Microsoft Visual C++
>6.0  I used this code from another source which said to
>use #include <stdafx.h>, but it doesn't work - either
does
><winbase.h>.  Also, If I have a character array with lets
>say, three elements in it (the numbers 3, 4 and 5), what
>is the function that would write the character array to
>the COMM1 port?  Also, does the setup look okay as far as
>parity, baud rate and stop bits ( I need 7 bits data,
>19200 baud, 2 stop bits)???

>    BOOL     m_bPortReady;
>    HANDLE   m_hCom;
>    CString  m_sComPort;
>    DCB      m_dcb;
>    COMMTIMEOUTS m_CommTimeouts;
>    BOOL     bWriteRC;
>    BOOL     bReadRC;
>    DWORD iBytesWritten;
>    DWORD iBytesRead;
>    char       sBuffer[128];

>    m_sComPort = "Com1";
>    m_hCom = CreateFile(m_sComPort,
>            GENERIC_READ | GENERIC_WRITE,
>            0, // exclusive access
>            NULL, // no security
>            OPEN_EXISTING,
>            0, // no overlapped I/O
>            NULL); // null template

>    m_bPortReady = SetupComm(m_hCom, 128, 128); // set
>buffer sizes

>    m_bPortReady = GetCommState(m_hCom, &m_dcb);
>    m_dcb.BaudRate = 19200;
>    m_dcb.ByteSize = 7;
>    m_dcb.Parity = ODDPARITY;
>    m_dcb.StopBits = TWOSTOPBITS;
>    m_dcb.fAbortOnError = TRUE;

>    m_bPortReady = SetCommState(m_hCom, &m_dcb);

>.



Sun, 11 Sep 2005 01:52:55 GMT  
 Serial Port Setup

Quote:

> I have the following code so far to setup communications
> with the COMM1 port.  Does anyone know what header files
> to include for this code? I'm using Microsoft Visual C++
> 6.0  I used this code from another source which said to
> use #include <stdafx.h>, but it doesn't work - either does
> <winbase.h>.  Also, If I have a character array with lets
> say, three elements in it (the numbers 3, 4 and 5), what
> is the function that would write the character array to
> the COMM1 port?  Also, does the setup look okay as far as
> parity, baud rate and stop bits ( I need 7 bits data,
> 19200 baud, 2 stop bits)???

>         BOOL     m_bPortReady;
>         HANDLE   m_hCom;
>         CString  m_sComPort;
>         DCB      m_dcb;
>         COMMTIMEOUTS m_CommTimeouts;
>         BOOL     bWriteRC;
>         BOOL     bReadRC;
>         DWORD iBytesWritten;
>         DWORD iBytesRead;
>         char       sBuffer[128];

>         m_sComPort = "Com1";
>         m_hCom = CreateFile(m_sComPort,
>                 GENERIC_READ | GENERIC_WRITE,
>                 0, // exclusive access
>                 NULL, // no security
>                 OPEN_EXISTING,
>                 0, // no overlapped I/O
>                 NULL); // null template

>         m_bPortReady = SetupComm(m_hCom, 128, 128); // set
> buffer sizes

>         m_bPortReady = GetCommState(m_hCom, &m_dcb);
>         m_dcb.BaudRate = 19200;
>         m_dcb.ByteSize = 7;
>         m_dcb.Parity = ODDPARITY;
>         m_dcb.StopBits = TWOSTOPBITS;
>         m_dcb.fAbortOnError = TRUE;

>         m_bPortReady = SetCommState(m_hCom, &m_dcb);

If you are working in a standard wizard-generated MFC app as your
starting point then check the stdafx.h file for a line something like
#define WIN32_LEAN_AND_MEAN and remove any such line.  That should make
the symbols compile without adding any #include's.  Your code looks OK.
But it will probably work better without the call to SetupComm (the
default buffer is roomier than 128).

To write an array (or any struct) to the COM port you pass its address
and number of bytes to send to WriteFile:

strcpy(sBuffer, "345");
WriteFile(m_hCom, sBuffer, strlen(sBuffer), &iBytesWritten, NULL);

--
Scott McPhillips [VC++ MVP]



Sun, 11 Sep 2005 05:13:04 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Serial port to serial port copying software

2. Challenge: Virtual serial ports from real serial port

3. using PC/104 serial expansion board with WinCE, 4 serial ports total

4. Parallel port/ Serial port

5. I want open the COM1 Port (serial Port)

6. Howto setup serial I/O ???

7. Serial port

8. Serial port access

9. Serial port drivers

10. Reading from a Serial Port with C# ???

11. open serial port with C

12. Serial port

 

 
Powered by phpBB® Forum Software