Hi every body
I have a rm_g144 sensor and have a USB-I2C connection to connect it directly to the PC.
I’m using this converter robot-electronics.co.uk/htm/usb_i2c_tech.htm
I used below code and I received some Hex numbers I do not know they are true or not!
can you please check my code and give me some feed back
[code]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace ADXL345
{
public partial class ADXL345 : Form
{
static SerialPort USB_I2C;
public ADXL345()
{
InitializeComponent();
USB_I2C = new SerialPort();
// Get a list of serial port names.
foreach (string s in SerialPort.GetPortNames())
{
Ports.Items.Add(s);
}
Message.Text = "Select Com port";
}
}
private void Ports_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
byte] SerBuf = new byte[10];
USB_I2C.PortName = Ports.Text;
USB_I2C.Parity = 0;
USB_I2C.BaudRate = 19200;
USB_I2C.StopBits = StopBits.Two;
USB_I2C.DataBits = 8;
USB_I2C.ReadTimeout = 500;
USB_I2C.WriteTimeout = 500;
USB_I2C.Open();
SerBuf[0] = 0x5A; // USB_I2C Module Command
SerBuf[1] = 0x01; // Get Revision
SerBuf[2] = 0;
SerBuf[3] = 0;
USB_I2C.Write(SerBuf, 0, 4);
USB_I2C.Read(SerBuf, 0, 1);
USB_I2C_VER.Text = string.Format("{0}", SerBuf[0]);
timer1.Start();
Message.ResetText();
}
catch { }
}
private void ReadEEPROM()
{
byte] SerBuf = new byte[4];
SerBuf[0] = 0x5A; // Command for Devices with 1 address byte
SerBuf[1] = 0x10; // 24C02 I2C address + low bit set to indicate its a read operation
if (checkBox1.Checked)
SerBuf[2] = 0x0D; // 24C02 internal address to read from
else
SerBuf[2] = 0x0E;
SerBuf[3] = 0x00;
USB_I2C.Write(SerBuf, 0, 4); // send the read command to the USB-I2C module
USB_I2C.Read(SerBuf, 0, 1); // get the 8 data bytes
SerBuf[0] = 0x5A; // Command for Devices with 1 address byte
SerBuf[1] = 0x12; // 24C02 I2C address + low bit set to indicate its a read operation
SerBuf[2] = 0x00; // 24C02 internal address to read from
SerBuf[3] = 0x00;
USB_I2C.Write(SerBuf, 0, 4); // send the read command to the USB-I2C module
USB_I2C.Read(SerBuf, 0, 4); // get the 8 data bytes
Message.Text += string.Format("Hex: {0:X02}, {1:X02}, {2:X02}, {3:X02} Dec: {4}, {5}, {6}, {7} {8}", SerBuf[0], SerBuf[1], SerBuf[2], SerBuf[3], SerBuf[0], SerBuf[1], SerBuf[2], SerBuf[3], Environment.NewLine);
}
private void timer1_Tick(object sender, EventArgs e)
{
ReadEEPROM();
}
}
}[/code]