Jump to content

Using a class to open COM port problem

- - - - -

  • Please log in to reply
1 reply to this topic

#1
alirezan

alirezan

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
Hi guys,

I have this library that I should use for serial communication :

#ifndef WIN32SERIAL_H

#define WIN32SERIAL_H


#include <windows.h>


#define FDTABLE_LENGTH  8


class Win32Serial

{

public:

                            Win32Serial();

                           ~Win32Serial();


    // Register a serial object (with a given fileDescriptor)


    static Win32Serial *    wsCreate();

    static int              wsCreate( Win32Serial * );


    // UnRegister a serial object (ref. object pointer)


    static bool             wsDestroy( Win32Serial *& );

    

    // Called from the open handle.


    int                     wsOpen( const char * port, const char * prop );

    int                     wsClose( );


    int                     wsRead( void * buf, int bufLen );

    int                     wsWrite( void * buf, int bufLen );


    bool                    wsIsEOF();


    int                     fdForThis();

                            // return a fileDescriptor for this object instance (if registered)

                            // returns -1 if object was not registered using wsCreate();


    static Win32Serial *    objForFD( int fd );

                            // given a fileDescriptor, return the registered object, or NULL


private:


    HANDLE                  hSerial;    // COMM port


    // For emulating legacy-style serial communications via fileDescriptor.


    struct fdTableType

    {

        int             fd;

        Win32Serial *   obj;

    };

                            

    static fdTableType     fdTable[FDTABLE_LENGTH];


};


#endif //WIN32SERIAL_H



and I have written a simple Main function like this to set up a COM port:
#include <windows.h>

#include <stdio.h>

#include <string.h>

#include <time.h>


#include "Win32Serial.h"


int main (void) {


Win32Serial WCOM;

WCOM.wsOpen ( "COM1", NULL );

return 0;

}

However, everytime I compile this code I get the following error:

Quote

Error 2 error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' c:\users\anematollahi\documents\visual studio 2008\projects\vchannel_conapp\vchannel_conapp\win32serial.cpp 175 VChannel_ConApp

and that refers to this part of the code:
           // open interface

            // PRINT_("{Win32Serial::wsOpen} :: opening %d ...\n", ComPort );

            hSerial = CreateFile( port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );

            if ( hSerial==INVALID_HANDLE_VALUE )

            {

                if ( GetLastError() == ERROR_FILE_NOT_FOUND )

                {

                    PRINT_("{Win32Serial::wsOpen} :: Comport (%s) not found!\n", port );

                }

                else

                {

                    PRINT_("{Win32Serial::wsOpen} :: error on opening Comport (%s) [creation]!\n", port );

                }

                openOK = 0;

                break;

            }


I read online that if I put "L" in front of the COM port that I pass (i.e. L"COM1"), it would solve the problem but it gives me:

Quote

Error 2 error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' c:\users\anematollahi\documents\visual studio 2008\projects\vchannel_conapp\vchannel_conapp\win32serial.cpp 175 VChannel_ConApp
Error 3 error C2664: 'Win32Serial::wsOpen' : cannot convert parameter 1 from 'const wchar_t [5]' to 'const char *' c:\users\anematollahi\documents\visual studio 2008\projects\vchannel_conapp\vchannel_conapp\main.cpp 11 VChannel_ConApp

I am using Visual Studio 2008 on Windows 7 and I don't know how I can get rid of this problem. Simple casting to LPCWSTR won't help either :(

Anyone any ideas?

Thanks

Edited by dargueta, 28 April 2011 - 11:40 AM.


#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,719 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Hm. Try the following:

#include <tchar.h>


TCHAR *my_annoying_string = TEXT("VS is annoying.");


This should work for Unicode and ASCII programs without rewriting anything. The macro might be _TEXT instead of just TEXT. I don't remember.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users