60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
|
|
|
|||
|
|
using System.IO.Ports;
|
|||
|
|
|
|||
|
|
string portName = "COM3";
|
|||
|
|
|
|||
|
|
// Create serial port instance (9600 baud rate is default standard)
|
|||
|
|
using (SerialPort port = new SerialPort(portName, 9600))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
port.Open();
|
|||
|
|
Console.WriteLine($"Successfully opened {portName}.\n");
|
|||
|
|
|
|||
|
|
// --- Step 1: DTR High, RTS Low ---
|
|||
|
|
Console.WriteLine("Step 1: Setting DTR = HIGH, RTS = LOW");
|
|||
|
|
port.DtrEnable = true;
|
|||
|
|
port.RtsEnable = false;
|
|||
|
|
PrintStatus(port);
|
|||
|
|
Thread.Sleep(1000);
|
|||
|
|
|
|||
|
|
// --- Step 2: DTR Low, RTS High ---
|
|||
|
|
Console.WriteLine("\nStep 2: Setting DTR = LOW, RTS = HIGH");
|
|||
|
|
port.DtrEnable = false;
|
|||
|
|
port.RtsEnable = true;
|
|||
|
|
PrintStatus(port);
|
|||
|
|
Thread.Sleep(1000);
|
|||
|
|
|
|||
|
|
// --- Step 3: Both DTR and RTS High ---
|
|||
|
|
Console.WriteLine("\nStep 3: Setting DTR = HIGH, RTS = HIGH");
|
|||
|
|
port.DtrEnable = true;
|
|||
|
|
port.RtsEnable = true;
|
|||
|
|
PrintStatus(port);
|
|||
|
|
Thread.Sleep(1000);
|
|||
|
|
|
|||
|
|
// --- Step 4: Both DTR and RTS Low ---
|
|||
|
|
Console.WriteLine("\nStep 4: Setting DTR = LOW, RTS = LOW");
|
|||
|
|
port.DtrEnable = false;
|
|||
|
|
port.RtsEnable = false;
|
|||
|
|
PrintStatus(port);
|
|||
|
|
Thread.Sleep(1000);
|
|||
|
|
|
|||
|
|
port.Close();
|
|||
|
|
Console.WriteLine("\nDone. Port closed successfully.");
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"Error: {ex.Message}");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
static void PrintStatus(SerialPort port)
|
|||
|
|
{
|
|||
|
|
// Outputs (driven by PC)
|
|||
|
|
Console.WriteLine($" [Driven Outputs] DTR: {port.DtrEnable} | RTS: {port.RtsEnable}");
|
|||
|
|
|
|||
|
|
// Inputs (read from connected hardware)
|
|||
|
|
Console.WriteLine($" [Hardware Inputs] CTS: {port.CtsHolding} | DSR: {port.DsrHolding} | CD: {port.CDHolding}");
|
|||
|
|
}
|