Announcement

Collapse
No announcement yet.

Strange behaviour with serial

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

  • Strange behaviour with serial

    Hello all. I am having a small problem with my serial comms.

    Code:
    var com1buf[200];
    var tempBuff[50];
    .
    .
    .
    
    com_SetBaud(COM1, 1920);
    com1_Init(com1buf, 77, 0 );
    
    repeat
            if ( com1_Count()>=10)
                txt_Width(1);
                txt_Height(1);
                txt_MoveCursor(8,3);
                txt_Set(TEXT_COLOUR,RED);
                to(TEXT);
                print("          ");
                to(tempBuff);
                txt_Width(1);
                txt_Height(1);
                txt_MoveCursor(8,3);
                txt_Set(TEXT_COLOUR,RED);
                var n:=10;
                while(n--)
                    putch(serin1());     // print the 10 chars we received to tempBuff
                wend
                putch(0);
                to(TEXT);
                print([STR]tempBuff);
                com1_Init(com1buf, 77, 0 );
    
            endif
    .
    .
    .
    forever
    From another device I send "12345" twice. So in theory I should be getting 1234512345 printed on my screen. However what happens is that I get 234512345 and after a few milliseconds I get the lost 1 but at the end of the string. So on my screen I see:
    234512345
    few milliseconds later.....
    2345123451

    If I putch directly to the screen
    Code:
    to(TEXT);
    while(n--)
         putch(serin1());
    wend
    then everything works ok and I get all chars immediately. I know that it is something silly but my brain is stuck. Any help is welcome.

    Best regards,

    Dimitris

  • #2
    Have a look at your code, if it doesn't make sense try a adding a few prints here and there.

    If you still need a hint, remember that to() only works for the very next 'print' statement
    Mark

    Comment


    • #3
      Mark thanks a lot for the reply and for the hint. After putting a delay between the end of the while and the moment I print the buffer I realised that what I was seeing at the screen was "generated" inside the while. So yes to() works for the next print I had to do these changes:
      Code:
              if ( com1_Count()>=10)
                  txt_Width(1);
                  txt_Height(1);
                  txt_MoveCursor(8,3);
                  txt_Set(TEXT_COLOUR,RED);
                  to(TEXT);
                  print("          ");
                  txt_Width(1);
                  txt_Height(1);
                  txt_MoveCursor(8,3);
                  txt_Set(TEXT_COLOUR,RED);
                  var n:=10;
                  var char;
                  to(tempBuff);
                  while(n--)
                      char := serin1();
                      if (char >= 0)
                          putch(char);     // print the 10 chars we received to tempBuff
                          to(APPEND);
                      endif
                  wend
                  putch(0);
                  to(TEXT);print([STR]tempBuff);
                  com1_Init(com1buf, 77, 0 );
      
              endif
      Thanks again and thank you for not just "spitting out" a solution but rather letting me figure it out

      Comment


      • #4
        On a side note something that made my jaw drop... those few milliseconds delay I was talking about... they where because of the var declarations inside the if. Declaring the variables outside eliminated the delay.. and we are talking about 1400 milliseconds dropping down to 4!!!!

        Comment


        • #5
          Hmm, that doesn't sound right, the compiler actually moves all 'vars' to the top of the function. I'll have a look
          Mark

          Comment


          • #6
            I turned your first example into a simple program and I don'e see any delay between the 5 and the 1
            Mark

            Comment


            • #7
              Hello Mark. So I print the content of the array on line 8 on my screen. What I was seeing at my screen was... "clear" the line that I am about to write to
              txt_MoveCursor(8,3); print(" "); delay almost a sec then get chars from buffer and dump them on the array and then print the array... What I had between the moment the line gets "cleared" and printing on that space was the two declarations. Also I have a cycle counter 0->1000 inside my repeat that I print on my screen just to see when the program hangs. During that delay the counter froze. What ever was happening it was freezing the execution of the program. When I moved those declarations from that spot the delay disappeared and ofc the counter stopped freezing.

              Comment


              • #8
                Please post a program that I can run that demonstrates this. As I said the sample I made works as expected.
                Mark

                Comment


                • #9
                  Ok even more confused now. its not the declaration that adds the delay but not doing n := 10 right after var n:= 10;


                  1 sec delay:
                  Code:
                  func main()
                  
                  com_SetBaud(COM1, 1920);
                  com1_Init(com1buf, 77, 0 );
                  
                  repeat
                      if ( com1_Count()>=10)
                              var n:=10;
                              var char;
                              txt_Width(1);
                              txt_Height(1);
                              txt_MoveCursor(8,3);
                              txt_Set(TEXT_COLOUR,RED);
                              to(TEXT);
                              print("          ");
                              txt_Width(1);
                              txt_Height(1);
                              txt_MoveCursor(8,3);
                              txt_Set(TEXT_COLOUR,RED);
                  
                              to(tempBuff);
                              while(n--)
                                  char := serin1();
                                  if (char >= 0)
                                      putch(char);     // print the 10 chars we received to tempBuff
                                      to(APPEND);
                                  endif
                              wend
                              putch(0);
                              to(TEXT);print([STR]tempBuff);
                              com1_Init(com1buf, 77, 0 );
                               sys_SetTimer(TIMER3,600);
                  
                          endif
                  forever
                  endfunc

                  No delay
                  Code:
                  func main()
                  
                  com_SetBaud(COM1, 1920);
                  com1_Init(com1buf, 77, 0 );
                  
                  repeat
                      if ( com1_Count()>=10)
                              var n:=10;
                              n:=10;
                              var char;
                              txt_Width(1);
                              txt_Height(1);
                              txt_MoveCursor(8,3);
                              txt_Set(TEXT_COLOUR,RED);
                              to(TEXT);
                              print("          ");
                              txt_Width(1);
                              txt_Height(1);
                              txt_MoveCursor(8,3);
                              txt_Set(TEXT_COLOUR,RED);
                  
                              to(tempBuff);
                              while(n--)
                                  char := serin1();
                                  if (char >= 0)
                                      putch(char);     // print the 10 chars we received to tempBuff
                                      to(APPEND);
                                  endif
                              wend
                              putch(0);
                              to(TEXT);print([STR]tempBuff);
                              com1_Init(com1buf, 77, 0 );
                               sys_SetTimer(TIMER3,600);
                  
                          endif
                  forever
                  endfunc

                  Comment


                  • #10
                    Not sure what language you are used to, but in 4DGL

                    Code:
                                var n:=10;
                    is only executed once, so the second time through n will be 0 which will cause the while loop to loop 65636 times causing the symptoms you are observing.
                    Mark

                    Comment


                    • #11
                      Local static variable, one time init, persists through each cycle

                      Comment


                      • #12
                        Ok it makes sense now. Thanks a lot guys for the replies.

                        Comment

                        Working...
                        X