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:
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
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.
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
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
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.
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
}