USB Communication Limited to ASCII

Hi,

I’m fairly new to Wixel, and trying to transmit integers over USB to a serial terminal on the Wixel, using the provided USB communication libraries. However, I seem to be limited to ASCII, e.g.

if(usbComTxAvailable()) {
            usbComTxSendByte(55);
}

results in “7” being sent to the terminal. Is there a way to send integers?

The Wixel’s USB serial port is not limited to ASCII. You can send and receive any byte from 0 to 255.

However, most serial terminal programs assume that the serial port data consists of characters encoded in ASCII or a similar encoding, so when you send byte 55 from your Wixel, the terminal receives it and displays the character “7” on the screen.

If you are using Windows and you want to send and receive small amounts of binary data over a serial port, you might consider using the Pololu Serial Transmitter. It shows the hex value of every received byte, along with the ASCII interpretation.

If you want your Wixel program to work with normal terminal programs, then you have to add some code to the Wixel firmware to format your number as ASCII before sending to the serial port.

The example_usb_com program in the Wixel SDK shows one way to do this using sprintf. To send an integer, you would do something like:

uint8 XDATA response[32];
if (usbComTxAvailable() >= sizeof(response))
{ 
  uint8 responseLength = sprintf(response, "%d\r\n", 55);
  usbComTxSend(response, responseLength);
}

If you are going to be writing to the serial port in more than a few places, it could be convenient to configure printf so that it writes to the serial port. You do this by defining putchar, which is called by printf. You can define putchar to just call usbComTxSendByte as we do in the radio_sniffer app. However, that doesn’t work well for messages larger the maximum return value of usbComTxAvailable. A more flexible alternative is to make putchar write to a large buffer and then send it to the serial port in pieces at a later time, as is done in test_adc.

Please let me know if you have any further questions.

–David

1 Like

Great - thanks!