Hello,
I am doing simple read/write program for serial port. I am faceing a error that i don´t understand. Error is:

"error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [5]' to 'LPCWSTR' "

My whole program:

#include <windows.h>
#include <iostream>
#include <string>

using namespace std;

void set_com_pin(bool value);


int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        cerr<<"bad usage !"<<endl;
        return -1;
    }

    bool onoff;
    if(string(argv[1])==string("-on"))onoff=true;
    else if(string(argv[1])==string("-off"))onoff=false;
    else
    {
        cerr<<"bad parameters !"<<endl;
        return -1;
    }

    set_com_pin(onoff);
    return 0;
}

void set_com_pin(bool value)
{
// open port for I/O
HANDLE hPort = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if(hPort == INVALID_HANDLE_VALUE) {
cout<<"Failed to open port" <<endl;
    } else {
// set timeouts
COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };


if(!SetCommTimeouts(hPort,&cto))
    cout<<"SetCommTimeouts failed" <<endl;


// set DCB
DCB 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(hPort,&dcb))
    cout<<"SetCommState failed" << endl;

    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(hPort, buf, write, &write, NULL);

    ReadFile(hPort, buf, sizeof(buf), &read, NULL);

    DWORD i;
    for(i=0; i<read; i++)
        cout<<"%i", (unsigned char)buf[i];

    CloseHandle(hPort);
}


}

Thanks in advance

As the error says, the first parameter must be LPCWSTR and not directly "COM1", so you can try writing the next(before the createfile call):

LPCWSTR abc = "COM1";

and rewrite the createfile to :

HANDLE hPort = CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

Thanks,
But it still have new error. That is
"error C2440: 'initializing' : cannot convert from 'const char [5]' to 'LPCWSTR' "
Please chack it again.

try the below one

//HANDLE hPort = CreateFile("COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
HANDLE hPort = CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

Hope the above help, you didn't ask what is LPCWSTR ok :). So I am providing the solution only :P.

Just a curiosity, what Visual Studio version are you using ?
Because i tryied both :

LPCWSTR abc = "COM1";
	CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

and

CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

And both worked under Visual Studio 2008 Professional.

Thanks, Now it didn´t show error.
I want to read/write data through "COM1". Could you check my program, am i in correct way? This is my first program for serial port/Embedded programming.

Thanks for your help.

yes Its seems fine with a cursory look.

Good luck (Y).

Hello jan140,
I am using Microsoft Visual Studio 2008.

