locked
Serial port communication error, CreateFile function

    Question

  • Dear all,

    I have been found out some solutions to send data over serial communication,COM1. I attempted find some code in website. Finally, I found the simple and correct one below this......

     

    But my problem is this program is working only in Visual C++6, when I used Visual C++ 2005. Some error was happened.

    error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'

     

    That is because Visual C++2005 don't understood function CreateFile. How should I do? Has some body ever faced this problem like me.

     

    Thank you in advance 

    GerRobot

    ----------------------------------------------------------------------------------------------------------------------------

     

    #include <windows.h>
    #include <commctrl.h>
    #include <iostream>
    #include <strsafe.h>

    using namespace std;

    void main()
    {
        HANDLE hComm;

        int write_status;
        int close_status;

        char message[17]= "$CALL L201 L200\n";

        DWORD num_bytes_written = 0;

        hComm = CreateFile( "COM1", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        write_status=WriteFile(hComm, message, sizeof(message), &num_bytes_written, NULL);
        close_status=CloseHandle(hComm);

        cout<<"CreateFile  : "<<hComm<<endl;
        cout<<"GetLastError: "<<GetLastError()<<endl;
        cout<<"WriteFile   : "<<write_status<<endl;
        cout<<"CloseHandle : "<<close_status<<endl;
    }
    Monday, January 14, 2008 5:31 PM

Answers

  • Try this.

    Code Block

    hComm = CreateFile(TEXT( "COM1"), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);


    Thanx
    Monday, January 14, 2008 6:08 PM

All replies

  • Try this.

    Code Block

    hComm = CreateFile(TEXT( "COM1"), GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);


    Thanx
    Monday, January 14, 2008 6:08 PM
  • Woww

    Hi thank you so much Pintu Shukla, your program is work!!!

     

    Would I trouble you again. Do you have some example programs for sending and receiving data from serial port in visual studio C++ 2005 version. Normally, example programs in internet. I found only in version VC++ 6.0.

     

    thank you again.

    GerRobot

    Monday, January 14, 2008 8:52 PM

  • Glad to help you. Simply have a look in MSDN there they have given example for CreateFile . If still problem you can ping here.

    Thanx
    Tuesday, January 15, 2008 5:47 AM
  • it helped me... thanks a lot buddy
    Wednesday, February 13, 2008 1:27 AM
  • I have a problem...

    when I place the lpFileName to a variable instead of directly specifying which port to Open like the example above TEXT("COM1"), it fails to CreateFile.

    like for example:

    exp_powermonitor_connection.exppm_com = CreateFile(
                                                            portname
                                                            ,GENERIC_READ | GENERIC_WRITE
                                                            ,0                       
                                                            ,NULL                   
                                                            ,OPEN_ALWAYS           
                                                            ,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED   
                                                            ,NULL);   

    How can I resolve this?

    Thanks for any help in advance...
    Thursday, February 14, 2008 1:44 AM
  • First thing i would like to suggest you make a new thread to ask the question .So that you can get multiple answes for your question because as this question is marked ans so no one will check it again. Second thing in your case Just use GetLastError() to know what is the actual problem.

    Thatx
    Thursday, February 14, 2008 8:22 AM
  • hi,i had try to change the command and i successful debug the program but the GetLastError: show value 87. may i know why and how to solved the problem!i din connect to the any device to the com 1 that time.is it the reason?thanks.

    Monday, March 24, 2008 7:01 PM
  • Try checking the error code against the list:

    http://msdn2.microsoft.com/en-us/library/ms681381.aspx

    Don't know if that would be the EXACT list you'd use, but MS should have em up there.  According to this list, you have an invalid parameter...

    Good luck.
    Tuesday, April 01, 2008 4:49 PM