Announcement

Collapse
No announcement yet.

Errors at reading data from comPort

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Errors at reading data from comPort


    Hi,

    I'm building an application in which an uLCD-43PCT display is connected to a MCU via com Port for a halfduplex communication. The protocol is very basic (there are instructions structured in 2 bytes) but there is an exception when MCU replies an 18 bytes (characters) string to uLCD.

    Sending data from uLCD-43PCT to MCU works fine, as expected. Receiving data also seem to work fine except receiving the 18 bytes string situation.

    What's happen:

    The string sent from MCU to uLCD is something like that: LD10011010011010M1 - 18 chr in total;
    Following this string are received another 3 strings, each with 2 characters, let's say R0, D1, C5;
    Then again it's received the initial string - LD10011010011010M1 (identical one);
    Again are received another 3 strings, each with 2 characters;
    And so on...

    Every one of the 2 chr. strings are received and displayed correctly.
    The 18 chr. string is displayed correctly at first reception, in the next 4 occurrences the characters are mixed-up (shifted, repeated and mixed somehow - I couldn't find the rule) and then, the fifth time it's displayed right again.

    I read most of the related topics but I could not find the problem. I need your help to solve this issue. It's odd because I use the same "technology" for receiving both kind of strings (with 2 or 18 characters) and more than that if I receive this string chopped in 2 bytes slices works fine.


    Below are the relevant parts of commented testing code:

    /*++++++++++++++++++++++++++++++++
    General variables definition
    +++++++++++++++++++++++++++++++++*/
    var Rxbuffer[10]; //Receiving buffer (20 chr)
    var Txbuffer[1]; //Tx buffer (2 chr)

    /*++++++++++++++++++++++++++++++++
    Serial port intialization
    +++++++++++++++++++++++++++++++++*/
    func InitCom1()
    com_SetBaud(COM1,960); //Com1=9600 baud
    com1_Init(Rxbuffer,20,0); //Init. receiving buffer -20 bytes
    endfunc

    /*++++++++++++++++++++++++++++++++
    Transmit function - only 2 bytes
    +++++++++++++++++++++++++++++++++*/

    //Using:
    // Txbuffer[0]:='K0'; //some intruction
    // SendData();

    func SendData()
    var i,ptr,chr;
    for (i:=0;i<2;i++)
    ptr:= str_Ptr(Txbuffer);
    chr:= str_GetByte(ptr+i);
    serout1(chr);
    next
    endfunc

    /*++++++++++++++++++++++++++++++++
    Receive function - placed in repeat/forever loop
    +++++++++++++++++++++++++++++++++*/
    func ReceiveComData()
    var ch,chrNo; //local variables
    var ptr,ptrSrs,ptrDst;
    var Prtbuffer[10]; //print buffer (just to see result and check str_ByteMove)
    //********************************
    if (com1_Count()>=2) //at least 2 chr in Com1 Rx buffer
    ptr:=str_Ptr(Rxbuffer); //rise pointer for Rx buffer
    ch:= serin1();
    str_PutByte(ptr,ch); //load first chr into Rxbuffer
    ptr++;
    chrNo++;
    if (ch=='L') //if first chr = L - only received string with 18 chr
    while (chrNo < 18) //wait to load all 18 characters
    while (com1_Count())
    ch:= serin1();
    str_PutByte(ptr,ch);
    ptr++;
    chrNo++;
    wend
    wend
    ptrSrs:= str_Ptr(Rxbuffer); //the following instructions are just to check str_ByteMove
    ptrDst:= str_Ptr(Prtbuffer);
    str_ByteMove(ptrSrs,ptrDst,18);
    print ("ChNo= ",chrNo," ",[STR]Prtbuffer,"\n"); //print result (characters No and received data)
    else
    while (chrNo < 2) //load 2 characters
    while (com1_Count())
    ch:= serin1();
    str_PutByte(ptr,ch);
    ptr++;
    chrNo++;
    wend
    wend
    ptrSrs:= str_Ptr(Rxbuffer); //the following instructions are just to check str_ByteMove
    ptrDst:= str_Ptr(Prtbuffer);
    str_ByteMove(ptrSrs,ptrDst,2);
    print ("ChNo= ",chrNo," ",[STR]Prtbuffer,"\n");
    endif
    endif
    _______________________
    Best regards,
    Chris






  • #2


    When you say
    com1_Init(Rxbuffer,20,0); //Init. receiving buffer -20 bytes
    You are telling the system to use Rxbuffer to buffer up to 20 bytes of serial data. The system manages it, you never need, or even should, refer to Rxbuffer again.

    com1_Count() tells you how many unread characters are in Rxbuffer

    serin1() reads from and manages the buffer (when in buffered mode)

    The transmit buffer works in a similar way.

    So when looking at your code I see the Rxbuffer being written to and thus I imagine what you read from it to be rather jumbled at times, like what you are seeing.
    Mark

    Comment


    • #3


      Thanks for your fast replay!
      All you said it make sense. I'll use a different buffer for downloading data.

      Best regards,
      Chris

      Comment

      Working...
      X