1 2 Previous Next 27 Replies Latest reply on Nov 17, 2012 7:37 AM by userc_40557

    USBUART_GetAll

       Hi, 

         

      I want to control DAC Output through USBUART but have problems with getting data from USB.

         

      I assume the function USBUART_GetAll () should get the whole null terminated string? It doesn't in my project. When I send 255 over serial line, the function returns 2 and then next time 55 or 25 and 55. I might have made mistake as this is the first time I am using USBUART.

         

      And the next problem  - atoi returns something odd. Even if I pass '2', it returns something but not 2. It must be something wrongh with my code:

         

      /* check the icoming data through usbuart */
      if (USBUART_1_DataIsReady())
      {
      /* read incoming message */
      USBUART_1_GetAll(rdBuffer);

      PrintToUSBUART("\r\n");
      PrintToUSBUART((char8 *)rdBuffer);
      PrintToUSBUART("\r\n");

      /* convert ascii to int */
      Wave_Value = (uint8)atoi(rdBuffer);

      /* Set the value in VDAC_2 data register */
      VDAC8_2_SetValue(Wave_Value);

      sprintf((char *)wrBuffer, "%u\r\n", Wave_Value);
      PrintToUSBUART((char8 *)wrBuffer);
      }

         

      Thank you,

         

      Oleg

        • 1. Re: USBUART_GetAll

          what I have found so far that  USBUART_1_GetAll(rdBuffer) doesn't receive the whole string. It receives chars separately. So it's working like USB U AR T_GetChar() then.

          • 2. Re: USBUART_GetAll

            USBUART_GetAll() returns you the retrieved data. So when you get only a single byte this just means that the PSoC hasn't received more at this moment. And since you call it as soon as _DataIsReady() return yes, this situation is rather likely.

               

            This also depends on how the sender is transmitting - if each character is send in its own USB packet, this also will increase the likelihood of this. If, on the pother hand, the sender sends the whole string as one packet, it should never occur.

               

            Note that _GetAll() doesn't return a null-terminated string, which might explain why atoi doesn't return what it should.

            • 3. Re: USBUART_GetAll

              thank you very much,

                 

              that is clear now. I find the functions are not described well in the reference document. At least for me.

                 

              What is then suggested method to get the message? Using getchar and checking the terminator probably? 

                 

              Oleg

              • 4. Re: USBUART_GetAll

                If you cannot ensure that the senders sends the whole message in one go, you need to handle single characters. Just use _GetChar() and construct the message, until you reach the separator. Just remember - you are basically doing serial communication!

                   

                I think the _GetAll() is just there because USBUART is just a special version of the generic USB component, where it makes more sense. The UART mode is just character-based, there is no notion of a 'message' there.

                • 5. Re: USBUART_GetAll
                  JoMe_264151

                  The speed of UART transfer is relatively slow compared to the speed of the processor, so you will need something to decide if the message you expect is already complete or if you have to wait for some more characters.

                     

                   

                     

                  There are some approaches to ensure that.

                     

                  1st. Messages of fixed length.

                     

                  2nd. The message itself contains information about its length.

                     

                  3rd. A "finish"-character, usually something like Newline (0x0a) Carriage Return (0x0d) FormFeed(0x1a) or Escape (0x1b). All these characters are taken from the ASCII-set of "Non-Printable" chars, which start with the "Space" (0x20)

                     

                  Of course the sender of the message has to comply to one of the above rules.

                     

                   

                     

                  Bob

                  • 6. Re: USBUART_GetAll
                    DaKn_263916

                    I vote for Bob's third approach, as it is self healing, eg. if a message

                       

                    becomes corrupted, the 3'rd approach acts as a message framing byte.

                       

                    Of course one can always consider applying an ECC algorithim to the

                       

                    message but that takes BW and limits max messaging speed.

                       

                     

                       

                    Regards, Dana.

                    • 7. Re: USBUART_GetAll
                      DaKn_263916

                      Some basic approachs to managing traffic -

                         

                       

                         

                      http://en.wikibooks.org/wiki/Serial_Programming/Error_Correction_Methods

                         

                       

                         

                      Regards, Dana.

                      • 8. Re: USBUART_GetAll

                        But when the message becomes corrupt, the frame delimit might be the one getting lost :)

                           

                        But USB already has its own transport-level protocol, ensuring that nothing gets lost. So when using USBUART, there should be no need to add another protocol on top of that (provided that you trust the other side to send its data correctly).

                        • 9. Re: USBUART_GetAll

                          Thank you so much for such a great attention to my little problem.

                             

                          I'll stay with getchar approach and \r\n termination as it's something I've used before. Normaly psoc's APIs worked well for me even if I didn't understand what they did. But not this time.

                             

                           

                             

                          I've just checked the simple echo back and it still doesn't work:

                             

                          if (USBUART_1_DataIsReady())
                          {
                          uint8 temp;

                          /* get the char into the buffer */
                          temp = USBUART_1_GetChar();

                          /* echo the char back */
                          USBUART_1_PutChar(temp);
                          }

                             

                          this perfectly  works  (nearly - i think it should send chars back in column once I heat enter but it reports them in a row)  with a single char sent from terminal but once it goes to 3 chars, the psoc strarts loosing chars and then sends rubbish back. Is something wrong with my code again?

                             

                           

                             

                          thanks

                          • 10. Re: USBUART_GetAll
                            DaKn_263916

                            Correct me if I am wrong, the only perfect transport protocol would take an infinite

                               

                            amount of time, hence there will never be perfect packet transport. So framing

                               

                            objects, retransmission, all fall under Shannon limits.

                               

                             

                               

                            Regards, Dana.

                            • 11. Re: USBUART_GetAll

                              I wonder if someone used usbuart to send data to chip. I'd love to have a look on implementation. The exapmple project from cypress sends only a single char to the chip and that works well. but once there are more chars in a message the  USBUART_1_DataIsReady() seems do not work correctly

                              • 12. Re: USBUART_GetAll
                                EvPa_264126

                                You can see my old project (comments in Russian language).
                                http://mylab.wmsite.ru/ftpgetfile.php?id=59
                                Project is made for PSoC Creator 1.0 and can be found here.
                                http://mylab.wmsite.ru/moi-razrab/kardiograf
                                I hope this will help a bit

                                • 13. Re: USBUART_GetAll

                                  thank you very much for sharing the project. Good ideas but the data in the project are sent from the chip to computer only which also works well in my design.

                                     

                                   

                                     

                                  I have troubles with receiving string sent from pc terminal software to psoc chip. None of the methods I've used gives good results. Once the string is longer then 2 chars, psoc looses the rest 

                                  • 14. Re: USBUART_GetAll
                                    EvPa_264126

                                    I asked for help with USBUART to the technical support. Dan Sweet <drsw@cypress.com> very quickly helped to correct my project

                                    1 2 Previous Next