Reset when i open serial port from C#

Hello everyone .

i have a issue with a c# program im writing , the serial device is not a arduino board but since arduino gets its reset from the DRT, the device expreriances reset-crash behavor with the issue here being DTR related i feel that can be asked here.

i wrote the program first, as a prototype in python

with serial.Serial(PORT_ID, 115200) as ser: # 8N1 default

    if ser.rts:
        logging.debug("RTS OK from tester")
        ser.dtr = True
        logging.debug(f"DTR OK from PC (actual {ser.dtr})")
        #ser.write(bytes(SET_L3_CONN_SETTINGS_BYTES))
        ser.write(bytes(READ_L1_BYTES))

and it works fine . i can talk to the instrument , get replies fine just fine.

but , in the c# implementation with similar code setup :

 SerialPort _serialPort = new SerialPort();

 _serialPort.PortName = "COM3";
 _serialPort.BaudRate = 115200;
 _serialPort.DataBits = 8;
 _serialPort.Parity = Parity.None;
 _serialPort.StopBits = StopBits.One;
 _serialPort.Handshake = Handshake.None;
 //_serialPort.DtrEnable = true;
 //_serialPort.RtsEnable = true;
 if (_serialPort.Handshake == Handshake.None)
 {
     _serialPort.DtrEnable = true;
     _serialPort.RtsEnable = true;
 }

//[...]

 try
 {
     _serialPort.DtrEnable = false;
     _serialPort.Open();
 }
 catch (Exception e) { 
 Debug.WriteLine(e);
 }
     if (_serialPort.IsOpen==true)
     {

     Console.WriteLine(_serialPort.RtsEnable + "RTS  from tester");
     _serialPort.DtrEnable = true;
     Console.WriteLine("DTR OK from PC(Actual: " + _serialPort.DtrEnable);

but everytime , when i run this program it crashes-resets the device , due to the DRT line being set briefly to false when runnig the open() command then back to true , this crashes the device before even leaving the try catch.

using a serial port sniffer saw that this behavor is not presant when i run the python code .
the device responds correnctly when my code follows the sequence
Port open-> wait for RTS from instrument -> set DTR-> start talking

While investigating the topic i found in this forum that i had to set serialPort.Handshake = Handshake.None; but it has zero effect ! the dtr is still being pulsed !

Any ideas would be highly appreciated cause im at a complete loss and i bearly know any python to implement my program there ...

thanks in advance and sorry if you find this offtopic

Having the Arduino reboot upon opening the Serial connection is indeed common. Usually this is dealt with the Arduino sending an ACK that it is ready at the end of the setup function (or at least after the Serial.begin()) and catch that on the PC side before starting the communication.

I found that to work all the time regardless of the programming language on the PC/Mac side.

so it goes like this

PC opens Serial port and awaits for an 'OK' message

  • arduino reboots
  • arduino executes setup()
  • arduino configures its serial port with Serial.begin()
  • arduino sends ACK like Serial.println("OK");

PC receives the "OK\r\n" message and knows the Arduino is fully ready
PC starts sending commands

  • arduino reads the incoming message asynchronously using techniques described here Serial Input Basics. No blocking code.

if you want to play with this concept in python, I've written a small tutorial ➜ See Two ways communication between Python3 and Arduino

Yes i know this .
What is not common is that even when i set in c#

_serialPort.Handshake = Handshake.None;

it still has the same behavor ...
Everyone on the internet (MSDN docs, stackoverflow etc) states that with Handshake.None; i should be able to get the behavor i want .. but it resets every time , as if it ignores it ...
any idea whats going wrong?

keep in mind i cannot tweek the code or the hardware of the device.

What is that device?

its a TM-600p vdsl2 tester that connects via CH340 serial to a ancient pc software for dumping-downloading the test results or change setings .
The software is very clunky so i reverse engineered its Serial protocol using some serial port monitors etc to make my own version.

im at a point that i can control the device from python fine just fine but in c# (my target language so i can build a winforms ui easily since i do not know Qt well enough do it with python)
once i open the port , dtr gets triggered and the device resets .

A behavor exacly the same as the arduinos , thus i posted here since its tangent with the forum.
In fact the mentions regarding Serial port handshaking i found first were in this forum . But no matter what i try i cannot get the correct functionality .
It works with the python code in #1 but with the c# the instruments is crashing (or reseting the arduino since i started debuging with a arduino so i wont damage the instrument with all these crashes , when i get the port to open without a AVR reset i sould be able to use it on the TM600)

its as if C# ignores the _serialPort.Handshake = Handshake.None; command!

any ideas would be highly appreciated , im at a complete loss ...

I don’t know windows’ bugs well enough to say it’s something on their side… if it works with Python I would go for that - Creating a small UI is not very complicated and if you don’t want to learn that I’m sure chatGPT can generate one for you (there are tons of examples it has been trained on)

This is normal and correct behavior for a serial port. You open a serial port, DTR goes LOW (for the duration of the "connection.") You need "special" code to override such behavior, and perhaps C# doesn't provide it. (Note that DTR is an active-low signal. LOW means "ready.")

Turning off "handshake" probably means that the code will let you write data to the port whether or not it receives the expected signal responses from the serial port.

running the following blink/serial test on a UNO

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  Serial.println("Blink serial test ");
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  Serial.println(digitalRead(LED_BUILTIN));
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED off by making the voltage LOW
  delay(1000);                     // wait for a second
  Serial.println(digitalRead(LED_BUILTIN));
}

trying the following C# console program where DTR is set false on port open

// C# receive text from serial port

using System;
using System.IO.Ports;

namespace ConsoleTerminal
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort serialPort = new SerialPort() ;
            serialPort.PortName = "COM18";
            serialPort.BaudRate = 115200;
            serialPort.DtrEnable=false;
            serialPort.Open();
            Console.WriteLine("C#  receive text from serial port");
            while (true)
            {
                string s = serialPort.ReadLine();
                Console.WriteLine(s);
            }
         }
    }
}


on first execution resets UNO
image

subsequent runs don't appear to reset
image

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.