-1

I have to transmit information from a uController to my Laptop. When I create the file I get the following error. I think it is easier to troubleshoot if you see the script.

Because it says that 'char [20]' in 'LPCWSTR' doesnt fit, I changed it like this:

CreateFile((LPCWSTR)name, GENERIC..

Now I can compile the program, but I still get INVALID_HANDLE_VALUE, and I'm not able to open the Port.

My Serial routine:

#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "com_bg.hpp"

char name[20]="";       //between the "" inserting COM"X"

...

do
{
    port = CreateFile(name, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

    if (port==INVALID_HANDLE_VALUE)
    {
        k++;
    } 

}
while ((k<MAXCREAT) && (port==INVALID_HANDLE_VALUE));

if (k==MAXCREAT)
    return(ERR_COM);

...

My main Looks like this:

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <conio.h>
#include "com_bg.hpp"
#include "com._bg.cpp"
#include "tchar.h"

using namespace std;

int _tmain(int argc, _TCHAR *argv[])
{   
     printf("Hello\n");

     getch();

     err = openCom(8, 9600, NONE,1,RTSDTRLOW);

     if (err != OK)
     {
         closeCom();

         printf("Failed to OpenPort\n");

         return 0;
     }
    …
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sebastian
  • 19
  • 1

1 Answers1

1

Type-casting will not change the underlying encoding of strings. If you need to call CreateFile for a file or device name that is represented in a char * string, use CreateFileA, either explicitly or by changing your project settings to not use Unicode.

That is, of course, if you really want to pass a char * string to CreateFile. The more sensible option is to change the type of name from char to wchar_t (the Windows platform type being WCHAR).

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Becouse the COM Port is always Changing im typing in the char manually trough console. So i have to use the char. When I Change to "wchar_t" or "WCHAR" it say: *C2440: 'Initialisierung': 'const char [1]' cant 'wchar_t [20]'* – Sebastian Apr 25 '19 at 18:58
  • @Sebastian Initialize wide strings with the L-prefix: `L"Hello World";` – Govind Parmar Apr 25 '19 at 19:22