0

I am using the code found at the following repository:

https://github.com/Festo-se/PGVA-1/blob/main/examples/c%23/src/driver/PgvaDriver.cs

I use the following line of code to connect using a serial port:

        pgva = new PgvaDriver("serial", comboBoxPort.Text, 8502, "192.168.0.199", 115200, 16);

And I am able to connect to the serial port chosen in comboBoxPort. How do I go about closing the port? If I just try

port.Close();

I receive a number of error messages:

Read Error: System.InvalidOperationException: The port is closed.
   at System.IO.Ports.SerialPort.DiscardInBuffer()
   at Modbus.Serial.SerialPortAdapter.DiscardInBuffer()
   at Modbus.IO.ModbusSerialTransport.Write(IModbusMessage message)
   at Modbus.IO.ModbusTransport.UnicastMessage[T](IModbusMessage message)
   at Modbus.Device.ModbusMaster.PerformReadRegisters(ReadHoldingInputRegistersRequest request)
   at Modbus.Device.ModbusMaster.ReadInputRegisters(Byte slaveAddress, UInt16 startAddress, UInt16 numberOfPoints)
   at driver.PgvaDriver.ReadData(Int32 register) in C:\Users\PgvaDriver.cs:line 157

How do I succesfully close the port when necessary, and then re-open the serial port?

Thanks!

  • Looks like a very basic bug, don't keep reading after you closed the port. Not closing at all is a very acceptable workaround btw, the finalizer and (last-gasp) the OS take care of it when you terminate the app. – Hans Passant Mar 21 '23 at 13:10

0 Answers0