Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Open Serial Port, handler from windows, error, the code works on standard C++
QtWS25 Last Chance

Open Serial Port, handler from windows, error, the code works on standard C++

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yczo
    wrote on 9 May 2016, 09:54 last edited by A Former User
    #1

    Hello, I'm trying to communicate a Qt program with a microcontroller board, (porting a program from C++). The original program works good on console, but moving the code from c++, to qt/c++ compile i get the next warning

    WriteFile(handlerP,bufferPort,strlen(bufferPort),&bytesRW,NULL);
    
    (from the above line)
    warning: C4267: 'argument' : conversion from 'size_t' to 'DWORD', possible loss of data
    
    

    and the next error

    handlerP = CreateFile(serialPort, GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
    
    (from the avobe line)
    error: C2664: 'HANDLE CreateFileW(LPCWSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE)' : cannot convert argument 1 from 'char []' to 'LPCWSTR'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    

    In theory must work.... ???
    No idea, any help will be appreciated

    thanks in advance, greetings.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 May 2016, 10:23 last edited by
      #2

      Hi,

      Since you are using a serial port to communicate with your board. Why not use QSerialPort ?

      As for your original problem, what type is serialPort ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yczo
        wrote on 9 May 2016, 10:53 last edited by
        #3

        It is a virtual COM, for USB. I program for windows and i think that QSerial port musst appart compiled and linkend, right?

        Greetings and thanks

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 9 May 2016, 11:46 last edited by
          #4

          If you are using Qt 5, no, it's in. You have to add QT += serialport to your .pro file to use it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sneubert
            wrote on 9 May 2016, 12:07 last edited by
            #5

            Hi yczo,

            the Windows API often has to version of a function.
            One for use with C-like ANSI String (char) and one for wide chars like e.g. utf16 (UNICODE).
            In windows.h the defines are mapped depending on the define for UNICODE.
            Your error is because your project somewhere defines UNICODE so the
            CreateFilefunction maps to CreateFileW.
            This function expects the FileName parameter to be of type wchar_t*, but your
            serialPort is of type char*.
            Either you set your project to be non UNICODE, or you change your code to use wchar_t instead of char.
            Bad practice would be to directly call CreateFileA.

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yczo
              wrote on 10 May 2016, 09:41 last edited by
              #6

              Thank you very much for your help!

              1 Reply Last reply
              0

              5/6

              9 May 2016, 12:07

              • Login

              • Login or register to search.
              5 out of 6
              • First post
                5/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved