Announcement

Collapse
No announcement yet.

Adding Serial Connect and Disconnect Actions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Adding Serial Connect and Disconnect Actions

    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!
    HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

    Facebook | Twitter | Flickr | Google+ | Website | YouTube

    #2
    I should mention the ComOpenPort really only checks to see if the port is already open (if so exit sub), then if it is not open it defines the parameters (e.g. baudrate) then starts a number of timers and does MySerialPort.Open
    HS3 PRO, Win10, WeatherXML, HSTouch, Pushover, UltraGCIR, Heaps of Jon00 Plugins, Just sold and about to move so very slim system.

    Facebook | Twitter | Flickr | Google+ | Website | YouTube

    Comment


      #3
      Travis I need to improve my serial comms handling too. Especially if I lose connection while the plugin is running, how do I detect this, wait a while and try again and then ultimately give up.

      Did you make any headway on your issue of reconnecting and the datareceived handler?
      Author of Highpeak Plugins | SMS-Gateway Plugin | Blue Iris Plugin | Paradox (Beta) Plugin | Modbus Plugin | Yamaha Plugin

      Comment

      Working...
      X