byte b[] = { 0x }

I have a function that sends some bytes to a remote host: byte LCD::send(byte *b, int bytes)

I can call this with i.e.: byte b[] = { 0x45, 0x15 }; byte result = host.send(b, 2);

This works.

My question is this; is there a way to combine the two lines required to call this function? Something like: byte result = host.send({0x45, 0x15});

Since the send() function specifies a 'byte *' as its first parameter, the compiler would probably be unhappy if you tried use a 'const byte *'. It's trying to prevent you from shooting yourself in the foot.

You could try

byte result = host.send(&(byte){0x45, 0x15}, 2);

My question is why?

I get "error: too many initializers for 'byte..." when using &(byte){...}

Why? Because it's faster to write and I have to write around a metric bazillion .send calls, so it would save me a lot of time.

No. The syntax for the function is asking for an array and its length.

Above doesn't work either.

garbage3.c:548:33: warning: excess elements in scalar initializer
     test_function(&(byte){0x45, 0x15}, 2);
                                 ^~~~
garbage3.c:548:33: note: (near initialization for '(anonymous)')

Picks up the first. Prints 69, 3.

Ok.

Is there a way to do this if I re-write the function? I don’t really care what part of the source I have to change, if it saves me time in the long run.

This is the function atm:

byte LCD::send(byte *b, int bytes)
{
  for(int i = 0; i < bytes; i++)
  {
    Serial1.write(*b);
    b++;
  }
  while(Serial1.available() == 0);
  return Serial1.read();
}

Interesting. It works for one element. I had thought that it would work for an array. Sorry.

byte b[] = { 0x45, 0x15 };
byte result = host.send(b, 2);

Rather than pointlessly redeclaring variables every time, save yourself some of that unbelievably arduous typing by using predeclared variables. You can even give the data chunks understandable names, for future debugging.

byte result;
byte command1_2[]={0x45, 0x15};

...

result = host.send(command1_2,2);

Zefugi: Why? Because it's faster to write and I have to write around a metric bazillion .send calls, so it would save me a lot of time.

Not sure (because you didn't post it), but it sounds like your program is poorly / incorrectly structured. Might be time for an array.

jremington: byte b[] = { 0x45, 0x15 }; byte result = host.send(b, 2);

Rather than pointlessly redeclaring variables every time, save yourself some of that unbelievably arduous typing by using predeclared variables. You can even give the data chunks understandable names, for future debugging.

byte result;
byte command1_2[]={0x45, 0x15};

...

result = host.send(command1_2,2);

This is example code, written for forum readability. :) Also: I do not optimize while experimenting.

gfvalvo:
Not sure (because you didn’t post it), but it sounds like your program is poorly / incorrectly structured. Might be time for an array.

This is the actual function:

byte LCD::send(byte *b, int bytes)
{
  for(int i = 0; i < bytes; i++)
  {
    Serial1.write(*b);
    b++;
  }
  while(Serial1.available() == 0);
  return Serial1.read();
}

I fully expect it to be poorly and incorrectly structured, as I normally write C# code, not c/c++. :slight_smile:

The function seems fine. From your reply:

Why? Because it's faster to write and I have to write around a metric bazillion .send calls

the problem seems to be how you're using it. Post an example of the bazillions of calls you have to make. As I implied the solution could be an array (perhaps 2-dimensional) and a loop.

Here's a few:

byte LCD::requestNewBaudRate(byte baudRateID)
{
  byte buffer[] = { 0x51, baudRateID };
  return send(buffer, 2);
}

byte LCD::displayDeviceInfo()
{
  byte buffer[] = { 0x56 };
  return send(buffer, 1);
}

byte LCD::initialiseMemoryCard()
{
  byte buffer[] = { 0x40, 0x69 };
  return send(buffer, 2);
}

..some of the more complex ones will require String input or process a composite response, but most of these functions look like above.

host.send(“\x45\x15”, 2);

This is example code, written for forum readability.

I see, you made up a fake problem, to which you want an answer.

westfw:

host.send(“\x45\x15”, 2);

The send() function wants a ‘byte *’. So I’m thinking that sending it the address of a c-string literal will make the compiler unhappy without casting. With casting, the send() function will be capable of changing that literal via the pointer. Bad form.

Looks like your stuck. I thought maybe you had a whole lot of calls to make at the same time and could blast them out using array / loop.

Better get typing.

westfw: host.send(“\x45\x15”, 2);

That seems to work, after re-writing the function to: byte LCD::send(const char *b, int bytes)

Is there a way to enter variables into this? Like in this example, where the baudRateID is used:

byte LCD::requestNewBaudRate(byte baudRateID)
{
  byte buffer[] = { 0x51, baudRateID };
  return send(buffer, 2);
}

jremington: I see, you made up a fake problem, to which you want an answer.

I have re-written the question, using actual runtime code, just for you as this is quite real. I do think this makes for a less readable question, but if you prefere it, then why not:

I have a function that sends some bytes to a remote host: byte LCD::send(byte *b, int bytes)

I actually call this with: byte buffer[] = { 0x51, baudRateID }; return send(buffer, 2);

This works.

My question is this; is there a way to combine the two lines required to call this function? Something like: return send({0x51, baudRateID });

gfvalvo: Looks like your stuck. I thought maybe you had a whole lot of calls to make at the same time and could blast them out using array / loop.

Better get typing.

thanks for trying.