I managed to get a code from the net for PC to a machine communication via RS-232. I'm using Win32 Console Application in VS2010. I wanted to run it and see the outcome. But I have some errors which I am unable to rectify.
Heres's the code:
// HTHH.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
HANDLE hSerial;
int _tmain(int argc, _TCHAR* argv[])
{
hSerial = CreateFile("COM4",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if (hSerial == INVALID_HANDLE_VALUE)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
}
}
DCB dcbSerialParams = {0};
dcbSerial.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams))
{
}
dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParam))
{
}
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts))
{
}
char szbuff[n+1] = {0};
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial, szbuff, n, &dwBytesRead, NULL))
{
CloseHandle(hSerial);
return 0;
}
The errors are as follows:--
1>c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(17): error C2664: 'CreateFileW' : cannot convert parameter 1 from 'const char [5]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(28): error C2065: 'dcbSerial' : undeclared identifier
1>c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(28): error C2228: left of '.DCBlength' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(38): error C2065: 'dcbSerialParam' : undeclared identifier
1>c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(53): error C2065: 'n' : undeclared identifier
1>c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(56): error C2065: 'n' : undeclared identifier
1>c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(64): fatal error C1075: end of file found before the left brace '{' at 'c:\users\singanathan\documents\visual studio 2010\projects\hthh\hthh\hthh.cpp(10)' was matched ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Sorry, its very lenghty. Appreciate your advise.
Thanks
Unicode
from the code generation of your project. That will get rid of the error from"COM4:"
being aconst char [5]
which is not compatible withconst wchar_t *
(akaLPCWSTR
. The other errors are simple typos, so should just require looking at the line and fixing up what the problem is. – Mats PeterssonReadFileEx
. I've used the separate-thread-for-I/O model to implement serial ports in .NET because you don't control the message loop. In normal Win32-via-C++ programming that isn't necessary, you can useMsgWaitForMultipleObjectsEx
in your event loop and have I/O callbacks. – Ben Voigt