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

question about usb cdc on PIC18F4550

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



Joined: 08 Feb 2011
Posts: 14

View user's profile Send private message

question about usb cdc on PIC18F4550
PostPosted: Fri Apr 01, 2011 1:31 am     Reply with quote

Hi,

I'm using the usb cdc function on PIC18F4550. I'm trying to send a array of data to PC using this function. The data I'm trying to send is as follows:

Code:
int8  send_usb_data[42];


Which of the following code is the best way to send that data to usb? Or is there another better way? I tried using usb_cdc_puts but whenever there is null data on the array, it will stop transmitting. Is there a way around this?

Code:
if (usb_enumerated())
{
    usb_cdc_puts(send_usb_data);
}


Code:
if (usb_enumerated())
{
     for (c=0;c<42;c++)
    {
       usb_cdc_putc(send_usb_data[c]);
    }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19343

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 3:55 am     Reply with quote

What is a 'string' in C?.
A string is a _null terminated_ array of characters. Keyword here is 'null terminated'. Obviously trying to send a zero (NULL), should terminate a 'string' operation. So 'puts' is never going to work for an integer array containing zeros.
It is also potentially 'dangerous'. Things meant to work with 'strings', _require_ the null terminator. If your array happens not to contain an '0', then you could end up sending much more data than you expect...
Then look st the source code. usb_cdc.h
Read what it says. The only 'advantage' of 'puts', versus using 'putc', is that the former will send the entire 'string' as one transaction.
So then ask yourself if this 'matters' in your case?.
If it doesn't, then the putc solution, is simple.
If it does, then simply copy the usb_cdc_puts code, and remove the test for the null character (if (!c)), and substitute the buffer size count as the limit.
You could make it into a generic function, say
'usb_cdc_putarray(int8 *ptr, int8nbytes)', and then just call this with an array and the number of bytes.

Best Wishes
neohimura



Joined: 08 Feb 2011
Posts: 14

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 7:47 am     Reply with quote

I created a new function on usb_cdc.h as you mentioned and it works perfectly.

Thank you very much Ttelmah. Very Happy
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