Seem to receive double events

Hi All,

I’m writing some software in vb2005 and I’m progressing well until I discovered that it seems that I receive some events as duplicate. I created a stripped down version with a form , one button and a file where the received packets are written to. The button sends only the module name request for address 2. I’m using the (unaltered) velbus dll. When the form loads it connect succesfully to the bus

This is the code :

Imports Velleman.Velbus

Public Class Form1
Private VMB1USB As New SerialBus()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    VMB1USB.PortName = "COM8"
    VMB1USB.Open()
End Sub

Public Sub New()
    InitializeComponent()
    AddHandler VMB1USB.PacketReceived, AddressOf VelbusPacketReceived
End Sub

Private Sub VelbusPacketReceived(ByVal source As Object, ByVal args As BusPacketIoEventArgs)
        Print(2, ([String].Format("{0} {1}", args.Packet.Address, args.Packet.Size)) & Environment.NewLine)
        FileClose(2)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim packet As New Packet()
    packet.Address = CByte(Microsoft.VisualBasic.Hex(&H2))
    Packet.Priority = PacketPriority.Low
    Packet.DataSize = 2
    Packet.Command = CByte(&HEF)
    packet.Item(1) = CByte(Microsoft.VisualBasic.Hex(&H4))
    If VMB1USB.IsOpen = True Then VMB1USB.SendBlocking(Packet)
End Sub

End Class

if I press the button I expect 3 packets with size 14 14 12 but occasionally (not all the time) I receive 14 14 12 12
So the last command is replicated.

I can’t figure out if it acually comes from the bus or that I need to clean the receive buffer? (I haven’t seen such object though)

Anyone some ideas?

Thanks Jeff

This is probably a multithreading issue, keep in mind that the receiver is a worker thread, so the PacketReceived event is also called in a different thread. Try using synchronization or thread-safe methods.