Announcement

Collapse
No announcement yet.

Can't extract numerical values from ascii string.

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

  • Can't extract numerical values from ascii string.

    I am new to the 4DGL displays and am having a problem saving data from a text string to a variable. Here is the code;
    Code:
    #platform "uLCD-32PT_GFX2"var i, p, n;var cougRXbuf[33];  // Data string format from controller//"TR=xxx CR=xxx CF=xxx PW=xxx HS=xxxx RT=xxxx FB=xx BA=xxx AH=xxx.x\r\n"var coug_rtd[9];    // Data from controller// [0] TR 'throttle'// [1] CR 'current reference'// [2] CF 'current feedback'// [3] PW 'PWM'// [4] HS 'heatsink'// [5] RT 'raw throttle'// [6] FB 'fault bits'// [7] BA 'battery amps'// [8] AH 'amp hours' note: the 1/10 value is ignoredfunc main()com_SetBaud(COM0,1920);                         // Cougar comms is 19200 8-n-1gfx_Cls();gfx_Set(SCREEN_MODE,LANDSCAPE);txt_Set(FONT_SIZE, FONT1);com_Init(cougRXbuf,63,'T');                           // set up a interrupt driven ring buffer for commsp:= str_Ptr(cougRXbuf);repeatwhile(!com_Full()) continue;                        // wait till com buffer fillsi:=0;while(str_GetW(&p, &coug_rtd[i++]) != 0);     // parse cougarRXbuf and store values to coug_rtdi := 0 ;gfx_Cls();print([STR] cougRXbuf, "\n\n"); // print buffer as diagnostic outputprint("Throttle is   ", coug_rtd[i++], "\n");print("Current Ref   ", coug_rtd[i++], "\n");print("Current Fdbk  ", coug_rtd[i++], "\n");print("PWM           ", coug_rtd[i++], "\n");print("Raw Heatsink  ", coug_rtd[i++], "\n");print("Raw Throttle  ", coug_rtd[i++], "\n");print("Fault bits    ", coug_rtd[i++], "\n");print("Battery Amps  ", coug_rtd[i++], "\n");print("Amp Hours     ", coug_rtd[i], "\n");com_Init(cougRXbuf,63,'T');                // reset RX bufferforeverendfunc
    What happens is that I receive and print cougRXbuf fine but the coug_rtd is always 0. I am not really understanding pointers and I think that is where my problem is. Any help or suggestions greatly appreciated. Thanks in advance, Greg

  • #2


    Its going to fail on the T - received from the TR=xxx in the GetW as that is not a number and will return a 0

    Comment


    • #3
      Have a look at this - this works if I load an example into a buffer

      Code:
      #platform "uLCD-32PT_GFX2"var i, p, n;var buffer[33];  // Data string format from controller//"TR=xxx CR=xxx CF=xxx PW=xxx HS=xxxx RT=xxxx FB=xx BA=xxx AH=xxx.x\r\n"var coug_rtd[9];    // Data from controller// [0] TR 'throttle'// [1] CR 'current reference'// [2] CF 'current feedback'// [3] PW 'PWM'// [4] HS 'heatsink'// [5] RT 'raw throttle'// [6] FB 'fault bits'// [7] BA 'battery amps'// [8] AH 'amp hours' note: the 1/10 value is ignoredfunc main()//com_SetBaud(COM0,1920);                         // Cougar comms is 19200 8-n-1gfx_Cls();gfx_Set(SCREEN_MODE,LANDSCAPE);txt_Set(FONT_SIZE, FONT1);//com_Init(cougRXbuf,63,'T');                           // set up a interrupt driven ring buffer for commsto(buffer) ; print ("TR=123 CR=456 CF=789 PW=123 HS=4567 RT=8901 FB=23 BA=456 AH=789");p:= str_Ptr(buffer);repeat//while(!com_Full()) continue;                        // wait till com buffer fillsi:=0;p := p + 3;while(str_GetW(&p, &coug_rtd[i]) != 0);     // parse cougarRXbuf and store values to coug_rtdp := p + 2;gfx_Cls();//str_Printf (&p,"%s\n\n"); // print buffer as diagnostic outputstr_GetW(&p, &coug_rtd[i]);p := p + 2;print("Throttle is   ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]);p := p + 4;print("Current Ref   ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]) ;p := p + 4;print("Current Fdbk  ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]);p := p + 4;print("PWM           ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]) ;p := p + 4;print("Raw Heatsink  ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]) ;p := p + 4;print("Raw Throttle  ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]) ;p := p + 4;print("Fault bits    ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]) ;p := p + 4;print("Battery Amps  ", coug_rtd[i], "\n");str_GetW(&p, &coug_rtd[i]) ;p := p + 4;print("Amp Hours     ", coug_rtd[i], "\n");//com_Init(cougRXbuf,63,'T');                // reset RX buffer//endifrepeatforeverforeverendfunc

      Comment


      • #4


        kaddrison, thanks for the code example. One thing I don't understand is this line.

        while(str_GetW(&p, &coug_rtd[i]) !=0)

        When I try to compile it I get these errors.

        Error: 'amp' not found (line 27 file:NoName2.4DG)
        Error: context error for unknown 'amp' (line 27 file:NoName2.4DG)
        Error: invalid context for & operator (line 27 file:NoName2.4DG)
        Error: expected ')' but got ';' (line 27 file:NoName2.4DG)
        Error: function 'str_GetW' requires 2 arguments, received 1 (line 27 file:NoName2.4DG)

        The syntax doesn't seem correct.

        Greg

        Comment


        • #5


          & - turns to &amp when you post code - change &amp to & and all should be good

          Comment


          • #6


            Thanks kaddrison, I have downloaded and tested the code with my data stream and it is now capturing the first string fine, but doesn't capture subsequent data. I think I can sort that out though, but maybe tomorrow, I'm off to bed now.

            Comment


            • #7


              I'm making progress with the code, but can't seem to work out a couple of bugs.

              Bug 1. Won't capture data to coug_rtd[8], the amp hour data.

              Bug 2. Except for the first piece of the data, the variables lag behind the actual data stream, storing the previous data instead of the current data. This is very strange because coug_rtd[0] is current and the rest are behind, showing data from the previous data capture.

              I have attached the program along with an Ardunio sketch that I am using to generate a data stream for testing purposes. Any help appreciated.

              Greg

              Attached files cougardExp4.4dg (3 KB) CougarSimulator.txt (2.1 KB)

              Comment


              • #8


                Couple of things

                You are adding 1 to I before you are printing it - so not getting anything the first time.

                and you are not adding 4 to P between throttle / current ref

                Attached should work.
                Attached files cougardExp4.4dg (3.2 KB)

                Comment


                • #9


                  Thanks again kaddrison. That is now working as expected, onto making those numbers into a fancy display. Greg.

                  Comment


                  • #10


                    No problem - what is it for ? - and don't forget to post the end result

                    Karl

                    Comment


                    • #11


                      The project is a display for a electric car motor controller. Link to the controller is here. I am keeping latest versions of the code here.

                      Comment


                      • #12


                        If its a critical thing - can I suggest you do a little check to make sure you have the correct data - if for any reason you miss 1 byte of data - you will continue to receive the next 64 bytes so - always out. What I would suggest is increase the buffer to get the CR LF also and check you have TR at the start and the CR LF at the end.

                        Comment


                        • #13


                          Yes I was planning on doing some error checking. The idea I had was to say check the string in 3 places for the correct format, for example "TR=" is the first item, "CF=" is in position 15 and "RT=" is in position 37, or whatever the values work out to be. Roll the whole routine into a function that can return an error value if the data stream was corrupted. Unfortunately work has rolled around again, so could be some time before I get back to this.

                          Comment


                          • #14


                            your problem is getting back in sync.

                            May I suggest checking the TR and Carriage return at the end. If they are not there set a serial buffer with a qualifier of the CR for 1 - which should get the LF. Then you should be good to go back to the full buffer

                            Comment


                            • #15


                              It actually self syncs as it is. I have tested this by randomly connecting the Arduino (my controller simulator) to the display. I can get one data stream with garbage, but after that, it is fine. Any errors get corrected on the next data stream, just need to check for errors and ignore the data and wait for the next lot if there are any. Being a display, the data rate can be set quite low, once or twice per second is all that is required.

                              Comment

                              Working...
                              X