Page 1 of 1
Serial0.write not working
Posted: 14 Oct 2017 11:02
by dawiinci
I want to use TX0 to send code to a computer.
Code: Select all
uint8_t test[5] = { 0x02, 0xFF, 0xFF, 0x01, 0x02 };
uint8_t command[5] = { 0x0F, 0x01, 0xF1, 0x00, 0x00 };
memcpy(command, test, sizeof(command));
Serial0.write(command, 5);
However, I get this error:
Code: Select all
no matching member function for call to 'write'
I am using the 2.1.1 test build.
In addition I can't use AltSoftSerial.h with Z-Uno. (not found)
Re: Serial0.write not working
Posted: 14 Oct 2017 13:05
by petergebruers
Welcome, dawiinci. Good catch... It seems HardwareSerial does not implement writing a buffer, while other classes do implement it.
Can you work around it like this? Works for me!
Code: Select all
uint8_t test[] = { 0x02, 0xFF, 0xFF, 0x01, 0x02 };
uint8_t command[] = { 0x0F, 0x01, 0xF1, 0x00, 0x00 };
void SerialWriteBuf(uint8_t *buffer, size_t size)
{
while (size != 0) {
Serial.write((uint8_t) * buffer);
buffer++;
size--;
}
}
void setup() {
Serial.begin();
}
void loop() {
SerialWriteBuf(command, sizeof(command));
delay(3000);
SerialWriteBuf(test, sizeof(test));
delay(3000);
}
Re: Serial0.write not working
Posted: 14 Oct 2017 13:20
by dawiinci
Thank you
I have to be honest I don't know why I had to use the buffer. But it worked like this and I don't want to break anything in the recipient. Maybe you have an explanation for this. The workaround seems plausible though.
I am also using it for Serial0.readBytes(status, 5), where ist seems to work. At least I don't get an error.
Re: Serial0.write not working
Posted: 15 Oct 2017 12:10
by dawiinci
Ok, I think I understand now. Serial ports only send one byte. With the buffer I can simply send multiple bytes.
Do you have an idea why Serial0.readBytes(status, 5); works? I'm using buffers as well. But I don't get any error. Is this supported?
Thanks for the workaround.
Re: Serial0.write not working
Posted: 15 Oct 2017 12:23
by petergebruers
Yes, readBytes is supported. I use it in my MH-Z19 sketch.