Why always have 0xDE 0x02 0x02 at terminal front?

Hi,all
I make a I2C adapter,It can send data by PC serial command
That command like this:
Send: "Read:31,01,32\r\n" 31 is Device address, 01 is Register address, 32 is data length
Received: "00000000001111111111000000000011\DE\02\02\r\n"
Its weird thing why always apear DE0202 this character

master_05.ino (3.58 KB)

slave_05.ino (1.43 KB)

Which Arduino boards do you use ?

With a command in the serial monitor, 32 bytes are requested.
The Slave returns exactly 32 bytes.
The Master puts them in a buffer, which is exactly 32 bytes in size.
The 32 bytes are printed.
All this time there is no zero-terminator.

This is 32 characters: "00000000001111111111000000000011", but there is also a zero-terminator at the end. That is not transferred over the I2C bus and the Master does not add it.

I use Arduino Nano, I provide source code as following:
by the way ,Is "Wire.write()" char array size 256?

This is Master

#include<Wire.h>
//#include <stdlib.h>

String cmd;
String PC_DevAddr;
String PC_RegAddr;
String PC_DataLength;
String PC_Data;
byte Output_U8_1D[256];
void setup()
{
Wire.begin();//不指定 代表Master
Serial.begin(115200);
Serial.setTimeout(5);
//Serial.println("I2C Master started");
//Serial.println("Try to send:Read:31,01,50");//DevAdr,RegAdr,Data length

}
void loop()
{
if (Serial.available())
{
cmd= Serial.readString();
if(SearchStringBefore(cmd,"\r\n") == "Keyword not found!")
{
}
else
{
//Read:31,01,50

PC_DevAddr = SearchStringBefore(SearchStringAfter(cmd,"Read:"), "," );
PC_RegAddr = SearchStringBefore( SearchStringAfter(SearchStringAfter(cmd,"Read:"), "," ) , "," );
PC_DataLength = SearchStringBefore( SearchStringAfter( SearchStringAfter( SearchStringAfter(cmd,"Read:") , "," ) , "," ), "\r\n" );
/*Serial.println("PC Sent:");
Serial.println(cmd);
Serial.print("Dev Address:0x");
Serial.println(PC_DevAddr);
Serial.print("Reg Address:0x");
Serial.println(PC_RegAddr);
Serial.print("Data length:0d");
Serial.println(PC_DataLength.toInt());
*/
requestFromSlave(byte(HexStrToU16(PC_DevAddr)),byte(HexStrToU16(PC_RegAddr)),PC_DataLength.toInt());//DevAdr,RegAdr,Data length
}
}

//requestFromSlave(byte(HexStrToU16(PC_DevAddr)),byte(HexStrToU16(PC_RegAddr)),HexStrToU16(PC_DataLength));//DevAdr,RegAdr,Data length
//delay(1000);
}
void requestFromSlave(byte DEV_ADR ,byte REG_ADR ,int DATA_LENGTH)
{

char U8array[DATA_LENGTH] = "";
Wire.beginTransmission(DEV_ADR);
Wire.write(REG_ADR);
Wire.endTransmission();
Wire.requestFrom(DEV_ADR, DATA_LENGTH);
//for(byte i=0;i<DATA_LENGTH;i++)
for(byte i=0;i<DATA_LENGTH;i++)
{
U8array *= Wire.read(); *

  • }*

  • //Serial.print("Received DATA LENGTH:"); *

  • //Serial.println(sizeof(U8array)); *

  • Serial.println(U8array); *
    }
    //搜尋字串後面
    String SearchStringAfter(String Source,String Keyword)
    {

  • int SearchOutIndex; *

  • int Keyword_length;*

  • SearchOutIndex = Source.indexOf(Keyword);*

  • Keyword_length = Keyword.length();*

  • if(SearchOutIndex == -1)*

  • {*

  • return "Keyword not found!";*

  • }*

  • else*

  • {*

  • return Source.substring(SearchOutIndex+Keyword.length());*

  • } *
    }
    //搜尋字串前面
    String SearchStringBefore(String Source,String Keyword)
    {

  • int SearchOutIndex; *

  • int Keyword_length;*

  • SearchOutIndex = Source.indexOf(Keyword);*

  • Keyword_length = Keyword.length();*

  • if(SearchOutIndex == -1)*

  • {*

  • return "Keyword not found!";*

  • }*

  • else*

  • { *

  • return Source.substring(0,SearchOutIndex);*

  • } *
    }
    //Hex字串轉U16數字
    *unsigned int HexStrToU16(String hexString) *
    {

  • unsigned int decValue = 0;*

  • int nextInt;*

  • for (int i = 0; i < hexString.length(); i++) {*

  • nextInt = int(hexString.charAt(i));*

  • if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);*

  • if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);*

  • if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);*

  • nextInt = constrain(nextInt, 0, 15);*

