ESP32S2 VCP Example
ESP32S2 VCP Example
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?
Re: ESP32S2 VCP Example
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
}
Re: ESP32S2 VCP Example
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....
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....
-
- Posts: 9709
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP32S2 VCP Example
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.
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;
vcp->tx_blocking(my_int, sizeof(my_int));
Code: Select all
int my_int=123;
char buffer[16];
sprintf(buffer, "%d", my_int);
vcp->tx_blocking(buffer, strlen(buffer));
Re: ESP32S2 VCP Example
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...
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
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)));
https://drive.google.com/file/d/1v-gMyJ ... sp=sharing
-
- Posts: 9709
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP32S2 VCP Example
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)));
Re: ESP32S2 VCP Example
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.
https://drive.google.com/file/d/1LegEQH ... sp=sharing
Code: Select all
int this_int[] = {0xFE,0xFE};
ESP_ERROR_CHECK(vcp->tx_blocking((uint8_t *)&this_int,(sizeof(this_int)-3)));
Re: ESP32S2 VCP Example
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: Baidu [Spider] and 122 guests