AnsweredHot!USB CDC Transfers - Can you get more than 1 char at a time?

Author
jgvicke
Super Member
  • Total Posts : 458
  • Reward points : 0
  • Joined: 2010/09/28 20:18:24
  • Location: 0
  • Status: offline
2015/09/02 06:52:14 (permalink)
0

USB CDC Transfers - Can you get more than 1 char at a time?

I have the USB stack set up in my program in echoing back characters to TeraTerm. Now it is time to start working on my parser. Does anyone know if you can ever receive more than 1 char at a time from a USB CDC transfer? I know the example application has a 512 byte buffer assigned to it.
 
Thanks,
John Vickers

PIC32 Helpful Tools I have made:
https://rebrand.ly/PIC32MZ144PinMapping
https://rebrand.ly/PIC32MX100PinMapping

Feel free to email me if you have any suggestions for any of these tools.
#1
Paul Bjork
Super Member
  • Total Posts : 204
  • Reward points : 0
  • Joined: 2011/09/21 14:08:10
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/09/02 07:13:52 (permalink)
4 (1)
Definitely.  From the cdc_com_port_single demo app, for example:
 
USB_DEVICE_CDC_Write(USB_DEVICE_CDC_INDEX_0,
&appData.writeTransferHandle,
appData.readBuffer, 1,
USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
#2
jgvicke
Super Member
  • Total Posts : 458
  • Reward points : 0
  • Joined: 2010/09/28 20:18:24
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/09/02 07:36:26 (permalink)
0
Paul,
 
Thanks for the quick response. I know I can send more than 1 at once. I was more wondering if programs like TeraTerm will ever send more than 1 char at a time. I did a test where I did a copy paste of a bunch of text into TeraTerm, and they still came across 1 char at a time in the micro controller. I don't see any reason why it would do this unless it is just how TeraTerm implemented the virtual comm port communications. It may be a limitation of the "Virtual Comm Port" driver itself, where the driver can send and receive more than one char at a time, but between the driver and windows app must be 1 char to be a comm port.
 
All speculation, just wondered if anyone had any experience in this issue.
 
John Vickers

PIC32 Helpful Tools I have made:
https://rebrand.ly/PIC32MZ144PinMapping
https://rebrand.ly/PIC32MX100PinMapping

Feel free to email me if you have any suggestions for any of these tools.
#3
Paul Bjork
Super Member
  • Total Posts : 204
  • Reward points : 0
  • Joined: 2011/09/21 14:08:10
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/09/02 08:37:55 (permalink)
3 (1)
Oops.  Sorry, John, for the trivial answer.
 
In my recollection, CDC reads can specify the number of characters that you're looking for on the micro controller end.  Are you using something like this?
 
USB_DEVICE_CDC_Read (USB_DEVICE_CDC_INDEX_0,
&appData.readTransferHandle, appData.readBuffer,
APP_READ_BUFFER_SIZE);
 
 
#4
jgvicke
Super Member
  • Total Posts : 458
  • Reward points : 0
  • Joined: 2010/09/28 20:18:24
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/09/02 08:50:37 (permalink)
0
Yes, I pretty much just implemented the demo app, so I have this function:
 
USB_DEVICE_CDC_Read (USB_DEVICE_CDC_INDEX_0,
                        &USB.readTransferHandle, USB.readBuffer,
                        512);
 
I just only ever see 1 char at a time in the buffer.
 
John Vickers

PIC32 Helpful Tools I have made:
https://rebrand.ly/PIC32MZ144PinMapping
https://rebrand.ly/PIC32MX100PinMapping

Feel free to email me if you have any suggestions for any of these tools.
#5
roundrocktom
Super Member
  • Total Posts : 992
  • Reward points : 0
  • Joined: 2012/09/07 08:19:09
  • Location: Texas
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/09/02 08:59:39 (permalink)
0
Try it with Hercules 3 2 6
 The Terminal Program is a freeware, so just down load and try it.
 
#6
vloki
always on da run
  • Total Posts : 6655
  • Reward points : 0
  • Joined: 2007/10/15 00:51:49
  • Location: Germany
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/09/02 09:55:18 (permalink)
3 (1)
jgvicke
I just only ever see 1 char at a time in the buffer.
Interesting, I've never checked  this.


Anyway, if you have no luck with hercules you may try mine - described here:
http://www.microchip.com/forums/FindPost/633363 and there
 
The latest version and an executable for windows as well as the needed dlls are here
(more options but without any descriptions at the moment...)
post edited by vloki - 2015/09/02 10:01:39

Posting images, links and code - workaround for restrictions.
#7
jgvicke
Super Member
  • Total Posts : 458
  • Reward points : 0
  • Joined: 2010/09/28 20:18:24
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/02 18:57:23 (permalink)
0
So, quick update. I have been testing this today, and yes you can receive multiple chars in the buffer at the same time. The way to do it with TeraTerm is to copy a blob of text, and past it into the console. They will all hit the micro at the same time.
 
This creates a wrinkle though. As far as I can see in the CDC demo app, there is no way to see how many characters were received. The only way I can see to test for this is to use memset() on the buffer to set it to all 0 before starting a receive sequence. Once the receive is complete, then you can do a strlen() on the buffer to see how many chars are there.
 
Does anyone know if there is a way to access how many chars were received directly? This is not a fast port, so it is not too bad to clear the buffer, but it is wasteful.
 
Thanks,
John Vickers

PIC32 Helpful Tools I have made:
https://rebrand.ly/PIC32MZ144PinMapping
https://rebrand.ly/PIC32MX100PinMapping

Feel free to email me if you have any suggestions for any of these tools.
#8
roundrocktom
Super Member
  • Total Posts : 992
  • Reward points : 0
  • Joined: 2012/09/07 08:19:09
  • Location: Texas
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/03 06:29:53 (permalink)
0
Post Deleted -- got side tracked and posted a non Harmony Response.   Sorry about that Crosland!
post edited by roundrocktom - 2015/11/03 07:54:34
#9
crosland
Super Member
  • Total Posts : 1100
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Bucks, UK
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/03 07:40:45 (permalink)
3 (1)
Is this another "feature" of Harmony, or the latest MLA, to obscure the really useful functionality?
 
If you look at the real functions provided by the USB stack API you'll see that there is a USBUsart read function (or similar) that returns the length of the buffer that was read. At least there is in the version I am using in MPLAB 8 and C18 :)
 
 
#10
Kipton Moravec
Starting Member
  • Total Posts : 68
  • Reward points : 0
  • Joined: 2011/06/20 09:57:32
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/08 20:50:00 (permalink)
3 (1)
I do not know what baud rate you are running at, but my guess is that your task loop (which checks to see if you have data) is running faster than the baud rate selected is sending characters.

For example, if you are at 9600 baud that is about 1 ms per character. If your task loop runs faster than 1 kHz you will only see 1 character at a time.  

MPLAB v3.10
XC32 1.40
Harmony 1.06
Harmony Configurator 1.0.6.22

Thanks,
Kip
#11
crosland
Super Member
  • Total Posts : 1100
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Bucks, UK
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/09 01:29:07 (permalink)
4 (1)
The baud rate has nothing to do with the instantaneous speed of transfers over USB.
 
It will affect the long term average data rate if a physical UART peripheral is being driven. In that case the buffers in the CDC stack will cope with the difference in data rates between USB and the serial link.
#12
Tez
Moderator
  • Total Posts : 462
  • Reward points : 0
  • Joined: 2006/10/04 11:09:05
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/16 07:16:43 (permalink) ☼ Best Answerby jgvicke 2015/11/18 20:00:54
4.75 (4)
You can find out the amount of data received through the USB_DEVICE_CDC_Read function by analyzing the data that accompanies the USB_DEVICE_CDC_EVENT_READ_COMPLETE event. This data is of the pointer of type USB_DEVICE_CDC_EVENT_DATA_READ_COMPLETE and contains a member which contains the received data size.
#13
jcblomquist
Senior Member
  • Total Posts : 102
  • Reward points : 0
  • Joined: 2015/02/17 16:45:02
  • Location: Fastoon
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/18 16:37:58 (permalink)
3 (1)
Thank you Tez. I've got it working now. For anyone else interested that has implemented their program like the CDC Harmony examples, I added a new variable to APP_DATA in app.h (size_t lengthOfStringReceived), and in the CDC event handler function (APP_USBDeviceCDCEventHandler), I added the following line to the "USB_DEVICE_CDC_EVENT_READ_COMPLETE" state:
 
appDataObject->lengthOfStringReceived = ((USB_DEVICE_CDC_EVENT_DATA_READ_COMPLETE *)pData)->length;

 
I then tested it by copying and pasting strings into TeraTerm with more than 1 character. I've got code in my main state machine that does this after the read is completed:
 
for(i = 0; i < appData.lengthOfStringReceived; i++){
receivedString[i] = appData.readBuffer[i];
}

 
Seems to be working correctly for strings of different lengths.
#14
jgvicke
Super Member
  • Total Posts : 458
  • Reward points : 0
  • Joined: 2010/09/28 20:18:24
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/18 20:00:10 (permalink)
4 (2)
I verified this as well today, very similar code, and it does appear to be very robust.
 
jcb, you could use a memcpy to do the same thing:
memcpy((void *)receivedString, (void *)appData.readBuffer, appData.lengthOfStringReceived);

// don't forget to null terminate your string if you want to do other string functions on it
receivedString[appData.lengthOfStringReceived] = 0;

 
John Vickers

PIC32 Helpful Tools I have made:
https://rebrand.ly/PIC32MZ144PinMapping
https://rebrand.ly/PIC32MX100PinMapping

Feel free to email me if you have any suggestions for any of these tools.
#15
Tez
Moderator
  • Total Posts : 462
  • Reward points : 0
  • Joined: 2006/10/04 11:09:05
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2015/11/30 06:14:34 (permalink)
0
Thank you for posting the solution. This is surely helpful to all on the forum!
 
#16
holtech
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2017/10/21 10:25:04
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2017/11/12 12:06:28 (permalink)
0
Indeed... very helpful. Harmony has more mistakes than Cousin Itt hair in The Addams Family. ;)
Just replace the original code with something like this:
 
