I am hoping someone might have done something similar, but in my plugin I rely almost exclusively on serial communications. I had handled this within the plugin through a IO.Serial where I have a Public WithEvents MySerialPort as SerialPort = New System.IO.Ports.SerialPorts
I then have a handler Private Sub MySerialPort_DataReceived(ByVal sender as Object, ByVal e as System.IO.Ports.SerialDataReceivedEventArgs) handles MySerialPort.DataRecieved.
I have added one connection event action which allows an event to Connect / Disconnect / Reconnect the serial plugin.
So in the actual event action code, I do a simple select case to work out what the serialized action is, then I wanted to issue the commands.
For example
Case "Connect"
Main.ComOpenPort() 'Function to open the serial port
Case "Disconnect"
Main.MySerialPort.Close()
Case "Reconnect"
Main.MySerialPort.Close()
Main.ComOpenPort()
So with this code, if I close the port I get an error in the handler as it has threaded and I have then closed the serial port, however If i remove the handler then close the serial port it works fine. The issue is then being able to connect / reconnect to the port with the missing handler.
How are others working with serial ports, is there a best practice way to be able to close and open the ports when there is a handler event sub associated and add / delete handlers each time, or is that overkill and there is a better way to go?
Thanks!
I then have a handler Private Sub MySerialPort_DataReceived(ByVal sender as Object, ByVal e as System.IO.Ports.SerialDataReceivedEventArgs) handles MySerialPort.DataRecieved.
I have added one connection event action which allows an event to Connect / Disconnect / Reconnect the serial plugin.
So in the actual event action code, I do a simple select case to work out what the serialized action is, then I wanted to issue the commands.
For example
Case "Connect"
Main.ComOpenPort() 'Function to open the serial port
Case "Disconnect"
Main.MySerialPort.Close()
Case "Reconnect"
Main.MySerialPort.Close()
Main.ComOpenPort()
So with this code, if I close the port I get an error in the handler as it has threaded and I have then closed the serial port, however If i remove the handler then close the serial port it works fine. The issue is then being able to connect / reconnect to the port with the missing handler.
How are others working with serial ports, is there a best practice way to be able to close and open the ports when there is a handler event sub associated and add / delete handlers each time, or is that overkill and there is a better way to go?
Thanks!
Comment