0
votes

Using VS2012 (.NET) I am developing a ribbon bar style add-in for PowerPoint (2010) where I want the incoming serial port values to be passed through an algorithm and, depending on the output, perform an action such as next slide or previous slide. I am using the SerialPort.DataReceived event handler.

My problem is simpler explained using an iterating variable j which increases in value by 1 each time the SerialPort.DataReceived event handler is called. I will detail the problem more after the code but, in short, once my code is called j increases in value as expected prior to a slideshow presentation being started and for the first 30 seconds or so of the slideshow presentation. After about 30 seconds of being in presentation mode, j stops iterating. I am watching the value of j using Breakpoint When Hit and the debugging Output window.

In one class (class2), associated with a form launched by clicking a ribbon button, a button click in said form sets the SerialPort.DataReceived handler and opens the serial port (abbreviated relevant code):

Public Class class2 'called by clicking ribbon button
    Public sp As New SerialPort
    Public Baud As Integer = 9600
    Public Port As String
    Public c1 As class1

    Public Sub New(oParent As class1) 'get reference to parent class
        c1= oParent
        InitializeComponent()
    End Sub
    ....

    Public Sub btn1_Click(sender As Object, e As EventArgs) _
        Handles btn1.Click
        Port = lbCom.SelectedItem 'Port selected from form listbox
        Try
            With sp
                .BaudRate = Baud
                .PortName = Port
                .ReadTimeout = 5000   'Give serial port 5sto open
                .RtsEnable = True
            End With

            c1.mySP = sp
            AddHandler sp.DataReceived, AddressOf c1.spDataReceivedHandler
            sp.Open()
            Me.Hide()
        Catch ex As Exception
            MsgBox("Error", vbOKOnly, "Connection Error")
        End Try
    End Sub
End Class

In the main class (class1) I have the DataReceived Handler and iterating variable j (again, abbreviated relevant code):

Public Class class1
    ...
    Public Sub spDataReceivedHandler(ByVal sender As Object, _ 
        e As SerialDataReceivedEventArgs)
        j = j + 1 ' value monitored using Breakpoint When Hit
    End Sub
End Class

More details about the problem. Once I start my code, if I don't start a slideshow presentation j will iterate without any problem (5+minutes). If I start a slideshow presentation but immediately hit alt+tab to give focus to another application, say VS, j will iterate without any problem (with PowerPoint still being in presentation mode). If I start a slideshow presentation and let it have the focus j will stop iterating after ~30 seconds, regardless of how long I let j iterate prior to starting the slideshow presentation.

I have also tried using a background worker instead of serial port event handler where I use a do loop to get data from the serial port but I run into the same issue; works as expected until slideshow presentation has been running for ~30 seconds.

I have already written two separate Windows Form style applications using the same serial port parameters and same device which work fine. The issue, as far as I can tell, is with the slideshow presentation having the focus (and I'm guessing resource availability?).

I'm going to try my luck using the Task Parallel library, but if anyone has any insight as to why the aforementioned maddening problem exists do please enlighten me (Note: relatively novice programmer, so if there is a glaring error in my approach please let me know as well). Thanks.

1

1 Answers

0
votes

Okay, so no idea why the program was behaving that way but a work around was indeed to put the "open and monitor" serial port code into a sub in the ThisAddIn class and call it as a new task when appropriate (clicking a button).

Clicking a button on my custom ribbon:

Public Class myribbon    
    Private Sub btn_Click(sender As Object, e As RibbonControlEventArgs) _
        Handles btn.Click
        ' Define new task spTask (sub located in ThisAddIn)
        Dim spTask = New Task(Sub() Globals.ThisAddIn.readToSP())   
    End Sub
End Class

Public Class ThisAddIn
    ...' other stuff
    Dim mySP As New SerialPort
    Public Sub readToSP()
        ...' serial port params
        Try
            mySP.Open()
            While (mySP.IsOpen)
                Dim analogV As String = mySP.ReadTo(delimStr)
                ...' do something with analogV
            End While
        Catch ex as Exception 'appropriate catches...
        End Try
    End Sub
End Class