Removing ⸮ chars from string buffer

Hi there,

I have a full lenght data output from my I2C slave device when my master device request it.

My issue is that I can't remove the trailing ⸮⸮⸮⸮⸮⸮⸮⸮⸮ chars from my data input.

Tried with .trim(); .replace(" ", ""); without any success.

Any help would be appreciated,

Thank you! :wink:

Any help would be appreciated,

We can't see your code, so there is no way we can help.

Delta_G:

 .trim(); .replace(" ", ""),

If you want it to replace question marks then why you tell it to replace spaces?

Thank you for the quick response! :wink:

No, that was just an example.

But

.replace("?", "");

will not remove them either! :frowning:

Delta_G:
It's hard to help when you show "examples" that aren't what really happened. Then we end up chasing some red herring instead of your actual problem. Please be explicit about what's really going on.

Slave:

String dataToSend= "#Apple@23.43@Mellon@Peach";

void requestEvent()
{
  Wire.write(dataToSend.c_str());
}

Master:

String dataRaw = "";

void readSlave()
{
  Wire.requestFrom(SLAVE_ADDRESS, 32);

  while (Wire.available()) {
    dataRaw += (char)Wire.read();
  }

  String dataIn = String(dataRaw);
  //dataIn.replace("?", "");
  //dataIn.trim();

  if (dataIn.charAt(0) == '#')
  {
    Serial.println("Data found!");
    Serial.println(dataIn);
  } else {
    Serial.println(dataIn.charAt(0));
    Serial.println("Data Not found!");
  }

  //stTmp.trim();
  //Serial.println(stTmp);
  dataIn = "";
  dataRaw = "";
}

The output I'm getting is:

#Apple@23.43@Mellon@Peach⸮⸮⸮⸮⸮⸮⸮

Assuming you MUST use String, perhaps the easiest solution is to add a terminator to your ‘dataToSend’, then your receiver can look for that marker to trim off the string of characters after the last character.

If you are getting a ⸮ in the String, then why on earth would you expect the String to change when you tell it to replace all ? with nothing?

If you are sending 25 characters, why are you asking for 32?

You seem to NOT understand just how masters and slaves are supposed to communicate, using I2C. If you need to send a variable amount of data from the master to the slave, you MUST do that in two steps. The first step is to send the amount of data (in binary, NOT ASCII). The second is to send the variable amount of data.

Clearly, on the receive end, you receive one byte, which tells you how many characters to receive. Then, you receive that number of characters.

PaulS:
If you are getting a ⸮ in the String, then why on earth would you expect the String to change when you tell it to replace all ? with nothing?

If you are sending 25 characters, why are you asking for 32?

You seem to NOT understand just how masters and slaves are supposed to communicate, using I2C. If you need to send a variable amount of data from the master to the slave, you MUST do that in two steps. The first step is to send the amount of data (in binary, NOT ASCII). The second is to send the variable amount of data.

Clearly, on the receive end, you receive one byte, which tells you how many characters to receive. Then, you receive that number of characters.

Yes, you are right. I started to learn I2C protocol from yesterday, so...

Anyway, after that I figured out something, because my config is like this:

  1. Master receiver (reader)
  2. Slave transmitter (writer)

So, what I did:

First, I'm sending command from Master to Slave to get the size of the prepared data.
Second, Then pasting the received data byte size and requesting the data by size from the Slave.

Is that a better approach?

Is that a better approach?

Yes.

PaulS:
Yes.

Thank you! :wink: