none
Microsoft Visual C++ 2005 C++ open serial port

    Question

  •  Hi need help here when i try this and run it i have an error :

    error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'. Is there anyway to solve it?

    Thanks a million guys

    #include "stdafx.h"
    #include <windows.h>

    void PrintError( LPCSTR str)
    {
    LPVOID lpMessageBuffer;
    int error = GetLastError();
    FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM,
    NULL,
    error,
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
    (LPTSTR) &lpMessageBuffer,
    0,
    NULL
    );
    printf("%s: (%d) %s\n\n",str,error,lpMessageBuffer);
    LocalFree( lpMessageBuffer );
    }

    int main(int argc, char* argv[])
    {
    // open port for I/O
    HANDLE h = CreateFile(argv[1],
    GENERIC_READ|GENERIC_WRITE,
    0,NULL,
    OPEN_EXISTING,0,NULL);

    if(h == INVALID_HANDLE_VALUE) {
    PrintError("E012_Failed to open port");
    } else {
    // set timeouts
    COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
    DCB dcb;
    if(!SetCommTimeouts(h,&cto))
    PrintError("E013_SetCommTimeouts failed");

    // set DCB
    memset(&dcb,0,sizeof(dcb));
    dcb.DCBlength = sizeof(dcb);
    dcb.BaudRate = 19200;
    dcb.fBinary = 1;
    dcb.fDtrControl = DTR_CONTROL_ENABLE;
    dcb.fRtsControl = RTS_CONTROL_ENABLE;
    // dcb.fOutxCtsFlow = 1;
    // dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;

    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.ByteSize = 8;

    if(!SetCommState(h,&dcb))
    PrintError("E014_SetCommState failed");

    char buf[7];
    DWORD read = 0;
    DWORD write=1; // Number of bytes to write to serial port
    buf[0] = 72; // Decmial value to write to serial port
    WriteFile(h,buf,write,&write,NULL); // write is updated with the number of bytes written

    ReadFile(h,buf,sizeof(buf),&read,NULL); // read is updated with the number of bytes read
    DWORD i;
    for (i=0; i<read; i++)
    printf("%i ", (unsigned char)buf[i]);

    CloseHandle(h);
    }

    return 0;
    }

    Sunday, November 09, 2008 3:17 AM

Answers

  • You are using the default project setting for a unicode build (16 bit characters).  You can change the setting: Project, Properties, Configuration Properties, General, Character Set.  Select 'Multibyte  character set' to turn off unicode.

    Alternatively, if you let Visual Studio build a skeletal console project for you it will provide:

    int _tmain(int argc, _TCHAR* argv[])
    {
      return 0;
    }

    ...and you can stick with the unicode build, which requires you to prefix constant text strings with _T or L

    Sunday, November 09, 2008 4:01 AM

All replies

  • You are using the default project setting for a unicode build (16 bit characters).  You can change the setting: Project, Properties, Configuration Properties, General, Character Set.  Select 'Multibyte  character set' to turn off unicode.

    Alternatively, if you let Visual Studio build a skeletal console project for you it will provide:

    int _tmain(int argc, _TCHAR* argv[])
    {
      return 0;
    }

    ...and you can stick with the unicode build, which requires you to prefix constant text strings with _T or L

    Sunday, November 09, 2008 4:01 AM
  • When i run this program nothing seems to work. With no errors but some how nothing is working on my servo. Thanks alot for solving  the previous problem that i encounter.

    What does this seems to show?
    .

     'Serial Port C++.exe': Loaded 'C:\Documents and Settings\Pioneer3\Desktop\Serial Port C++\debug\Serial Port C++.exe', Symbols loaded.
    'Serial Port C++.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded.
    'Serial Port C++.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded.
    'Serial Port C++.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', Symbols loaded.
    'Serial Port C++.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded.
    The program '[1664] Serial Port C++.exe: Native' has exited with code 0 (0x0)
    Monday, November 10, 2008 8:53 AM
  • set some breakpoints and try debug it and you can see the behavior of your program.
    programmer on windows platform
    Monday, November 10, 2008 9:52 AM