Implementing Modbus from existing project

This is an example that should work with an Arduino UNO:

  byte Anemometer_request[] = {0x01, 0x03, 0x00, 0x16, 0x00, 0x01, 0x65, 0xCE}; // inquiry frame
  RS485Serial.write(Anemometer_request, sizeof(Anemometer_request));
  RS485Serial.flush();
  byte Anemometer_buf[8];
  RS485Serial.readBytes(Anemometer_buf, 8);
  currentValue = (Anemometer_buf[4]);

However, since I don't have SoftwareSerial available (Z-UNO), I need to adapt the above code to work.

Z-Uno example code:

  if(Serial0.available() > 0) {
    char c = Serial0.read();  
    Serial.print(c,DEC);
    currentValue = 9999; // Set here value from above
}

Unfortunately I don't know how to do this.

My approach seems to fail:

  if(Serial0.available() > 0) {
    byte Anemometer_request[] = {0x01, 0x03, 0x00, 0x16, 0x00, 0x01, 0x65, 0xCE}; // inquiry frame
    Serial0.write(Anemometer_request, sizeof(Anemometer_request));
    Serial0.flush();
    byte Anemometer_buf[8];
    Serial0.readBytes(Anemometer_buf, 8);
    currentValue = (Anemometer_buf[4]);

Error:

uCxx returned error code:1

exit status 1
no matching member function for call to 'write'
Selected code line:     Serial0.write(Anemometer_request, sizeof(Anemometer_request));

call looks OK - have a look at Z-UNO serial - use print()?

Good point. But I have an array using hex values.

It seems that print doesn’t handle arrays. Or does ist?

Serial0.print() doesn't work with the array.

However I found the solution (test is pending).

Z-Uno doesn't support to write buffers. This will apparently work.

void SerialWriteBuf(uint8_t *buffer, size_t size)
{
  while (size != 0) {
    Serial0.write((uint8_t) * buffer);
    buffer++;
    size--;
  }
}
...
SerialWriteBuf(Anemometer_request, sizeof(Anemometer_request));

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.