Click here to Skip to main content
16,020,677 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All

I need Develop one Program It Contain More Than 100 Of variable. The are changing with different function.

I need to send this variable through the serial Port Only if it is changed Without Defining new more variable for each.


I tried defined New variable for each and stored the value. Then new value stored in original variable if it is different i execute the function. Below I just show the example as I did.



This all function Execute by a timer in vb.net

Thanks

Regards
Nayana

What I have tried:

VB
Public Class Form1

    Dim newDistance As Integer
    Dim Distance As Integer

    Private Sub functionX()

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If newDistance <> Distance Then
            functionX()
            newDistance = Distance
        End If
    End Sub

End Class
Posted
Updated 27-Feb-16 8:33am
v2

1 solution

Worst thing you are doing is handling the timer ticks. How do you think things are going to be synchronized with those ticks. I don't even what to discuss this logic which I don't understand; I just face a number of developers with such "timer thinking" which I would rather call "anti-thinking". You don't have to explain your rationale behind that, but if you can, it would be curious to hear. You don't really need to "time" or synchronize anything. You should just unconditionally send the data. More interestingly, the receiving side should do the same. But as the streams are generally lock each other, it would be important to do all the communications on both sides in separate threads, or, in some cases, some couple of threads dedicated to serial communications on each side.

You just need to use the class System.IO.Ports.SerialPort:
SerialPort Class (System.IO.Ports)[^].

Now, the second aspect of it: you should not just send what you call "variables"; such approach would be totally non-structured, would take you anywhere. You should create some data structure which you can serialize before you send it and deserialize before you receive. This is a separate topic called serialization:
Serialization — Wikipedia, the free encyclopedia[^],
https://msdn.microsoft.com/en-us/library/ms233843.aspx[^],
System.Runtime.Serialization Namespace[^],
Binary Serialization[^],
Using Data Contracts[^],
DataContractSerializer Class (System.Runtime.Serialization)[^],[^],
DataContractJsonSerializer Class (System.Runtime.Serialization.Json)[^].

—SA
 
Share this answer
 
Comments
Member 12278335 28-Feb-16 5:13am    
Yep. I got things of you explain. Thank you very much for your Replay.

But That variable only can update With timer tick only Because of so many reason. Is there any method to compare before update new value for the variable This is for Serial Write only .
Sergey Alexandrovich Kryukov 29-Feb-16 11:10am    
You did not bother to explai those reasons. Compare? ==? =>? <=? !=?... :-)
—SA

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