Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Can someone help me with my problem?
I am a beginner only in programming.

I have 2 labels that count 1 every time I send a signal to my Arduino input.

Last time If I close the program it reset to 0.
I add the program that will save the count using File. Writealltext

It is working but the problem now, I cannot reset the count to 0.
If I update the label. text to = 0, it shows 0 but after I send the signal again to my Arduino again it returns to the previous number
Example:
my Ocount label count is 10
then I send the Ocount label to 0
but after I send signal to my Arduino again return to 11

What I have tried:

Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Dim receivedData As String = SerialPort1.ReadLine()

        If receivedData.Trim() = "A" Then
            signalCountO += 1
            UpdateSignalCountLabel()
        ElseIf receivedData.Trim() = "B" Then
            signalCountI += 1
            UpdateSignalCountLabel()
        End If

    End Sub

    Private Sub UpdateSignalCountLabel()
        If OCount.InvokeRequired Then
            OCount.Invoke(Sub() UpdateSignalCountLabel())
        Else
            OCount.Text = signalCountO.ToString()
        End If

        If ICount.InvokeRequired Then
            ICount.Invoke(Sub() UpdateSignalCountLabel())
        Else
            ICount.Text = signalCountI.ToString()
        End If

        ' Save the count value to a file
        File.WriteAllText(countFilePath, signalCountO.ToString())
        File.WriteAllText(countFilePath1, signalCountI.ToString())
        Form1.Chart1.Visible = True

    End Sub
Posted
Updated 6-Aug-23 21:49pm
Comments
Richard MacCutchan 7-Aug-23 3:45am    
Where is the code where you reset either count to zero? Also, this is Basic code not C++.

1 solution

Each time you call UpdateSignalCountLabel it updates the labels with the values from your internal variables, and saves them to individual files.
But unless you somewhere read those files and update the values of signalCountO and signalCount1 from them, or otherwise update the variables it will have no effect on your labels.

I'd suggest a change: use the settings file instead: open the Solution pane, open the project branch, then double click "Settings.settings".
In the resulting page, create two new lines named OCount and ICount, make them both int, and leave the scope as User. Add a Value of 0 for each.

Now in your code, you can load your internal variables in the Form.Load event:
VB
signalCountI = Properties.Settings.Default.ICount
signalCountO = Properties.Settings.Default.OCount
And save them in your Form.Closing event:
VB
Properties.Settings.Default.ICount = signalCountI
Properties.Settings.Default.OCount = signalCountO
Properties.Settings.Default.Save()
And the system will handle it all for you. All you need to do is update signalCountI and signalCountO when you want to count data in or out, or reset them to zero when you need to.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900