It seems I can use 2 ways below to treat it:

Looking at the protocol, (which is pretty badly designed IMO), you're going to have to use a mixture of both methods.

  1. First you need to read 1 byte to determine if it is a fixed (0x10) or variable (0x68) length packet.
  2. If the former, you can then read the next 5 bytes, check the checksum and unpack the payload.
  3. If the latter, then you need to read the next two bytes to obtain the length.
  4. Then you add 3 to that. (To account for the second start byte, the checksum and stop bytes.)
  5. Then you can read the rest of the packet, check the sum and unpack the payload.
  6. And finally ack (0xe6) or nack (0xa2) the packet.

Don't forget to binmode your socket and use sysread. I've found that wrapping sysread in a sub that checks the return code and returns the result for assignment makes life much easier.

That still leaves the problem of timeouts. There are various ways of tackling that--alarm, ioctl, select etc. Which is appropriate will depend a lot on you platform. That said, tcpip comms is pretty reliable especially if LAN connected. It may be a sufficiently infrequent occurrence that you can essentially ignore it. That will depend upon your circumstances and network.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^9: deal data transfer by BrowserUk
in thread deal data transfer by xiaoyafeng

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.