CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

USB CDC - Receive strings

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
soapfeldmann



Joined: 24 Jan 2011
Posts: 4

View user's profile Send private message

USB CDC - Receive strings
PostPosted: Sat Jul 02, 2011 8:23 pm     Reply with quote

Hello.
I'm trying to receive a string over the CDC on my PIC18F4550.
The problem is that there are only functions to get one char at a time.

I tried:
Code:

do
   {
      usb_task();
      if (usb_enumerated())
      {
         char* buff;
         int i = 0;
         while (usb_cdc_kbhit())
         {
             buff[i] = usb_cdc_getc();
             i++;
         }
         lcd_putc(buff);
      }
   } while (TRUE);


But i only get some strange letter just like omega or asterisks...
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jul 03, 2011 1:08 am     Reply with quote

CDC is a stream oriented protocol, receiving single characters is by nature. If you want to transmit strings, you have to think about control characters to separate them, e.g. carriage return.

The most simple method to display characters as they come in:
Code:
while (usb_cdc_kbhit())
  lcd_putc(usb_cdc_getc());


To form a valid C string, buff[i]=0; would be needed at the end. lcd_putc() is however a function to display single characters, not strings.
soapfeldmann



Joined: 24 Jan 2011
Posts: 4

View user's profile Send private message

PostPosted: Sun Jul 03, 2011 8:06 am     Reply with quote

Hello, and thanks for the reply.
Yes, I know that´s the best way, but after writing the incoming text on the LCD, i want to compare the received string to execute some command like powering up RB1, for example....

Even with the
Code:
buff[i]=0;
I cant get a valid string and there´s alwaays a strange char...
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Sun Jul 03, 2011 9:39 am     Reply with quote

How are you sending the 'strings'?.
Odds are that the 'strange character' is a line feed or carriage return.

Best Wishes
soapfeldmann



Joined: 24 Jan 2011
Posts: 4

View user's profile Send private message

PostPosted: Sun Jul 03, 2011 10:31 am     Reply with quote

I´m using a C# program that i´ve made to send the strings.
The problem is that the result char array from the loop contains only a strange char such like omega, beta, arrows, asterisks....

My bet is that the char ar not being place in another index of the array, they are being summed, resulting in a char from the ascii table....
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Sun Jul 03, 2011 3:53 pm     Reply with quote

Or possibly you are sending UNICODE characters, not ASCII....

Best Wishes
soapfeldmann



Joined: 24 Jan 2011
Posts: 4

View user's profile Send private message

PostPosted: Sun Jul 03, 2011 6:15 pm     Reply with quote

I've solved the problem on my own by creating the following function:
Code:

char recv[64] = "";
void getString()
{
   int i = 0;
   for (i=0;i<sizeof(recv);i++)
   {
      char c = '\0';
      if (usb_cdc_kbhit())
         c = usb_cdc_getc();
         
      recv[i] = c;
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group