简体   繁体   中英

How can I read from an RS485 COM port in C++ with Windows?

I try to control a voltage source via RS485 (via a USB to RS485 converter). I use Standard C++ in Visual Studio. I wrote the following code. Writing works fine, the voltage source responds to all input, but reading back bytes is difficult for me. For example, the answer might be the following string: "12.000" (31 32 2E 30 30 30 0A)

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

const unsigned char oe = static_cast<unsigned char>(148);

int main()
{

    //create new handle
    HANDLE hComm;
    hComm = CreateFile(L"\\\\.\\COM5",
        GENERIC_READ | GENERIC_WRITE,
        0,
        0,
        OPEN_EXISTING,
        FILE_FLAG_OVERLAPPED,
        0);
    if (hComm == INVALID_HANDLE_VALUE)
        printf("CreateFile handle failed ERROR: %d.\n", GetLastError());

    /*
    //Timwouts
    COMMTIMEOUTS CTO;
    CTO.ReadIntervalTimeout = 500;
    CTO.ReadTotalTimeoutConstant = 500;
    CTO.ReadTotalTimeoutMultiplier = 500;
    CTO.WriteTotalTimeoutConstant = 500;
    CTO.WriteTotalTimeoutMultiplier = 500;

    //build DCB strukture
    DCB dcb;

    if (!BuildCommDCB(L"COM5 baud=115200 parity=N data=8 stop=1", &dcb))
    {
        MessageBox(0, L"Error BuildCommDCB", L"Test", MB_OK);
    }

    if (!SetCommTimeouts(hComm, &CTO))
    {
        MessageBox(0, L"Error CommTimeouts", L"Test", MB_OK);
    }
    */

    //Variables for read RS485
    char sBuffer[1024];

    //Variables for user input values
    char parameter1;
    float floatV;
    char parameter2;

    while (true) {
        std::cout << "Hallo, was m" << oe << "chten Sie tun?\n";
        std::cin >> parameter1 >> floatV >> parameter2;
        //std::cout << "parameter1: " << parameter1 << "\n";
        //std::cout << "floatV: " << floatV << "\n";
        //std::cout << "parameter2: " << parameter2 << "\n";
        //system("cls");

        if (parameter1 == 'Q' && parameter2 == 'V') {
            uint32_t Voltage = (uint32_t)(floatV * 1000);
            char myString[30];
            sprintf_s(myString, "%f", floatV);

            //Set new voltage
            //VSET:
            TransmitCommChar(hComm, 'V');
            TransmitCommChar(hComm, 'S');
            TransmitCommChar(hComm, 'E');
            TransmitCommChar(hComm, 'T');
            TransmitCommChar(hComm, ':');
            //Wert
            TransmitCommChar(hComm, myString[0]);
            TransmitCommChar(hComm, myString[1]); 
            TransmitCommChar(hComm, myString[2]);
            TransmitCommChar(hComm, myString[3]);
            TransmitCommChar(hComm, myString[4]);
            TransmitCommChar(hComm, myString[5]);
            //ENTER
            TransmitCommChar(hComm, 0x0A);

            //Get the voltage of the source
            //Control V
            //56 53 45 54 3F 0A
            TransmitCommChar(hComm, 'V');
            TransmitCommChar(hComm, 'S');
            TransmitCommChar(hComm, 'E');
            TransmitCommChar(hComm, 'T');
            TransmitCommChar(hComm, '?');
            TransmitCommChar(hComm, 0x0A);

            //Sleep(100);
            /*
            LPVOID buffRead = 0;
            DWORD dwBytesRead = 0;
            if (!ReadFile(hComm, &buffRead, 6, &dwBytesRead, NULL))
            {
                printf("error reading from input buffer \n");
            }
            printf("Data read from read buffer is \n %s \n", (char*)buffRead);
            */

            DWORD dwBytesRead = 0;
            char bytes_to_receive[7];

            if (!ReadFile(hComm, bytes_to_receive, 7, &dwBytesRead, NULL)) {
                printf("SetCommState failed. Error: %d.\n", GetLastError());
                //CloseHandle(hComm);
                //return (4);
            }
            else {
                printf("Bytes read %d -> %s\n", dwBytesRead, bytes_to_receive);
            }

            /*if (ReadFile(hComm, sBuffer, 7, NULL, NULL)) {
                printf("%s \n", sBuffer);
            }
            else {
                printf("Reading file fails! \n");
            }*/

            /*UINT numberBytesRead = _lread(
                HFILE  hFile,
                LPVOID lpBuffer,
                UINT   uBytes
            );*/
        }
    }
}

The output I get is:

Hallo, was möchten Sie tun?
Q12V
SetCommState failed. Error: 87.
Hallo, was möchten Sie tun?

The voltage source jumps to 12V, so she got the command.

EDIT: I changed FILE_FLAG_OVERLAPPED to FILE_ATTRIBUTE_NORMAL, now I get:

Hallo, was möchten Sie tun?
Q12V
Bytes read 7 -> 12.000
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠G┤°V
Hallo, was möchten Sie tun?

So it basically seems to work, but I get a lot of strange signs.

EDIT 2: Another problem that I still face is that after restarting the computer nothing works anymore and I need to start HTerm, press connect and disconnect, what apparently makes a few initializations that I miss in my program. After that it works till the next restart of the computer. Do you have any ideas what I might not have in my program?

You get strange signs because bytes_to_receive is not a null-terminated string. printf keeps reading bytes until it sees a null (0) byte. You don't put a null byte in bytes_to_receive , so it keeps printing until it goes off the end and finds one somewhere else.

ReadFile tells you how many bytes were read, in dwBytesRead , so you can add a null byte for printf : bytes_to_receive[dwBytesRead] = '\\0'; .

Make sure to increase the size of bytes_to_receive to 8 because now you are writing one extra byte. Still pass 7 to ReadFile because you don't want to read 8 bytes - you are adding the null byte yourself after reading the data.

Ok, in the meantime I solved the problem. Some extra code is needed, I hope I can help the next one who has the same problem, otherwise I am open to suggestions for improvement. Thank you for your support, I have distributed upvotes to all who have helped me.

DCB Dcb;
COMMTIMEOUTS Cto;
HANDLE hComm;

//Create file Handle
hComm = CreateFile(L"\\\\.\\COM5",
    GENERIC_READ | GENERIC_WRITE,
    0,
    0,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    0);
if (hComm == INVALID_HANDLE_VALUE) {
    printf("CreateFile handle failed ERROR: %d.\n", GetLastError());
}

//Data Center Bridging
Dcb.DCBlength = sizeof(Dcb);
GetCommState(hComm, &Dcb);
Dcb.BaudRate = CBR_115200;
Dcb.fParity = false;
Dcb.fNull = false;
Dcb.StopBits = ONESTOPBIT;
Dcb.Parity = NOPARITY;
Dcb.ByteSize = 8;
SetCommState(hComm, &Dcb);

//Timeouts
Cto.ReadIntervalTimeout = 0;
Cto.ReadTotalTimeoutMultiplier = 0;
Cto.ReadTotalTimeoutConstant = 0;
Cto.WriteTotalTimeoutMultiplier = 0;
Cto.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &Cto);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM