How to read unsigned char array from serial port?


#1

i have used such as

Serial4.read(response,9);

where response is an unsigned char array[9] variable

it gives error as

no matching function for call to ‘USARTSerial::read(unsigned char [9], int)’

can anybody help me


Serial Read to char Array
#2

USARTSerial class inherits from Stream and hence you should find something there
https://docs.particle.io/reference/firmware/photon/#stream-class


#3

i think it is readBytes() in stream

but how to declare stream class variable?


#4

Serial4 (as pointed out) is an instance inheriting from Stream class…

Serial4.readBytes(myBuffer, size);

#5

thank u i got it


#6

Question on The Serial, I know this is and older thread , How can I get a Serial read to input data into an Array with out uploading the new firm where every time.


char t;
 char SENSOR_THr[3]={0x00,0x00,0x00};
void setup()
{
 // begin the serial communication
 Serial.begin(9600);
}
void loop(){
    
  while (Serial.available() == 0)  {
      t=Serial.read();
    SENSOR_THr[0] = 0;
    SENSOR_THr[1] = 0;
    SENSOR_THr[2] = 0;
    }
{
 // check if data has been sent from the computer
 if (t=='t') {
    for ( int i = 0; i < 3; i++)
    SENSOR_THr[i] = Serial.read();
   Serial.printlnf("Sensor%X", SENSOR_THr[i]);
 }
}

need to add Hex digits into SENSOR_THr,


#7
  while (Serial.available() == 0)  {
      t=Serial.read();
    SENSOR_THr[0] = 0;
    SENSOR_THr[1] = 0;
    SENSOR_THr[2] = 0;
    }

translation: if there is nothing in the Serial buffer, read from the Serial buffer…

so since read() returns an int, and returns -1 if no data is available, your code is not doing, well, anything.

another noteworthy point is that your device’s processor is Very Fast, and loop() is executed at least once or more between each character arriving, you would want to avoid the while (Serial.available()) paradigm.


#8
char SENSOR_THr[3] = {0x00,0x00,0x00};
//want to be able to change these whith Serial input and not have to reload every time 
//{0x05,0x43,0xAA} so something like this where type 0x05, 0x43, 0xAA and have it loaded into the array
void setup()
{
 // begin the serial communication
 Serial.begin(9600);
}
void loop(){
    
      for (int i = 0; i<= 2; i++)
      {SENSOR_THr[i] = Serial.read();//Store all remaining data in an array
      
      delay(40);//Need to give time to empty the serial buffer
      }
  Serial.printlnf("Sensor 0 %X", SENSOR_THr[0]);
 Serial.printlnf("Sensor 1 %X", SENSOR_THr[1]);
 Serial.printlnf("Sensor 2 %X", SENSOR_THr[2]);

}

#9

Thanks working on it add new stuff but still can not get it to work right
trying to load in three hex number into a array to change array without having to load it into the firmware and reload it every time.


#10

try this (complex) asynchronous approach:

// example packet: t123

const size_t PACKET_SIZE = 3;

template <class T> inline Print& operator <<(Print& obj, T arg)
{
  obj.print(arg);
  return obj;
}

char SENSOR_THr[3];

void setup()
{
  Serial.begin(9600);
  Serial << ("let's go!\n");
}

void loop()
{
  if (const char* newPacket = checkForNewPacket(Serial, 't'))
  {
    Serial << (F("Just Recieved:\t")) << newPacket[0] << "," << newPacket[1] << "," << newPacket[2] << "\n";
    for (size_t i = 0; i < PACKET_SIZE; i++) {
      SENSOR_THr[i] = newPacket[i];
    }
    for (auto i : SENSOR_THr) {
      Serial.print(i, HEX);
      Serial.print(",");
    }
    Serial.println();
  }
}

const char* checkForNewPacket(Stream& stream, const char startMarker)
{
  static char incomingPacket[PACKET_SIZE] = "";
  static bool gotStartMarker = false;
  static byte idx = 0;
  if (stream.available())
  {
    if (gotStartMarker) {
      incomingPacket[idx++] = stream.read();
      if (idx > sizeof(PACKET_SIZE)) {
        gotStartMarker = false;
        return incomingPacket;
      } else {
        return nullptr;
      }
    } else if (stream.peek() == startMarker) {
      idx = 0;
      gotStartMarker = true;
      (void) stream.read();
    } else {
      (void) stream.read();
    }
  }
  return nullptr;
}

Serial Read to char Array
#11

Question on The Serial, I know this is and older thread , How can I get a Serial read to input data into an Array with out uploading the new firm where every time.

char t;
 char SENSOR_THr[3]={0x00,0x00,0x00};
void setup()
{
 // begin the serial communication
 Serial.begin(9600);
}
void loop(){
    
  while (Serial.available() == 0)  {
      t=Serial.read();
    SENSOR_THr[0] = 0;
    SENSOR_THr[1] = 0;
    SENSOR_THr[2] = 0;
    }
{
 // check if data has been sent from the computer
 if (t=='t') {
    for ( int i = 0; i < 3; i++)
    SENSOR_THr[i] = Serial.read();
   Serial.printlnf("Sensor%X", SENSOR_THr[i]);
 }
}

need to add Hex digits into SENSOR_THr,


#12

answered here:

on your other post.


#13

As you see in the reply you got, it’s not really appreciated to double post a topic you already had opened.
Hence I’ll close this new topic and move its posts back to the original thread.


#14

Another way to do it might be like so.

I’m not sure what you’re doing with the character ‘t’, but in the sample code below, I’m using it to indicate the next 3 characters should be added to the array.

char t;
bool readT;
int counter = 0;
char SENSOR_THr[3]={0x00,0x00,0x00};
char sensorString[20];


void setup() {

 Serial.begin(9600);
}

void loop(){
    
    
    if (Serial.available() > 0 && readT == false) {
        if (Serial.read() == 't') {
            Serial.println("got a t");
            readT = true;
        }
    }
        
    if (Serial.available() > 0 && readT == true) {
        SENSOR_THr[counter] = Serial.read();
        counter ++;
        if (counter > 2) {
            readT = false;
            counter = 0;
            sprintf(sensorString, "0x%x, 0x%x, 0x%x", SENSOR_THr[0], SENSOR_THr[1], SENSOR_THr[2]);
            Serial.printlnf("Result is: %s", sensorString);
        }
    }
}

#15

Thanks to the Help but still little unclear how Get (0x05,0x00,0x00) or (0xAA, 0xEA, 0x43) need something like this into the SENSOR_THr


#16

I changed the printout in the code I posted so you can see the result as hex values. The code I posted does put those values into SENSOR_THr. Did you try it?
What method are you using (or planning to use) to send the data to your device? What range of values do you need to send? Does it need to be all values from 0x00 to 0xff? Also, do you need to use serial instead of using a publish or a Particle.function to add the values; are your devices not connected to the cloud?