ESP32S2 VCP Example

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

ESP32S2 VCP Example

Postby MarkIngle » Tue Jul 18, 2023 12:58 am

I am working to understand the new ESP32S2 serial architecture. It is a great design! I have been able to successfully connect to a device with a FTDI USB to Serial chip and transmit a string of text. This is done with the tx_blocking function. Now I am interested in sending integers but I cannot get the code to compile when using an integer. And when it does compile the values are not correct on the other end/serial device. My question is the tx_blocking function only designed to work with strings?

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: ESP32S2 VCP Example

Postby bidrohini » Tue Jul 18, 2023 8:45 am

As far as I know, the ESP32-S2 architecture does not have a built-in "tx_blocking" function for serial communication.Here's an example of how you can send an integer over serial communication in Arduino (using the Arduino core for ESP32):

Code: Select all

void setup() {
  Serial.begin(115200);
}

void loop() {
  int intValue = 42;
  char buffer[10]; // Make sure the buffer is large enough to hold the integer as a string
  snprintf(buffer, sizeof(buffer), "%d", intValue);
  Serial.println(buffer);
  delay(1000); // Optional delay to avoid flooding the serial communication
}

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

Re: ESP32S2 VCP Example

Postby MarkIngle » Tue Jul 18, 2023 10:02 pm

Thanks! I am using the cdc_acm_vcp_example_main.cpp from the examples directory that comes with the esp-idf 5.1 install. The function is at this link..

https://github.com/espressif/esp-idf/bl ... C9-L171C63

You might be correct in your statement though...I have searched the esp repo and cannot find the tx_blocking function anywhere.

At this point I dont think its possible to send and integer...which sounds crazy. I will keep trying though....

ESP_Sprite
Posts: 9772
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32S2 VCP Example

Postby ESP_Sprite » Wed Jul 19, 2023 12:47 am

Well, vcp->tx_blocking() sends a random chunk of data out. Question is how you want to send the integer. If you want to send it as binary, you'd do e.g.

Code: Select all

int my_int=123;
vcp->tx_blocking(my_int, sizeof(my_int));
but you may very well may want to do it as a decimal number, in which case you need to convert it first:

Code: Select all

int my_int=123;
char buffer[16];
sprintf(buffer, "%d", my_int);
vcp->tx_blocking(buffer, strlen(buffer));

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

Re: ESP32S2 VCP Example

Postby MarkIngle » Wed Jul 19, 2023 2:17 am

Hi ESP_Sprite! Yes thank you for confirming that tx_blocking sends a chuck of data. The problem with using an integer is that my code will not compile...see screenshot at the link below


https://drive.google.com/file/d/1OinDvZ ... sp=sharing

This code works...I have been able to do something similiar but it is not what I need...

Code: Select all

ESP_LOGI(TAG, "Sending data through CdcAcmDevice");
        //uint8_t data[] = "254";
        //const char* data[] = {"254","!","0","255","0","L","0","0","128","?","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","20","0","0","0","0","195","223"};
        //uint8_t data[] = "o";

        //THIS WILL NOT COMPILE...
        //int this_int = 254;
        //ESP_ERROR_CHECK(vcp->tx_blocking(this_int,sizeof(this_int)));

        //THIS COMPILES AND WORKS!!! BUT NOT WHAT I NEED....I NEED TO SEND AN INTEGER
        int n = 254;
        char buffer[16];
        sprintf(buffer, "%d", n);
        ESP_ERROR_CHECK(vcp->tx_blocking((uint8_t *)buffer, strlen(buffer)));

Here is a screenshot of my end goal...once I understand how to send integers I can build a loop or an array to send the data needed

https://drive.google.com/file/d/1v-gMyJ ... sp=sharing

ESP_Sprite
Posts: 9772
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32S2 VCP Example

Postby ESP_Sprite » Wed Jul 19, 2023 6:00 am

That's fair, you likely need to cast the int to an uint8_t pointer:

Code: Select all

ESP_ERROR_CHECK(vcp->tx_blocking((uint8_t*)&this_int,sizeof(this_int)));

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

Re: ESP32S2 VCP Example

Postby MarkIngle » Wed Jul 19, 2023 11:00 pm

Thanks ESP_Sprite! That got me going with using integers....I am fighting the additional 3 bytes when using an array of integers but I will try to solve that myself...helps with learning techniques. Once I get this working I will post the solution here for others who might need it.

Code: Select all

int this_int[] = {0xFE,0xFE};
        ESP_ERROR_CHECK(vcp->tx_blocking((uint8_t *)&this_int,(sizeof(this_int)-3)));
https://drive.google.com/file/d/1LegEQH ... sp=sharing

MarkIngle
Posts: 30
Joined: Wed Jan 18, 2017 4:48 am

Re: ESP32S2 VCP Example

Postby MarkIngle » Thu Jul 20, 2023 12:59 am

Success!!! I need to initialize the arrary to uint8_t ....1 byte instead of 4 bytes! Much easier...

Code: Select all

//THIS WILL COMPILE YAY!!!!...
        //                 1    2    3    4    5    6    7    8    9    10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41
        uint8_t this_int[] = {0xFE,0x21,0x00,0xFF,0x00,0x4C,0x00,0x00,0x80,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x00,0x00,0x00,0x00,0xC3,0xDF};
        ESP_ERROR_CHECK(vcp->tx_blocking((uint8_t *)&this_int,(sizeof(this_int))));

Who is online

Users browsing this forum: No registered users and 105 guests