LPCWSTR abc = "COM1";
CreateFile(abc, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

This shows an error that i mentioned before, but

CreateFile(TEXT("COM1"), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

This is OK. I don´t know why!!!

Thanks for your help.

http://msdn.microsoft.com/en-us/library/7dzey6h6(VS.71).aspx
Visual Studio 6 for example defaults to ANSI
Visual Studio 2008 defaults to UNICODE

The Win32 API functions (like CreateFile) are already wrapped, but many standard functions in the CRT are not.

Also, you need to use the TEXT() or _T() macro for ALL your string constants.

Read the rest of the MSDN link.

Hi,
I'm trying simple programme to open the visual com port and send data on it. could someone help me to advice the source code or the way to start? I'm using VC++ Express edition.

Thanks in advance.

commented: Start your own thread. -1

Hi,
I'm trying simple programme to open the visual com port and send data on it. could someone help me to advice the source code or the way to start? I'm using VC++ Express edition.

Thanks in advance.

I think, it will helpful to you.

// Open the serial port.
HANDLE hPort = CreateFile (TEXT("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if(hPort == INVALID_HANDLE_VALUE) {
	cout<<"hPort INVALID" <<endl;
} else{
	cout<< "Handle Port Success" <<endl; 


DCB PortDCB;

// Initialize the DCBlength member. 
PortDCB.DCBlength = sizeof (DCB); 

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600;					// Current baud 
PortDCB.fBinary = TRUE;						// Binary mode; no EOF check 
PortDCB.fParity = TRUE;						// Enable parity checking 
PortDCB.fOutxCtsFlow = FALSE;				// No CTS output flow control 
PortDCB.fOutxDsrFlow = FALSE;				// No DSR output flow control 
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;	// DTR flow control type 
PortDCB.fDsrSensitivity = FALSE;			// DSR sensitivity 
PortDCB.fTXContinueOnXoff = TRUE;			// XOFF continues Tx 
PortDCB.fOutX = FALSE;						// No XON/XOFF out flow control 
PortDCB.fInX = FALSE;						// No XON/XOFF in flow control 
PortDCB.fErrorChar = FALSE;					// Disable error replacement 
PortDCB.fNull = FALSE;						// Disable null stripping 
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;	// RTS flow control 
PortDCB.fAbortOnError = FALSE;				// Do not abort reads/writes on error
PortDCB.ByteSize = 8;						// Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY;					// 0-4=no,odd,even,mark,space 
PortDCB.StopBits = ONESTOPBIT;				// 0,1,2 = 1, 1.5, 2 

// Configure the port according to the specifications of the DCB structure.
if (!SetCommState (hPort, &PortDCB))
{
  // Could not configure the serial port.
  cout<<"SetCommState failed" <<endl;
  return FALSE;
}


 
// Retrieve the timeout parameters for all read and write operations on the port. 
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 50;  
CommTimeouts.ReadTotalTimeoutMultiplier = 50;  
CommTimeouts.ReadTotalTimeoutConstant = 50;    
CommTimeouts.WriteTotalTimeoutMultiplier = 50;  
CommTimeouts.WriteTotalTimeoutConstant = 50;    

// Set the timeout parameters for all read and write operations on the port. 
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
  cout<<"SetCommTimeouts failed" <<endl;
  return FALSE;
}
}

char buf[20];
DWORD read = 0;
DWORD CommModemStatus;
DWORD write=1; // Number of bytes to write to serial port
buf[0] = 72; // Decmial value to write to serial port

while (hPort != INVALID_HANDLE_VALUE) 
{
WaitCommEvent(hPort, &CommModemStatus, 0);
//cout<<"Wait Completed" << endl;
//cout<< CommModemStatus;
if (CommModemStatus!=0)
{

WriteFile(hPort, buf, write, &write, NULL); // write is updated with the number of bytes written

ReadFile(hPort, buf, sizeof(buf), &read, NULL); // read is updated with the number of bytes read

//write code to Print date

}
}

CloseHandle(hPort);
commented: Giving away code. -1

thank you,jp071. i got it :-)

I think, it will helpful to you.

// Open the serial port.
HANDLE hPort = CreateFile (TEXT("COM3"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

if(hPort == INVALID_HANDLE_VALUE) {
	cout<<"hPort INVALID" <<endl;
} else{
	cout<< "Handle Port Success" <<endl; 


DCB PortDCB;

// Initialize the DCBlength member. 
PortDCB.DCBlength = sizeof (DCB); 

// Get the default port setting information.
GetCommState (hPort, &PortDCB);

// Change the DCB structure settings.
PortDCB.BaudRate = 9600;					// Current baud 
PortDCB.fBinary = TRUE;						// Binary mode; no EOF check 
PortDCB.fParity = TRUE;						// Enable parity checking 
PortDCB.fOutxCtsFlow = FALSE;				// No CTS output flow control 
PortDCB.fOutxDsrFlow = FALSE;				// No DSR output flow control 
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;	// DTR flow control type 
PortDCB.fDsrSensitivity = FALSE;			// DSR sensitivity 
PortDCB.fTXContinueOnXoff = TRUE;			// XOFF continues Tx 
PortDCB.fOutX = FALSE;						// No XON/XOFF out flow control 
PortDCB.fInX = FALSE;						// No XON/XOFF in flow control 
PortDCB.fErrorChar = FALSE;					// Disable error replacement 
PortDCB.fNull = FALSE;						// Disable null stripping 
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;	// RTS flow control 
PortDCB.fAbortOnError = FALSE;				// Do not abort reads/writes on error
PortDCB.ByteSize = 8;						// Number of bits/byte, 4-8 
PortDCB.Parity = NOPARITY;					// 0-4=no,odd,even,mark,space 
PortDCB.StopBits = ONESTOPBIT;				// 0,1,2 = 1, 1.5, 2 

// Configure the port according to the specifications of the DCB structure.
if (!SetCommState (hPort, &PortDCB))
{
  // Could not configure the serial port.
  cout<<"SetCommState failed" <<endl;
  return FALSE;
}


 
// Retrieve the timeout parameters for all read and write operations on the port. 
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);

// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = 50;  
CommTimeouts.ReadTotalTimeoutMultiplier = 50;  
CommTimeouts.ReadTotalTimeoutConstant = 50;    
CommTimeouts.WriteTotalTimeoutMultiplier = 50;  
CommTimeouts.WriteTotalTimeoutConstant = 50;    

// Set the timeout parameters for all read and write operations on the port. 
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
  cout<<"SetCommTimeouts failed" <<endl;
  return FALSE;
}
}

char buf[20];
DWORD read = 0;
DWORD CommModemStatus;
DWORD write=1; // Number of bytes to write to serial port
buf[0] = 72; // Decmial value to write to serial port

while (hPort != INVALID_HANDLE_VALUE) 
{
WaitCommEvent(hPort, &CommModemStatus, 0);
//cout<<"Wait Completed" << endl;
//cout<< CommModemStatus;
if (CommModemStatus!=0)
{

WriteFile(hPort, buf, write, &write, NULL); // write is updated with the number of bytes written

ReadFile(hPort, buf, sizeof(buf), &read, NULL); // read is updated with the number of bytes read

//write code to Print date

}
}

CloseHandle(hPort);

>>>>Hi,
if i want to transfer .txt file instead of message,how could i do?

Dear All,
I want to read ini file for configuration. Who can send me a simple code?
Thank you in advance
Hoshang

Dear All,
I want to read ini file for configuration. Who can send me a simple code?
Thank you in advance
Hoshang

Sorry to say this but we're not a coding service meaning we don't provide code but only helps members from theirs

If you need further help start a new thread (instead of replying to an old one) if your troubled by a problematic code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.