case USB_DEVICE_CDC_EVENT_READ_COMPLETE:
            /* This means that the host has sent some data*/
        {    
            size_t length = ((USB_DEVICE_CDC_EVENT_DATA_READ_COMPLETE *)pData)->length;
            appDataObject->readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID;
            memcpy(
                (uint8_t *) &cdc_readData[appDataObject->cdc_readProcessedLen],
                (uint8_t *) &cdc_readBuffer,
                length);
            if (appDataObject->cdc_readProcessedLen < MASTER_USB_CDC_COM_PORT_SINGLE_READ_BUFFER_SIZE)
            {
                appDataObject->cdc_readProcessedLen+=length;
            }
            break;
        }
#17
jcandle
Super Member
  • Total Posts : 217
  • Reward points : 0
  • Joined: 2011/09/19 22:01:53
  • Location: 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2017/11/13 21:46:01 (permalink)
0
I had something similar:
 

case USB_DEVICE_CDC_EVENT_READ_COMPLETE:
if(index == 0)
{
//__SYS_INFO_MESSAGE("USB CDC1 Read Completed.\n\r");
foo = (strUSB_DEVICE_CDC_EVENT_DATA_READ_COMPLETE*) (pData);
if (foo)
{
if (foo->length)
{
POSTEDstr posted;
memcpy (posted.Buf, ReadBuffer, APP_READ_BUFFER_SIZE);
posted.Len = foo->length;
xQueueSendToBackFromISR(ProcessForDMX_Queue_Handle, &posted, &xHigherPriorityTaskWoken1);
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken1 );
}
} // end not a packet.
/// JCA - TODO:
// state variable
// if length >= 0; determine intended packet length.
// if length and intended length agree, post the packet.
// if not, it is a partial. In that case we need to
USB_Event = USBDEVICETASK_READDONECOM1_EVENT;
}
/*let processing USB Task know USB if configured..*/
xQueueSendToBackFromISR(USBDeviceTask_EventQueue_Handle, &USB_Event, &xHigherPriorityTaskWoken1);
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken1 );
break;

#18
RISC
Super Member
  • Total Posts : 4631
  • Reward points : 0
  • Status: offline
Re: USB CDC Transfers - Can you get more than 1 char at a time? 2017/11/14 16:02:44 (permalink)
4 (1)
Hi,
This thread is 2 years old...
Regards
#19
Jump to:
© 2017 APG vNext Commercial Version 4.5