8 years, 4 months ago.

Serial Communication Write Packets

I'm new to using mbed and so I'm a little stuck with using serial communication. I'm trying to send large-ish packets out through serial. I'm using the following code:

Serial pc(dp16,dp15); pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5);

(There is more to the code but this is the relevant part)

I'm wondering if there is an alternative to pc.printf() that will allow me to send out arrays? My problem is that I'm trying to send out sensor values and I would like to send out more than one value for each sensor per packet.

pc.write() seemed like it might be what I want but I couldn't find any examples of it being used... Thanks guys.

1 Answer

8 years, 4 months ago.

If you want to send an array of strings the a for loop using puts() would work.

for (int i = 0; i<5; i++) {
  pc.puts(data[i]);
  pc.putc(',');
}

Or if the data is in a nice solid block with no gaps between (unlikely for strings) you could use pc.write(array,numberOfElements*sizeof(arrayElement));

Accepted Answer

Thanks! I'm going to try the pc.write solution.

posted by Matthew Mellor 27 Jun 2016