I think it unlikely that the problem you have is related to the UART buffer being off or not working properly. You are holding the serial port open so the serial port driver will certainly receive all the data that comes in for you, whether or not your process is scheduled.

The problem might be an unwanted character translation as others have pointed out. Another possibility is that there is something else also listening to the serial port and it is absorbing a character once in a while. This could be a getty or some other process watching for something to happen on a serial port. Some program might also be running that expects to talk to some kind of transparent serial port dongle like an X10 Firecracker that really isn't so transparent after all.

Since it sounds like you have a magic sequence at the start of every received message (0x55 0xaa), you can use this to synchronise to the start of a message and recover from the situation where you have fallen out of sync due to a lost character. The details of doing this depend on whether or not the protocol is designed so that the magic sequence cannot possible occur in the body of a message (maybe it would be quoted if it does appear). To do this, you should read characters one at a time until you find a 0x55 followed by a 0xaa, use this to detect that you have come to the start of a new message, and then read the rest of the header in bulk (and then the body in bulk).

In order to solve your problem, you will probably want to dump everything you read from the serial port into a debugging log. When you get a corrupted message, analyze the PREVIOUS message in the log to see how it ended. You might find that the end of the previous message contained the start of the current one, which would be wrong and would indicate the size of the previous message was one byte off.


In reply to Re: Losing bytes with Device::SerialPort ? by Celada
in thread Losing bytes with Device::SerialPort ? by Anonymous Monk

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.