none
C# and USB CDC problem RRS feed

  • Question

  • Hi everybody,

    I'm using stm32 microcontroller as a USB CDC device. I can send data to the usb device using some terminal sofware which is written in a low level programming language. However I can not send data from the C# program, actually, only the first byte of the data has been sent to the micro and I can't understand this behavior of the C#.

    Can anybody help me about this problem? I really spend a lot of  time to solve the problem but i didn't succeed.

    Thanks

    Wednesday, June 21, 2017 2:52 PM

All replies

  • actually, only the first byte of the data has been sent to the micro 

    UTF-16.  Your "data" likely is a string that .NET tends to encode in UTF-16 so every odd byte is binary 0, which means end of the string in the "low level programming language". Convert the unicode string to "ANSI string" or array of bytes before sending to the device.

    - pa

    Wednesday, June 21, 2017 3:02 PM
  • Thank you Pavel A for your attention. 

    I convert to array of bytes before sending the data. I used System.Text.Encoding.ASCII.GetBytes() to convert a string to the array of bytes. Is this wrong?

    I am using LibUsbDotNet for reading and writing to usb in C#, so special configuration is required on UsbEndpointWriter or UsbDevice? Even using the the SerialPort class for sending data, I have the same problem. 

    Thanks

    Wednesday, June 21, 2017 6:31 PM
  •  I used System.Text.Encoding.ASCII.GetBytes() to convert a string to the array of bytes. Is this wrong?

    This should be perfect for a serial port (if done correctly, of course). But you say it looks to the system not as a serial port? I am not familiar with stm32. Is it some custom interface class that works with libusb (or maybe WinUSB)? Or can it change communication mode?

    This is important, without knowing exactly how to talk to this interface  chances to success are slim. Sometimes there's no other choice but to read the documentation...

    -- pa

    Wednesday, June 21, 2017 10:20 PM
  • Hi Pavel A, 

    I used the USB peripheral of the microcontoller in USB CDC mode and the device looks to the system as virtual com port. It can be treated as a usb device or as a serial port, However it does not matter which way to communicate with the device, the result is the same : only the first byte will be received.

    My USB device descriptor is as follows:

    STMicroelectronics.- STMicroelectronics Virtual COM Port

    OSVersion: Microsoft Windows NT 6.2 .9200 .0 LibUsbDotNet

    Version: 2.2 .8 .104

    DriverMode: LibUsb

    Length: 18

    DescriptorType: Device

    BcdUsb: 0x0200

    Class: Comm

    SubClass: 0x02

    Protocol: 0x00

    MaxPacketSize0: 64

    VendorID: 0x0483

    ProductID: 0x5740

    BcdDevice: 0x0200

    ManufacturerStringIndex: 1

    ProductStringIndex: 2

    SerialStringIndex: 3

    ConfigurationCount: 1

    ManufacturerString: STMicroelectronics

    ProductString: USB Device

    SerialString: 00000000001 A

    CONFIG #1

      Length:9

      DescriptorType:Configuration TotalLength:67 InterfaceCount:2 ConfigID:1 

      StringIndex:0 Attributes:0xC0 MaxPower:50 ConfigString:

     

    INTERFACE (0,0)

      Length:9

      DescriptorType:Interface

      InterfaceID:0

      AlternateID:0

      EndpointCount:1 

      Class:Comm

      SubClass:0x02

      Protocol:0x01

      StringIndex:0

      InterfaceString:

      ENDPOINT 0x82

      Length:7

      DescriptorType:Endpoint

      EndpointID:0x82

      Attributes:0x03

      MaxPacketSize:8 

      Interval:16

      Refresh:0

      SynchAddress:0x00

     

    INTERFACE (1,0)

      Length:9

      DescriptorType:Interface

      InterfaceID:1

      AlternateID:0

      EndpointCount:2 

      Class:Data

      SubClass:0x00

      Protocol:0x00

      StringIndex:0

      InterfaceString:

     ENDPOINT 0x01  

      Length:7

      DescriptorType:Endpoint

      EndpointID:0x01

      Attributes:0x02

      MaxPacketSize:64 

      Interval:0

      Refresh:0

      SynchAddress:0x00

    ENDPOINT 0x81  

      Length:7

      DescriptorType:Endpoint

      EndpointID:0x81

      Attributes:0x02

      MaxPacketSize:64 

      Interval:0

      Refresh:0

     SynchAddress:0x00

    Does this mean anything for you to help me?

    Thanks

    Thursday, June 22, 2017 6:47 AM
  • Hi

    Can anyone help me to solve the problem?

    thanks

    Saturday, June 24, 2017 10:40 AM
  • Well, since the "low level" program works - the device indeed behaves as a COM port. So most likely, your Text.Encoding or something else in the C# program is not done correctly. Try to use some serial monitor (software or hardware) to see the actual data exchanges.

    -- pa

    Saturday, June 24, 2017 11:22 PM