How to handle packet send/ack
Asked Answered
H

1

17

I'm building an application to communicate to an Xbee module via the Xbee API.

Currently I have something working, but its fairly simple and has quite a few limitations.

Sub processPackets() ' this runs as its own thread
'remove data from serial buffer and format in to packet
'if IO response generated remotely, not requested put in IOQueue
'Otherwise put in CMDQueue (response generate from request, ie cmd response or packet Ack
End Sub

Then as an example of a typical command request Send data to serial port Loop (with timeout) checking CMDQueue for packet, dequeue and check if it matches Otherwise timeout

Now its pretty obvious the potential issues with this method. In particular, since the Xbee modules can sleep you may have to wait a considerably long time for an Ack. Plus it is dependent on order, etc.

I would like to take a non-blocking approach. In this case, in order to act on the the Ack/response packet in most cases I need to know the original packet it was sent in response to.

I'm thinking of creating a few threads. SendPacket will send the packet, load the sent packet, time sent, and timeout in to memory, also include callback function? (array?) PacketProc will parse packets, check the array of packets waiting for response and call the callback function. It will also check for any waiting packets that have timed out and call the callback to indicate timeout?

Ultimately I'm looking for the ability to send packets to multiple devices (may response in any order) and act on those responses or act on the timeout.

I'm not particularly familiar with .NET can anyone comment on this approach or recommend a better pattern to look in to? Any .Net methods I should look in to?

Hershey answered 2/10, 2011 at 2:25 Comment(5)
If you are limited to serial communication, you will not be able to run multiple threads successfully to one instrument. You can run one thread per instrument, if I am understanding what you want to do.Chlorohydrin
I have a project that is using a special emulator I created with microsoft's dsf(device simulation framework). If you want to use it to emulate a device or test a custom one of your own it can accept customized devices. If you need something better tell me but with dsf anyone can simulate or test just about anything. You will need an out endpoint in addition to the in endpoint to have two way communication (wdk guys would be able to show you how to do this). This works on a lower level (unmanaged/kernel code from ms involved) so you can customize the packet before its sent.Spanishamerican
sorry heres the link: kinectmultipoint.codeplex.com. Note: in order to edit the device simulator you need to edit softhidreceiver which is in the wdk under WinDDK\7600.16385.1\src\test\dsf\usb\GenericHID. It will have a function or sub called CreateReportDescriptor which allows you to input your own device. Their are vb.net and c++ versions to suit your flavor.Spanishamerican
The dsf runtime and softhidreceiver are both in the wdk(windows driver kit) 7.1 but are compatible with windows 8 with a little work from my research.Spanishamerican
In c++ for arduino, my HonOBDapt has some good examples of a non blocking serial I/O pattern. (sourceforge.net/projects/honobdapt/files/?source=navbar)Balas
I
1

Use the Task class.

Imports System.Threading
Imports System.Threading.Tasks

...
Dim buffer As StringBuilder;
Sub processPackets() ' this runs as its own thread
    ' Wait for packet
    ' put here command/loop that waits packet

    buffer.Append(packet);
    'remove data from serial buffer and format in to packet
    'if IO response generated remotely, not requested put in IOQueue
    If buffer.ToString() = "REMOTELY" Then
        ' Put IOQueuo
        buffer.Clear()
    Else
        'Otherwise put in CMDQueue (response generate from request, ie cmd response or packet Ack
        ' Put in CMDQueue 
        buffer.Clear()
    End If
End Sub

...

' Create and execute the Task that will process the packets
 Dim t = Task.Factory.StartNew(Sub() processPackets())

http://www.dotnetcurry.com/ShowArticle.aspx?ID=491

Idolla answered 17/9, 2013 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.