Announcement

Collapse
No announcement yet.

Problem with sample code

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

  • Problem with sample code

    I have a serious problem with the sample code in SPE_alpha01.4dg on my uOLED32028-P1
    In my project I use the following funktion to read a byte from serial interface.

    Code:
     func SC_GetByte()var ch;while((ch := serin()) < 0);return ch;endfuncfunc SC_GetStr(var inStr)var ch, i;i:=0;repeatch := SC_GetByte();if (ch != 0)inStr[i] := ch;elseinStr[i] := 0;break;endifch := SC_GetByte();if (ch != 0)inStr[i] := inStr[i] + ch << 8;endifi++;until (ch == 0);endfunc
    then I send the following command to the Display
    02 01 55 65 62 65 72 73 63 68 72 69 66 74 00 57 65 72 74 00
    The first 2 bytes will be read as bytes and then follow two #00 terminated strings.

    To read the command I use this code

    Code:
    var sInfo1[15];var sInfo2[15];func Draw()var type;var row;type:=SC_GetByte();row:=SC_GetByte();SC_GetStr(sInfo1);SC_GetStr(sInfo2);print([DEC] type,"\n");print([DEC] row,"\n");putstr(sInfo1);putstr(aInfo2);endfunc
    On the display this gives the following

    95
    8
    Ueberschrift
    Wert
    The strings will be decoded correctly so also the SC_GetByte should be work correct. But why do I get 95 08 instead of 02 01 for the first 2 bytes???


  • #2


    Um, I'm a bit rusty on 4DGL atm, but in the interests of giving you a potential answer quickly, I'd say using an array as a local variable is your problem.

    What happens if you move

    var sInfo1[15];
    var sInfo2[15];

    to make them global?
    Mark

    Comment


    • #3


      Sorry the code posted was an old one. The arrays now are global variables and are not the problem. My problem are the 2 bytes at the beginning of by command.
      The 02 becomes 95 and the 01 becomes 08.
      This is what I don't understand because the SC_GetByte() funtion is used within the SC_GetStr() funtion and there it works. In the ScrPutStr() function (same sample file) these both funtions are used and they work. This is very confunsing to me.

      Comment


      • #4


        On my uOLED-28028-P1T and your code modified as mentioned and a "setbaud(BAUD_9600);" added, works fine.

        What speed are you trying to communicate at? (and maybe, more importantly, where is the setbaud in your code?)
        Mark

        Comment


        • #5


          I communicate with 115200bps.
          I did some further tests and found out that I get the same results (95 ,08) when I send other bytes.
          Could it be a problem of the dimension of Com buffer?
          com_Init(combuf, 32, '_' );

          BTW I use a different compiler DLL.
          http://4d.websitetoolbox.com/post?id=4283329

          Comment


          • #6
            I've come up with the following code and I'm runnin it with the new compiler.... Seems to work fine..

            Of course I needed to add a '_' to the start of data packet.

            Is your 115200 baud perhaps not yet stable?

            #platform "uOLED-32024-P1"
            var sInfo1[15];
            var sInfo2[15];
            var mybuf[17];
            func SC_GetByte()
            var ch;
            while((ch := serin()) < 0);
            return ch;
            endfunc
            func SC_GetStr(var inStr)
            var ch, i;
            i:=0;
            repeat
            ch := SC_GetByte();
            if (ch != 0)
            inStr[i] := ch;
            else
            inStr[i] := 0;
            break;
            endif
            ch := SC_GetByte();
            if (ch != 0)
            inStr[i] := inStr[i] + ch << 8;
            endif
            i++;
            until (ch == 0);
            endfunc

            func main()
            var type;
            var row;
            com_Init(mybuf,32,'_') ;
            setbaud(BAUD_115200);
            type:=SC_GetByte();
            row:=SC_GetByte();
            SC_GetStr(sInfo1);
            SC_GetStr(sInfo2);
            print([DEC] type,"\n");
            print([DEC] row,"\n");
            putstr(sInfo1) ;
            print("\n") ;
            putstr(sInfo2);
            while(1) ;
            endfunc
            Mark

            Comment


            • #7


              Yes the '_' is the initial character for the data transfer. Before I used the SC_GetByte() function I had serin() in my project which worked fine. All the other commands work fine with serin(). By replacing serin() with SC_GetStr some command work and some not.   


              Is your 115200 baud perhaps not yet stable?
              I will check it this evening. Maybe the baud rate is too high.

              Comment


              • #8


                Problem solved!

                I had a duplicate command code in the contants definition and so an other function red the bytes from the com buffer but could not execute the subcommand. After that when the correct funtion was executed the buffer was empty and so I got the wrong byte values.

                Comment

                Working...
                X