1
votes

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

1
First step would probably be to turn off Unicode from the code generation of your project. That will get rid of the error from "COM4:" being a const char [5] which is not compatible with const wchar_t * (aka LPCWSTR. The other errors are simple typos, so should just require looking at the line and fixing up what the problem is.Mats Petersson
Also Serial communication is not trivial... I suggest to use a read-to-use library, like codeproject.com/Articles/992/Serial-library-for-CJochen Kalmbach
@Jochen: I find that the Win32 API is more ready-to-use than most wrappers.Ben Voigt
@Ben: Most wrappers use the Win32 API ;) ... For beginners: Good wrappers are better then finding out by yourself, what you need to know in order to read data from the port... from my Point of view, this is not trival.Jochen Kalmbach
@Jochen: Most wrappers try to hide the Win32 API, which is exceptionally well-designed, behind a facade designed by amateurs. For example, in the library you linked, there is a claim that Win32 serial I/O functions don't support an event driven callback model. That's false, one is already provided in ReadFileEx. 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 use MsgWaitForMultipleObjectsEx in your event loop and have I/O callbacks.Ben Voigt

1 Answers

0
votes

You are compiling targeting UNICODE and so CreateFile maps to CreateFileW which expects wide character arrays, wchar_t*. But "COM4" is a char literal. Change it to L"COM4".

The compiler told you that dcbSerial is undeclared. It's right. And same for dcbSerialParam. The variable you declared is named dcbSerialParams.

And you did not declare n at all.