|
|
View previous topic :: View next topic |
Author |
Message |
neohimura
Joined: 08 Feb 2011 Posts: 14
|
question about usb cdc on PIC18F4550 |
Posted: Fri Apr 01, 2011 1:31 am |
|
|
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: 19540
|
|
Posted: Fri Apr 01, 2011 3:55 am |
|
|
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
|
|
Posted: Fri Apr 01, 2011 7:47 am |
|
|
I created a new function on usb_cdc.h as you mentioned and it works perfectly.
Thank you very much Ttelmah. |
|
|
|
|
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
|