If you know, please let me know.
We are currently developing modem testers using the SerialPort
class on C#.
Is there a way to manually control RTS/CTS without using System.IO.Ports.Handshake.RequestToSend
?
The flow of data transmission and reception is as follows:
Repeat 1. through 6.
above.The SerialPort
class properties are
SerialPort.RtsEnable
→true
:RTS ON,false
:RTS OFFSerialPort.CtsHolding
→true
:CTSON,false
:CTS OFFI am thinking that
Attempted to route the RS232C cable so that the output of the RTS signal is fed into the CTS signal.
I used this RS232C cable to verify with the following code, but SerialPort.CtsHolding
does not become true
and times out.
Even if SerialPort.RtsEnable
is set to true
, does RTS not turn on (Hight)?
(Do I have to use Win32 API...)
public class SerialCommunicationTester:System.IDisposable
{
private System.IO.Ports.SerialPort_serialPort=null;
// 0. Open SerialPort.
public pool Open(
string portName,
intbaudRate,
int dataBits,
System.IO.Ports.Parity parity,
System.IO.Ports.StopBits stopBits,
int readTimeout,
int writeTimeout,
) {
_serialPort.PortName=portName;
_serialPort.BaudRate=baudRate;
_serialPort.DataBits=dataBits;
_serialPort.Parity=parity;
_serialPort.StopBits=stopBits;
_serialPort.Handshake=Handshake.None;
_serialPort.ReadTimeout=readTimeout;
_serialPort.WriteTimeout=writeTimeout;
try{
_serialPort.Open();
} catch(Exceptionex){
Console.WriteLine("SerialPort#OpenFailed...{0}",ex);
}
return_serialPort.IsOpen;
}
// 1. Set to RTS "High" and 2. Wait for CTS to go "High".
public bool RtsCtsFlowCtrl(uint timeout){
if(!_serialPort.IsOpen)return false;
bool result = true;
// 1. Set to RTS "High"
_serialPort.RtsEnable=true;
Stopwatch sw = new Stopwatch();
sw.Start();
// 2. Wait for CTS to go "High".
while(true){
Thread.Sleep(0);
if(_serialPort.CtsHolding)break;
if (0<timeout&&timeout<=sw.ElappedMilliseconds) {
result=false;
break;
}
}
sw.Stop();
return result;
}
// 4.Set to RTS "Low"
public void RtsOff() {
if(_serialPort.IsOpen)_serialPort.RtsEnable=false;
}
}
As far as the .NET source code is concerned, it is working as expected.In other words,
SerialPort.RtsEnable
The property eventually ends up in
EscapeCommFunction (_handle, <SerialPort.RtsEnable value >?SETRTS:CLRRTS);
is called, and the SerialPort.CtsHolding
property is
<GetCommModemStatus() Results>&MS_CTS_ON
refers to
SerialPort.CtsHolding is not true and times out.
The cause of may be another.For example, you set the timeout to 1 second, but
if (0<timeout&&timeout<=sw.ElappedMilliseconds) {
A comparison of may result in a timeout determination of 1 millisecond.
Incidentally, .NET provides TimeSpan
type to prevent such hourly errors, which can be described as TimeSpan.FromSeconds(1)
or TimeSpan.FromMillisconds(1)
.
© 2024 OneMinuteCode. All rights reserved.