sending through serial to i2c

hi everyone, first time in the forum and first time with arduino..

I would like to convert the data coming through serial directly into a configuration to be used to configure the registers of an i2c device. so the code should work in this way:

sending through arduino serial monitor the following: 200208
....
converting
....
sending through i2c the following:

  Wire.beginTransmission(0x20);
  Wire.write(0x02); 
  Wire.write(0x08); 
  Wire.endTransmission();

the conversion is necessary because I can have different values to write in the registers, like:
20020A --> 0x20,0x02,0x0A
401210 --> 0x40,0x12,0x10
200D1B --> 0x20,0x0D,0x1B

how can I do with a code as simple as possible?

i think you need to capture the "bytes" of data from the serial interface into variables (an array), along with the amount of data and then use that data to write to the I2C bus. you may need to delimit (possibly a linefeed) each group of data

looks like each Serial "message" is 3 bytes: chip, addr, data. you

Have a look at the serial input basics tutorial. Example #5 shows how to receive and parse multiple data values. It may not look simple, but it is very reliable and does not block.

groundFungus:
Have a look at the [...
[/quote]

i take a look but i get lost a bit

i think i can get the arrray of the data

Serial.readBytesUntil('\n', buffer, BUFFER_LENGHT);

but my problem is how to place buffer inside wire.write

something like

wire.write(0x buffer[0] buffer[1]);

](Serial Input Basics - updated - Introductory Tutorials - Arduino Forum)

Why not:

  Wire.beginTransmission(buffer[0]);
  Wire.write(buffer[1]);
  Wire.write(buffer[2]);
  Wire.endTransmission();

?

  Wire.beginTransmission(buffer[0]);    //7-bit device address (lower 7-bit of byte-0 of received message)
  Wire.write(buffer[1]);                        //8-bit address of the internal register
  Wire.write(buffer[2]);                        //8-bit data for the above internal register
  Wire.endTransmission();                    //I2C Bus termination

it's not working

I try

  Wire.beginTransmission(buffer[0]);    //7-bit device address (lower 7-bit of byte-0 of received message)
  Wire.write(buffer[2]);                        //8-bit address of the internal register
  Wire.write(buffer[4]);                        //8-bit data for the above internal register
  Wire.endTransmission();                    //I2C Bus termination

note that i need both for get the HEX, but I can consider buffer[1] and buffer[3] always 0 ... by now

btw if I use

            Wire.beginTransmission(I2C_ADDRESS);
            Wire.write(0x02);
            Wire.write(0x02);           
            Wire.endTransmission();

works

if I use

            Wire.beginTransmission(I2C_ADDRESS);
            Wire.write(buffer[2]);
            Wire.write(buffer[4]);           
            Wire.endTransmission();  
            Serial.write(buffer[2]);  
            Serial.write(buffer[4]);

doesn't work, and i got a 22 on the arduino serial konitor

maybe i need to convert into HEX or BIN?

What data type is the buffer array? Should be byte because you are reading bytes.

Post the whole transmit and receive codes that you are using to test. Your snippets leave out too much of what we need to know.

What is the receiving device(s)?

You are receiving 3 bytes. Where are buffer[3] and buffer [4] coming from?

It does not matter if you send HEX, DEC or BIN, all data is sent as binary. HEX, DEC, BIN and char are all representations of the binary for humans to read.

sgraflo:
sending through arduino serial monitor the following: 200208

A: Given - 200208
==> 0x20 (byte-0) : device/I2C address
==> 0x02 (byte-1) : address of internal register
==> 0x08 (byte-2) : data for the internal regiater

B: Try these codes (with this setting of Serial Monitor (Fig-1): Line ending tab at 'Newline' option). You must enter data in the InputBox of Serial Monitor as quickly as possible as the default timeout period is 1 sec.
SerialMonitor.png
Figure-1:

char myData[20];
void loop()
{
    byte m = Serial.readBytesUntil('\n', myData, 20);  //receiving data frame from Serial Interface
    myData[m] = '\0';       //insert null character
    Serial.println(myData);    //Serial Monitor shows: 200208 (0x32, 0x30, 0x30, 0x32, 0x30, 0x38)
    //----------------------------------------
    byte i2cAddress = (myData[0]<<4)|myData[1]&0x0F;  //0x20 = 00100000
    byte regAddress = (myData[2]<<4)|myData[3]&0x0F; //0x02 = 00000010
    byte regData     = (myData[4]<<4)|myData[5]&0x0F; //0x08 = 00001000
    //----------------------------------------------------------------------------------
    Wire.beginTransmission(i2cAddress); //7-bit device address (lower 7-bit of byte-0 of received msg.)
    Wire.write(regAddress);                   //8-bit address of the internal register
    Wire.write(regData);                       //8-bit data for the above internal register
    Wire.endTransmission();                  //I2C Bus termination
}

SerialMonitor.png

GolamMostafa:

    //----------------------------------------

byte i2cAddress = (myData[0]<<4)|myData[1]&0x0F;  //0x20 = 00100000
    byte regAddress = (myData[2]<<4)|myData[3]&0x0F; //0x02 = 00000010
    byte regData    = (myData[4]<<4)|myData[5]&0x0F; //0x08 = 00001000

thanks for the reply, that was just what i was looking for!

I have to add a conversion from HEX in order to manage 0x0A

    if (buffer[i] >= 48 && buffer[i] <= 57) buffer[i] = map(buffer[i], 48, 57, 0, 9);
    if (buffer[i] >= 65 && buffer[i] <= 70) buffer[i] = map(buffer[i], 65, 70, 10, 15);

regards