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