ajeet
ajeet

Reputation: 21

Receive bytes from serial port

Hi i am using vc++ 6.0 MSCOMM for serial communication i have to read data (4 bytes) and display from serial port.

ex: data format is:
>88 84 10 02   (4 bytes data from port)<br>
>C6 1E 10 02   (Received Data) but i have to display 88 84 but i am getting C6 and 1E.

my code is:

VARIANT data;
BSTR k;
static char dbuff[4];
int dcount=0;
CString data;

if(m_mscom.GetCommEvent()==2) // Receiving data from port
{
    data=m_mscom.GetInput();
    k=data.bstrVal;
    dbuff[dcount] = char (k[0]);
    dcount++;

    if(dcount == 4)
    {
        dcount=0;
        data.Format ("%02X%02X%02X%02X", (unsigned char)dbuff[0], 
               (unsigned char)dbuff[1], (unsigned char)dbuff[2],
               (unsigned char)dbuff[3]);
    }
}

please help what is the wrong in my code, thanks in advance,please give me one example how to read bytes from port.

Upvotes: 2

Views: 1184

Answers (2)

Tim
Tim

Reputation: 20360

If I were you I would do the following:

  • Move up to a new compiler - VS 2008 or 2010
  • Use a real interface - not COM/MSCOMM

Try looking at:

WriteFile()
ReadFile()
CreateFile()

Regardless of the VS version - drop the MSCOMM garbage and use file handles.

Or, if you insist on the COM object, try searching online for C++ and mscomm - like this result: http://www.edaboard.com/thread19993.html

Upvotes: 1

Leon
Leon

Reputation: 1141

Check that you are using the correct number of stop bits as you may be getting a skew. Check the parity bit for transmission errors.

I recently used a quick C# project to read from serial port. Can post code if you want to switch to C#

Upvotes: 0

Related Questions