In the win32 API it's possible to enable DTR handshake using fDtrControl of the DCB struct. However, the Handshake property of the C# SerialPort class allows only RTS flow control.
How can I do DTR handshaking in C#?
In the win32 API it's possible to enable DTR handshake using fDtrControl of the DCB struct. However, the Handshake property of the C# SerialPort class allows only RTS flow control.
How can I do DTR handshaking in C#?
As you think, there are probably two possible ways.
One is to deal with it within the scope of the .NET SerialPort API.
Specify a very large size for ReadBufferSize. Up to 2GB can be specified in the specifications.
The buffer size, in bytes. The default value is 4096; the maximum value is that of a positive int, or 2147483647.
Set DtrEnable to true if you somehow determine that your application has sufficient processing power. If your application does something heavy, set DtrEnable to false during that time.
It is not possible to perform timely processing such as controlled by a device driver, but if the buffer size is large, it is unlikely that data will be lost.
If the communication protocol specification defines a bidirectional DTR/DSR handshake, check DsrHolding before writing from the application.
This property is used in Data Set Ready/Data Terminal Ready (DSR/DTR) handshaking. The Data Set Ready (DSR) signal is usually sent by a modem to a port to indicate that it is ready for data transmission or data reception.
The other is to call the communication function of Win32API using P/Invoke.
There is a setting called fDtrControl in the DCB specified by SetCommState, and DTR_CONTROL_HANDSHAKE can be specified.
fDtrControl
The DTR (data-terminal-ready) flow control. This member can be one of the following values.
- DTR_CONTROL_DISABLE 0x00 Disables the DTR line when the device is opened and leaves it disabled.
- DTR_CONTROL_ENABLE 0x01 Enables the DTR line when the device is opened and leaves it on.
- DTR_CONTROL_HANDSHAKE 0x02 Enables DTR handshaking. If handshaking is enabled, it is an error for the application to adjust the line by using the EscapeCommFunction function.
If you want to use the bidirectional DTR/DSR handshake here, you can also specify fOutXDsrFlow.
fOutxDsrFlow
If this member is TRUE, the DSR (data-set-ready) signal is monitored for output flow control. If this member is TRUE and DSR is turned off, output is suspended until DSR is sent again.
If the device driver is properly implemented, DTR flow control should be enabled.
However, it cannot be used on platforms other than Windows, and all serial port processing including other functions must use the Win32API with P/Invoke.
For example, there is an article like this.
Use P/Invoke to Develop a .NET Base Class Library for Serial Device Communications
However, the download link seems to be disabled. This may be the case.
netserialcomm - default
sntcz/SnT.IO.Ports
A similar method would be to create a library that can be called from C# using the Win32 communication function API in C++/CLI.