RTS/DTR C++ control

Asked

Viewed 36 times

0

Performing an integration of an RS232 device, we found an obstacle that is related to the flow control using the RTS and DTR with the equipment. Using the Escapecommfunction function to manipulate the signals on these pins, it was not possible to establish communication with the apparatus, having no response to the communication boot command. Example:

Initializing the door

HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};

hSerial = CreateFile("\\\\.\\COM1", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL );

dcbSerialParams.BaudRate = CBR_19200;
dcbSerialParams.ByteSize = 7;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = EVENPARITY;
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
dcbSerialParams.fRtsControl = RTS_CONTROL_ENABLE;
SetCommState(hSerial, &dcbSerialParams);

timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
SetCommTimeouts(hSerial, &timeouts);

EscapeCommFunction(hSerial,CLRDTR);
EscapeCommFunction(hSerial,CLRRTS);

Sending command

EscapeCommFunction(hSerial,SETRTS)

DWORD bytes_written, total_bytes_written = 0;

if(!WriteFile(hSerial, bytes_to_send, sizeof(bytes_to_send), &bytes_written, NULL))
{
    fprintf(stderr, "Erro\n");
    CloseHandle(hSerial);
    return 1;
}
fprintf(stderr, "%d bytes escritos...", bytes_written);
EscapeCommFunction(hSerial,CLRRTS);

Visualizing answers

char buffer[32];
DWORD  bytes_read;

//loop infinito de leitura
while(true){
    EscapeCommFunction(hSerial,SETDTR)
    ReadFile(hSerial, buffer, sizeof(buffer), &bytes_read, NULL);
    if (bytes_read){
        fprintf(stderr, "Número de bytes lidos %d\n",bytes_read);
    }
    EscapeCommFunction(hSerial,CLRDTR)
}

There is no response from the equipment, however, we know that the command used has no errors, because the manufacturer provides a software that performs the same communication but not exporting the data, serving only for visualization, and making a Spy on the door during this communication, we verify the integrity of the.

There is something that could be going blank in this control or another way to be realized?

1 answer

0

Dear Paul,

I used serial a lot on Windows 98 -- thousands of years ago, therefore. A lot may have changed.

What I can say about that experience is that the manipulation of the Windows serial is enormously more complex than in Linux. Also, a lot of the Win32 API calls don’t work according to the documentation, and should be experienced point by point.

An important point is to do what I call paranoid programming. That is: test each function call, and call the corresponding GetLastError() when the call generates an error.

In your case, on the block Initializing the door, I would test if the call to CreateFile() generated a valid Handle. It would then confirm that the calls to EscapeCommFunction() generated errors.

It’s always important to call GetLastError() after every problem, to try to understand what happened.

Similarly in the other blocks: each call must be tested for error, and GetLastError() should be consulted to understand what happened.

What tutorial are you using to build your program ? It is important to find a good tutorial as a base -- of course, testing it enough, to see if it works with your equipment.

Trying to remember how I used the serial, I believe that your code is perhaps simpler than the one I used in practice. Examining the list of communication functions on the page Communications Functions, I believe I made use of several other functions, such as BuildCommDCB(), Setcommconfig() and maybe WaitCommEvent().

The code worked well at the time -- and I successfully communicated with telephone exchanges that did not understand RS-232 in the same way as Microsoft...

It’s stuff from 20 years ago, but I’ll try to locate the code somewhere in my junk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.