_ decValue = (decValue * 16) + nextInt;_

  • }*

  • return decValue;*
    }
    //Hex字串轉U8 array
    int HexStrToU8array(String hexString)
    {

  • int hexStringLen=0;*

  • hexStringLen = hexString.length();*

  • hexStringLen = hexStringLen/2; *

  • for(int i=0;i<hexStringLen;i++)*

  • { *
    Output_U8_1D = byte( HexStrToU16( hexString.substring(i2,i2+2) ) );
    * }*
    }
    This is slave
    #include<Wire.h>
    #define I2C_SLAVE_ADDRESS 0x31
    byte REG_ADR;
    void setup()
    {
    * ackToMaster(0x31); *
    }
    void loop()
    {
    }
    void ackToMaster(byte DEV_ADR)
    {
    * Wire.begin(DEV_ADR);//指定Address,代表Slave模式*
    * Wire.onReceive(receiveEvent);//取得Reg Address*
    * delay(0);*
    * Wire.onRequest(requestEvent);//回饋Reg Address內容*
    * delay(0);*
    }
    void receiveEvent(int howMany)
    {
    * REG_ADR = Wire.read();
    //Serial.println(REG_ADR);
    _
    }_
    void requestEvent()
    _
    {_
    byte Data_0x01[30] = {0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43,0x43};
    byte Data_0x42[] = {0b11000011,0b00011101, 0xFF, 0xFF, 0xFF ,0xFF, 0x00, 0x00};
    _
    // 0-7 8-15 16-23 24-31 32-39 40-47 48-55 56-63*_
    _ /_
    _
    StaticGridSts(0)_
    _
    DynamicGridSts(1) _
    _
    InternalErrorStatus(6)_
    Communications_Timeout(7)
    _
    ExternalErrorStatus(8)_
    _
    ZoomViewResp(10-12)_
    _
    Data(16-47)_
    _ /_
    switch(REG_ADR)

    * {*
    * case 0x00:*
    * Wire.write("Hello", 8);*
    * break;*
    * case 0x01:*
    * Wire.write("00000000001111111111000000000011",32);*
    * break;*
    * case 0x08:*
    * Wire.write("this is ", 9);*
    * break;*
    * case 0x10:*
    * Wire.write("I2C test", 8);*
    * break;*
    * case 0x42:*
    * Wire.write(Data_0x42, 8);
    _
    break;_
    _
    }_
    _
    }*_

For a Nano it is maximum 32 bytes: Buffer size of the Wire library · Koepel/How-to-use-the-Arduino-Wire-library Wiki · GitHub.

You still have not added a zero-terminator in the Master.

Koepel:
For a Nano it is maximum 32 bytes: Buffer size of the Wire library · Koepel/How-to-use-the-Arduino-Wire-library Wiki · GitHub.

You still have not added a zero-terminator in the Master.

Thanks Koepel replied, I had knew that limited
I already remove Serial.println(U8array); -> Serial.print(U8array);
But still happened this weird string at terminal as "00000000001111111111000000000011⸮ "

The Serial.println() or Serial.print() prints as many characters as you want. It never stops printing, unless it finds the end of a string. The end of a string is zero-terminator.
Do you know what a zero-terminator is ?

Koepel:
The Serial.println() or Serial.print() prints as many characters as you want. It never stops printing, unless it finds the end of a string. The end of a string is zero-terminator.
Do you know what a zero-terminator is ?

Are you means that DE 02 02 is zero-terminator?

Null-terminated string: Null-terminated string - Wikipedia.
The "zero-terminator" or the "null-terminator" in the C++ language is written as the character '\0', but it is zero, all bits are 0.
That is basic 'C' language knowledge.

Koepel:
Null-terminated string: Null-terminated string - Wikipedia.
The "zero-terminator" or the "null-terminator" in the C++ language is written as the character '\0', but it is zero, all bits are 0.
That is basic 'C' language knowledge.

Thank you provide that information to me.I learned
Is Null-terminated string have some relate with 0xDE 0x02 0x02?

No, those are just bytes that are located in memory beyond the array. A zero-terminator is 0x00.

Koepel:
No, those are just bytes that are located in memory beyond the array. A zero-terminator is 0x00.

All rights, I am not sure what is that problem ,Is from Master or Slave?

The problem is when printing a string.
Since 32 bytes is the maximum, the Slave can not add a zero-terminator to that. So the Master has to fix it. Be sure that the array is big enough.

I can turn it around. Can you explain that when an array of 32 byte is printed, how does Serial.println() know that exactly 32 bytes should be printed ? Where does Serial.println() get that information from ?
The 'C' and 'C++' language are basic languages, the compiler does not automatically do things, the compiler does only what you tell it to do.

int n = DATA_LENGTH + 1;     // add one for zero-terminator
char U8array[n];

Wire.beginTransmission( DEV_ADR);
Wire.write( REG_ADR);
Wire.endTransmission();

Wire.requestFrom( DEV_ADR, n - 1);
for( int i=0; i<n-1; i++)
{
  U8array = Wire.read();   
}
U8array[n - 1] = '\0';  // put zero-terminator at the last position, after the data

Serial.println( U8array);

Koepel:

int n = DATA_LENGTH + 1;     // add one for zero-terminator

char U8array[n];

Wire.beginTransmission( DEV_ADR);
Wire.write( REG_ADR);
Wire.endTransmission();

Wire.requestFrom( DEV_ADR, n - 1);
for( int i=0; i<n-1; i++)
{
  U8array = Wire.read(); 
}
U8array[n - 1] = '\0';  // put zero-terminator at the last position, after the data

Serial.println( U8array);

Thanks Koepel, I had modify that code and It don't have any weird symbol thanks!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.