View previous topic :: View next topic |
Author |
Message |
soapfeldmann
Joined: 24 Jan 2011 Posts: 4
|
USB CDC - Receive strings |
Posted: Sat Jul 02, 2011 8:23 pm |
|
|
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... |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Jul 03, 2011 1:08 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
soapfeldmann
Joined: 24 Jan 2011 Posts: 4
|
|
Posted: Sun Jul 03, 2011 8:06 am |
|
|
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 I cant get a valid string and there´s alwaays a strange char... |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19366
|
|
Posted: Sun Jul 03, 2011 9:39 am |
|
|
How are you sending the 'strings'?.
Odds are that the 'strange character' is a line feed or carriage return.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
soapfeldmann
Joined: 24 Jan 2011 Posts: 4
|
|
Posted: Sun Jul 03, 2011 10:31 am |
|
|
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.... |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19366
|
|
Posted: Sun Jul 03, 2011 3:53 pm |
|
|
Or possibly you are sending UNICODE characters, not ASCII....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
soapfeldmann
Joined: 24 Jan 2011 Posts: 4
|
|
Posted: Sun Jul 03, 2011 6:15 pm |
|
|
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;
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|