开发者

How to handle packet send/ack

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开发者_StackOverflow社区 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