1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Tango.PMR.Stubs;
using Tango.Stubs;
//----------------------
const Int32 I2C_ID = 2;
//const Int32 I2C_Slave_Add = 0x46; //must be 0x40, 0x44 or 0x46
//----------------------
private const string FILE_PATH = "D:\\MidTankPressurelog.txt";
int adc_configuration(uint I2C_Slave_Add,uint channel )
{
StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
stubI2CWriteBytesRequest.I2CId = I2C_ID;
stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
UInt32 uInt32 = new UInt32();
stubI2CWriteBytesRequest.BytesTWrite.Add(0x00);//Byte 0 to write
stubI2CWriteBytesRequest.BytesTWrite.Add(channel);//Byte 1 to Write, must be 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02 or 0x01 for different channels
stubI2CWriteBytesRequest.BytesTWrite.Add(0x80);//Byte 2 to Write
var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
return 1;
}
int adc_set_for_read_temp(uint I2C_Slave_Add)
{
StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
stubI2CWriteBytesRequest.I2CId = I2C_ID;
stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
UInt32 uInt32 = new UInt32();
stubI2CWriteBytesRequest.BytesTWrite.Add(0x02);//Byte 0 to write
var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
return 1;
}
int adc_read_temp(uint I2C_Slave_Add)
{
StubI2CReadBytesRequest stubI2CReadBytesRequest = new StubI2CReadBytesRequest();
stubI2CReadBytesRequest.I2CId = I2C_ID;
stubI2CReadBytesRequest.SlaveAddress = I2C_Slave_Add;
stubI2CReadBytesRequest.NumberOfBytesToRead = 2; // Number of bytes to read
var response1 = stubManager.Run<StubI2CReadBytesResponse>(stubI2CReadBytesRequest);
for(int i=0; i<stubI2CReadBytesRequest.NumberOfBytesToRead;i++)
{
stubManager.WriteLineHex(response1.ReadBytes[i],2);
}
return 1;
}
int adc_set_for_read_ch(uint I2C_Slave_Add)
{
StubI2CWriteBytesRequest stubI2CWriteBytesRequest = new StubI2CWriteBytesRequest();
stubI2CWriteBytesRequest.I2CId = I2C_ID;
stubI2CWriteBytesRequest.SlaveAddress = I2C_Slave_Add;
UInt32 uInt32 = new UInt32();
stubI2CWriteBytesRequest.BytesTWrite.Add(0x01);//Byte 0 to write
var response = stubManager.Run<StubI2CWriteBytesResponse>(stubI2CWriteBytesRequest);
return 1;
}
int adc_read_ch(uint ID, uint I2C_Slave_Add)
{
StubI2CReadBytesRequest stubI2CReadBytesRequest = new StubI2CReadBytesRequest();
stubI2CReadBytesRequest.I2CId = I2C_ID;
stubI2CReadBytesRequest.SlaveAddress = I2C_Slave_Add;
stubI2CReadBytesRequest.NumberOfBytesToRead = 2; // Number of bytes to read
var response1 = stubManager.Run<StubI2CReadBytesResponse>(stubI2CReadBytesRequest);
uint temph=0;
uint templ=0;
uint temp;
double calc;
double calc1;
double calc2;
double calc3;
double calc4;
for(int i=0; i<stubI2CReadBytesRequest.NumberOfBytesToRead;i=i+2)
{
temph=response1.ReadBytes[i];
templ=response1.ReadBytes[i+1];
temp = (temph << 8) | templ;
temp=temp & 0x0fff;
stubManager.WriteHex(temp,4);
calc = (double)temp *2.5 / 4096.0;
stubManager.Write("ADC [volts]: ");
stubManager.WriteLine(calc);
calc1 = (1.96- calc) * 4.64 + 1.96;
stubManager.Write("Vsensor [volts]: ");
stubManager.WriteLine(calc1);
calc2 = ((calc1/5)-0.5)/(0.16/1);
stubManager.Write("Pressure [pka]: ");
stubManager.WriteLine(calc2);
calc3 = (calc2 * 1000.0) / (1060.0 *9.81);
stubManager.Write("Liquid High [meter]: ");
stubManager.WriteLine(calc3);
calc4 = 3.1415*((0.11 - 0.0126) / 2.0) * ((0.11 - 0.0126) / 2.0) * calc3 * 1000.0;
stubManager.Write("Liquid volume [litter]: ");
stubManager.WriteLine(calc4);
stubManager.AppendToFile(FILE_PATH,ID.ToString()+ " "+temp.ToString("X4") + " "+calc.ToString("F4")+ " "+ calc1.ToString("F4")+ " "+calc2.ToString("F4")+ " "+calc3.ToString("F4")+ " "+calc4.ToString("F4"));
}
return 1;
}
public void OnExecute(StubManager stubManager)
{
stubManager.WriteToFile(FILE_PATH, "\n-----------MIDTANK_PRESSESENS----------- \n\n");
stubManager.AppendToFile(FILE_PATH, "ID ADC[bits] ADC[volts] Vsensor[volts] Pressure[pka] Liquid High[meter] Liquid volume[litter]");
//for (int i = 0; i<3000; i++)
while(true)
{
stubManager.Write("\n-----------MIDTANK_PRESSESENS----------- ");
stubManager.Write("\n1_1= ");
adc_configuration(0x44,0x20); //a2d_address 0x44 channel channel 2
adc_set_for_read_ch(0x44);
adc_read_ch(1,0x44);
stubManager.Write("\n1_2= ");
adc_configuration(0x44,0x10); //a2d_address 0x44 channel channel 3
adc_set_for_read_ch(0x44);
adc_read_ch(5,0x44);
stubManager.Write("\n2_1= ");
adc_configuration(0x44,0x08); //a2d_address 0x44 channel channel 4
adc_set_for_read_ch(0x44);
adc_read_ch(2,0x44);
stubManager.Write("\n2_2= ");
adc_configuration(0x44,0x04); //a2d_address 0x44 channel channel 5
adc_set_for_read_ch(0x44);
adc_read_ch(6,0x44);
stubManager.Write("\n3_1= ");
adc_configuration(0x46,0x80); //a2d_address 0x46 channel channel 0
adc_set_for_read_ch(0x46);
adc_read_ch(3,0x46);
stubManager.Write("\n3_2= ");
adc_configuration(0x46,0x40); //a2d_address 0x46 channel channel 1
adc_set_for_read_ch(0x46);
adc_read_ch(7,0x46);
stubManager.Write("\n4_1= ");
adc_configuration(0x46,0x20); //a2d_address 0x46 channel channel 2
adc_set_for_read_ch(0x46);
adc_read_ch(4,0x46);
stubManager.Write("\n4_2= ");
adc_configuration(0x46,0x10); //a2d_address 0x46 channel channel 3
adc_set_for_read_ch(0x46);
adc_read_ch(8,0x46);
stubManager.AppendToFile(FILE_PATH, "" );
Thread.Sleep(5000);
}
}
|