0

I'm using

Serial2.begin(9600, SERIAL_8N1, 15, 14,true);
Serial2.write(b,100);

To write a buffer to serial from the esp32

But for some reason it doesn't transmit the full buffer ? Instead it transmit one byte at a time.

data recived

Any one have an advise on how I can transmit the full buffer, instead of 1 byte at a time ?

Code example:

#include <HardwareSerial.h>

void setup() {
    Serial.begin(9600);
    Serial2.begin(9600, SERIAL_8N1, 15, 14,true); 
}

uint8_t buf[1000];

uint8_t* genBuffer(const char* txt, int* bufSZ)
{
    int index = 0;
    int i = 0;

    buf[index++] = 0x00;
    buf[index++] = 0x00;
    buf[index++] = 0x00;
    buf[index++] = 0x00;
    buf[index++] = 0x00;
    buf[index++] = 0x01;
    buf[index++] = 0x46;
    buf[index++] = 0x46;
    buf[index++] = 0x30;
    buf[index++] = 0x30;
    buf[index++] = 0x02;
    buf[index++] = 0x41;
    buf[index++] = 0x41;
    buf[index++] = 0x43;
    buf[index++] = 0x32;

    for (i = 0; txt[i]; i++) {
        buf[index++] = txt[i];
    }

    *bufSZ = index;

    return buf;
}

void loop()
{
    int sz = 0;
    uint8_t* b = genBuffer("test", &sz);     
    Serial.println(sz); 
    Serial2.write(b,sz);

    for (int i = 0; i < sz; i++) {  
        Serial.print(" ");
        Serial.print(b[i], HEX);
    }
    Serial.println();
    delay(5000);
    // put your main code here, to run repeatedly:
}
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
Stweet
  • 683
  • 3
  • 11
  • 26
  • what is your buffer? this is C++ btw, not C – Piglet Apr 20 '20 at 08:12
  • What baud rate is the UART set to, what transmission speed did you expect? "One byte at a time" is how you send a buffer, of course ... – unwind Apr 20 '20 at 08:21
  • 00 00 00 00 00 01 46 46 30 30 02 41 41 43 32 30 37 46 32 30 30 36 30 31 30.... @Piglet – Stweet Apr 20 '20 at 08:23
  • please provide a [mcve], not just 2 lines of code – Piglet Apr 20 '20 at 08:25
  • 9600baud I mean in serial monitor / busdog it shows 1 byte of length 1 at a time. Using usb/serial adapter this buffer shows all bytes in same transmission and a length of etc. 60 @unwind – Stweet Apr 20 '20 at 08:25
  • @Piglet i have added code – Stweet Apr 20 '20 at 08:30
  • Pls spend sometime to understand how c++ [arrays](https://www.cplusplus.com/doc/tutorial/arrays/) works, and how many of the functions in standard libraries like [cstdlib](https://www.cplusplus.com/reference/cstdlib/) or in [cstring](https://www.cplusplus.com/reference/cstring/) can be used in array/string manipulation, once you understand those, you will be able to solve your problem. – hcheung Apr 22 '20 at 03:55

1 Answers1

0

As Serial.write(buf,len) isdefined

Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
val: a value to send as a single byte.
str: a string to send as a series of bytes.
buf: an array to send as a series of bytes.
len: the number of bytes to be sent from the array.

You have to define e.g.

char b[] = "Two hundred and fiftyfive character have to be in here tomake theexample work ....";

then

 Serial2.write(b,100);

will work, or you define a fixed length buffer

 char b[256] = {'\0'};

and you fill values (converted to string) with itoa, utoa, ftoa into it with strcpy and strcat than you have to make sure that minimum 100 chars are in the buffer to write . If b is an int or similar the command igners the ,100 and behaves like Serial.write(val) -> value to send as a single byte!
EDIT The answer was given before the OP posted the code but as the buf is defined as global array there is no need to return it from a function
You defined it as uint8
If you print your b to serial you'll see weird chars (probably terminator and some memory content) What should the prog do? As by

uint8_t* b

the pointer value is written to serial and not I guess the buffer from that pointer on - so what do you expect? See above usage from the docs.

Codebreaker007
  • 2,911
  • 1
  • 10
  • 22