Search code examples
c++visual-c++mfcserial-communication

what are the functions i need use for serial communication in vc++?


I am working on an embeeded device.i connect to it using COM port. It gives the list of all files when i send a command "LIST" to it.

so i wrote an "hello world" which will connect to the port device is connected and will send data.

When i connect my device and run my program it is writing to the port and not receiving any bytes from the port.

but when i open the COM port using PUTTY(which is used to open port and send some data) and send COMMAND it works and when i CLOSE PUTTY and NOW RUN MY PROGRAM now it is working fine,so i need to OPEN port with putty for the first time for my program to work.

may be i am not initialising some functions...:(

can anyone help me out in this,i am unable to find solution for the past day.thanks in advance...

my source code is:-

#include "stdafx.h"
#include <iostream>
#include <afx.h>

int main()
{
    using namespace std;
    int i=0;
//  cout << "Hello world!" << endl;



    HANDLE hSerial;

    hSerial = CreateFile("COM5",
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_WRITE | FILE_SHARE_READ,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);

    if(hSerial==INVALID_HANDLE_VALUE)
    {
        if(GetLastError()==ERROR_FILE_NOT_FOUND)
        {
//          TRACE("serial port does not exist for reading\n");
        //serial port does not exist. Inform user.
        }
//          TRACE("some other error,serial port does not exist for reading\n");
        //some other error occurred. Inform user.
    }

    DCB dcbSerialParams = {0};

    dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

    if (!GetCommState(hSerial, &dcbSerialParams)) 
    {
//                  TRACE("error getting state for reading\n");
    //error getting state
    }

    dcbSerialParams.BaudRate=9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    dcbSerialParams.fOutX=TRUE;
    dcbSerialParams.fInX=TRUE;
    if(!SetCommState(hSerial, &dcbSerialParams))
    {
    //TRACE("error setting state for reading\n");
    //error setting serial port state
    }
    COMMTIMEOUTS timeouts={0};

    timeouts.ReadIntervalTimeout=50;
    timeouts.ReadTotalTimeoutConstant=50;
    timeouts.ReadTotalTimeoutMultiplier=10;

    timeouts.WriteTotalTimeoutConstant=50;
    timeouts.WriteTotalTimeoutMultiplier=10;

    if(!SetCommTimeouts(hSerial, &timeouts))
    {
//      TRACE("some error occured for reading\n");
        //error occureed. Inform user
    }       
    int n=100,n1=100;
    char szBuff[100];
    DWORD dwBytesRead = 0;
    char szBuff1[100];
    DWORD dwByteswrote = 0;
    memset(szBuff1,0,100);
    memcpy(szBuff1,"LIST\r",5);
    FlushFileBuffers(hSerial);
    LPDWORD uf=0;
    GetCommModemStatus(hSerial,uf);
    TRACE("%d\n",uf);
    if(!WriteFile(hSerial, szBuff1,5, &dwByteswrote, NULL))
    {
                    cout << "error writing" ;
    }
    cout << szBuff1 << endl;
    cout << dwByteswrote << endl;
    dwByteswrote=0;
    while(1)
    {
        if(!ReadFile(hSerial, szBuff, n1, &dwBytesRead, NULL))
        {
            cout << "error reading";
            break;
        }
        else
        {
            cout << dwBytesRead << endl;
            szBuff[dwBytesRead]='\0';
            if(dwBytesRead>0)
            {
                cout << (szBuff);

                break;
            }
            else
            {
            }
        }
    }
    cin >> i;
}

Solution

  • You might try setting all the DCB options and clearing port errors

    // Common settings
    dcbSerialParams.DCBlength = sizeof( dcbSerialParams );
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.BaudRate = CBR_9600;
    dcbSerialParams.fParity = FALSE;
    dcbSerialParams.Parity = NOPARITY;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.fDtrControl = 0;
    dcbSerialParams.fRtsControl = 0;
    
    // If you say so
    dcbSerialParams.fOutX = TRUE;
    dcbSerialParams.fInX = TRUE;
    
    // Not so common settings
    dcbSerialParams.fBinary = FALSE;
    dcbSerialParams.fParity = FALSE;
    dcbSerialParams.fOutxCtsFlow = FALSE;
    dcbSerialParams.fOutxDsrFlow = FALSE;
    dcbSerialParams.fDsrSensitivity = FALSE;
    dcbSerialParams.fErrorChar = FALSE;
    dcbSerialParams.fNull = FALSE;
    dcbSerialParams.fAbortOnError = FALSE;
    
    // Clear errors
    unsigned long ulCommErr = 0;
    ClearCommBreak( hSerial );
    ClearCommError( hSerial, &ulCommErr, NULL );
    

    Probably unrelated, but I noticed I at some point added a security descriptor to CreateFile() in my serial code, I believe some configuration of Windows Server required it.

    // Allow access
    SECURITY_ATTRIBUTES  sa, *pSa = NULL;
    sa.nLength = sizeof( SECURITY_ATTRIBUTES );
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,SECURITY_DESCRIPTOR_MIN_LENGTH);
    if ( sa.lpSecurityDescriptor
         && InitializeSecurityDescriptor( (SECURITY_DESCRIPTOR*)sa.lpSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION )
         && SetSecurityDescriptorDacl( (SECURITY_DESCRIPTOR*)sa.lpSecurityDescriptor, TRUE, (PACL)NULL, FALSE ) )
        pSa = &sa;
    
    // Open the port
    hSerial = CreateFile(   x_pPort, 
                            GENERIC_READ | GENERIC_WRITE,
                            0,
                            pSa,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL );
    
    if ( pSa )
        LocalFree( pSa->lpSecurityDescriptor );