0

I try to start a array in the header file

rs485.h

class RS485
{
public:
uint8_t off[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
void sendmsg(uint8_t* cmd);

};

rs485.cpp

void RS485::sendmsg(uint8_t* cmd)
{
    //digitalWrite(ENTX_PIN, HIGH); // enable to transmit
    Serial.println("sending message------------");

    Serial2.write(cmd[0]);
    Serial.println(cmd[0], HEX);
    Serial2.write(cmd[1]);
    Serial.println(cmd[1], HEX);
    Serial2.write(cmd[2]);
    Serial.println(cmd[2], HEX);
    Serial2.write(cmd[3]);
    Serial.println(cmd[3], HEX);
    Serial2.write(cmd[4]);
    Serial.println(cmd[4], HEX);
    Serial2.write(cmd[5]);
    Serial.println(cmd[5], HEX);
    Serial2.write(cmd[6]);
    Serial.println(cmd[6], HEX);
    Serial2.write(cmd[7]);
    Serial.println(cmd[7], HEX);

    Serial.println("--------------------------");
}

main.cpp


void callback(char *topic, byte *payload, unsigned int length)
{

'''omit'''

  if (cmd)
  {
    Serial.print("cmd: ");
    Serial.println(cmd);
    if (cmd == 700)
    {
      rs485.sendmsg(rs485.off);
    }
    else if (cmd == 701)
    {
      rs485.sendmsg(rs485.on);
    }
'''omit'''
}
'''omit'''

}

complier have an error message of "too many initializer values".

When I try to use

    uint8_t off[8] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};

it build normally. The problem is when in use this variable in main.cpp and pass it to rs485.cpp only one element off[0] is pass normally.

 rs485.sendmsg(rs485.off);

I have use serial print to check all value it can all print out but the rs485 cannot tx all char.

    Serial2.write(cmd[0]);
    Serial.println(cmd[0], HEX);
    Serial2.write(cmd[1]);
    Serial.println(cmd[1], HEX);
    Serial2.write(cmd[2]);
    Serial.println(cmd[2], HEX);
    Serial2.write(cmd[3]);
    Serial.println(cmd[3], HEX);
    Serial2.write(cmd[4]);
    Serial.println(cmd[4], HEX);
    Serial2.write(cmd[5]);
    Serial.println(cmd[5], HEX);
    Serial2.write(cmd[6]);
    Serial.println(cmd[6], HEX);
    Serial2.write(cmd[7]);
    Serial.println(cmd[7], HEX);

Any result for that?

add the wiring

gpio26 -->DE+RE
gpio21 -->DI
gpio25 -->RO
Samuel
  • 228
  • 2
  • 13
  • Dupe1: [too many initializers for 'int [0\]' c++](https://stackoverflow.com/questions/21152171/too-many-initializers-for-int-0-c), Dupe2: [why does int arr[\]={0,3,2,4,5,6,7};give error and int arr[7\]={0,3,2,4,5,6,7}; not](https://stackoverflow.com/questions/55046463/why-does-int-arr-0-3-2-4-5-6-7give-error-and-int-arr7-0-3-2-4-5-6-7-no) – Jason Aug 02 '22 at 05:46
  • @AnoopRana The real question isn't so much about the initializer. It comes a bit later: _"The problem is when in use this variable in main.cpp and pass it to rs485.cpp only one element off[0] is pass normally."_ – Ted Lyngmo Aug 02 '22 at 05:48
  • 1
    @TedLyngmo Yes there are two question asked and both are equally "real". I agree that this question should be remained open. I mentioned the above two links so that OP can see for themselves what is wrong with their first code snippet. – Jason Aug 02 '22 at 05:50

1 Answers1

1

A member variable declared as

uint8_t off[] = { ... };

does not become an array with the number of elements in the initializer list, like when you declare a non-member variable. Instead, it then becomes a "flexible array" (a C thing that g++ enables by default in C++) - and flexible arrays can't have initializers, which is why you get "too many initializer values".

The correct way is therefore to specify the number of elements:

uint8_t off[8] = { ... };

I suggest that you send them all out at once and check how many that are actully written:

size_t written = Serial2.write(cmd, 8);
Serial.println(written);

This should display 8 if the sending code works.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • '''cmd: 700 sending message------------ 8 ''' I can print 8 with serial.print. but the TX form 485 is still 00 04 FC. – Samuel Aug 02 '22 at 05:30
  • @Samuel Since it prints `8`, all 8 `uint8_t` were sent. What does _"the TX form 485 is still 00 04 FC"_ mean? If you are not receiving all 8 then the wiring or receiver code could be wrong. – Ted Lyngmo Aug 02 '22 at 05:38
  • the connection now is esp32-->RS485--> RS485 USB module --> my pc. I use the RS485 USB module to monitor the output of the "esp-->RS485" part. I try to get all 8 char in my pc, but the output i can get is 0x00 0x04 0xFC. – Samuel Aug 02 '22 at 05:43
  • do I need to use Serial2.flush();?? – Samuel Aug 02 '22 at 05:46
  • @Samuel You could try `flush()`, sure. I don't think it should be needed though. Are the transmission modes set the same on both sides? – Ted Lyngmo Aug 02 '22 at 05:46
  • yes, same all are 9600 8N1 – Samuel Aug 02 '22 at 05:48
  • @Samuel _"In order to be used as a transmitter, RE pin and DE pin must be connected to 5V, and DI pin is connected to TX. Data is sent from Arduino TX pin to module DI pin, then data will be sent through AB."_ - Is that how you've wired it? – Ted Lyngmo Aug 02 '22 at 05:50
  • The message become 00 40 FC – Samuel Aug 02 '22 at 05:53
  • the RE and DE pin is connected to GPIO 26 for used as transmitter and receiver. I check that the voltage can only goto 3.3V. Is this too low? – Samuel Aug 02 '22 at 05:57
  • @Samuel I'm not sure, but since it says _"must be connect to 5V"_ it does seem a little low. – Ted Lyngmo Aug 02 '22 at 05:58
  • I can rx mesage normally form my PC, but the problem is the message form the ESP32. – Samuel Aug 02 '22 at 05:58
  • I try to hard wire RE and DE to 5V first – Samuel Aug 02 '22 at 05:59
  • 00 04 A0 04 41 D0. only 6 char. – Samuel Aug 02 '22 at 06:13
  • @Samuel It doesn't look like the correct `uint8_t`s either. – Ted Lyngmo Aug 02 '22 at 06:15
  • I hand make another 485 adaptor and I can read all char normally. hardware problem on my 485 USB adaptor. Thanks Ted. – Samuel Aug 02 '22 at 06:46
  • @Samuel Brilliant! Glad it worked out. Good job and you're welcome! – Ted Lyngmo Aug 02 '22 at 06:47