Commit 46f56166 authored by reza's avatar reza

second version

parent eaedc3d8
......@@ -26,14 +26,12 @@ namespace I8Devices
}
}
catch (Exception e)
catch (Exception)
{
// Console.WriteLine(e);
// Console.WriteLine("fuck3");
// Console.WriteLine(e);
}
}
return suspect_coms;
// return null;
}
static ManagementObject[] FindPorts()
......@@ -51,8 +49,7 @@ namespace I8Devices
}
catch (Exception e)
{
// Console.WriteLine(e);
// Console.WriteLine("fuck1");
Console.WriteLine(e);
return new ManagementObject[] { };
}
}
......@@ -72,80 +69,199 @@ namespace I8Devices
}
}
public class Device
public class Settings
{
static int AnswerToInitFlag = 0;
public bool test_signal { get; set; }
private int _sampling_rate = 250;
public int sampling_rate
{
get => _sampling_rate;
set
{
if (value <= 250) _sampling_rate = 250;
else if ((value > 250) && (value <= 500)) _sampling_rate = 500;
else if ((value > 500) && (value <= 1000)) _sampling_rate = 1000;
else if ((value > 1000)) _sampling_rate = 2000;
}
}
private int _gain = 250;
public int gain
{
get => _gain;
set
{
if (value <= 1) _gain = 1;
else if ((value > 1) && (value <= 2)) _gain = 2;
else if ((value > 2) && (value <= 4)) _gain = 4;
else if ((value > 4) && (value <= 6)) _gain = 6;
else if ((value > 6) && (value <= 8)) _gain = 8;
else if ((value > 8) && (value <= 12)) _gain = 12;
else if ((value > 12)) _gain = 24;
}
}
public bool[] channels_on = new bool[8] { true, true, true, true, true, true, true, true };
public Settings()
{
test_signal = false;
sampling_rate = 2000;
gain = 24;
}
}
public class Device
{
private static string device_name = "No Device Found";
private static int sample_count = 0;
private static string firmware_version = "No Data";
private static string unic_ID = "No Data";
private static string flash_data = "No Data";
static SerialPort _serialPort;
static byte[] rec_data = new byte[32];
static byte[] last_data_recieve = new byte[74];
static int[] last_data_recieve_int = new int[74];
static double[] last_data_converted = new double[24];
static int rec_cnt = 0;
static System.String begin_command = "U8_BT8 V";
static System.String reset_command = "U8_RT8 V";
static byte[] SendBuffer = new byte[32];
static string name, device_com_port;
static IList com_names = new ArrayList();
bool DataStopFlag = false;
static bool newData_flag = false;
static int cnt = 0;
public static void ConnectToDevice() //string device_name
static List<double[]> receiveBuffer = new List<double[]>() { };
static byte[] sendBuffer = new byte[32];
static bool dataGathering_flag = false;
static bool answerToInit_flag = false;
static bool settingMode_flag = false;
static int newData_count = 0;
public static bool debug_mode = false;
public bool writeSetting(Settings mysetting)
{
int bias_derivation = 0;
settingMode_flag = true;
if (debug_mode) Console.WriteLine("DLL: Sampling rate= " + mysetting.sampling_rate);
if (debug_mode) Console.WriteLine("DLL: Test Mode= " + mysetting.test_signal);
if (debug_mode) Console.WriteLine("DLL: Gain= " + mysetting.gain);
if (debug_mode) Console.Write("DLL: Channels On/Off=");
foreach (var chon in mysetting.channels_on)
{
SendBuffer[0] = Convert.ToByte('U');
SendBuffer[1] = Convert.ToByte('8');
SendBuffer[2] = Convert.ToByte('_');
SendBuffer[3] = Convert.ToByte('S');
SendBuffer[4] = Convert.ToByte('T');
SendBuffer[5] = Convert.ToByte('8');
SendBuffer[6] = Convert.ToByte(' ');
SendBuffer[7] = Convert.ToByte('C'); // SRB1 connected to all Nchs
SendBuffer[8] = 0x01; //Bias Derivation to 3-0 Pchs 0xD Register ADS
SendBuffer[9] = 0x03; //Bias Derivation to 7-4 Pchs 0xE Register ADS
SendBuffer[10] = 0x0E; //Bias Derivation to 3-0 Nchs 0xD Register ADS
SendBuffer[11] = 0x0D; //Bias Derivation to 7-4 Nchs 0xE Register ADS
SendBuffer[12] = 0x60; //Channel 1 Setting
SendBuffer[13] = 0x60; //Channel 2 Setting
SendBuffer[14] = 0x60; //Channel 3 Setting
SendBuffer[15] = 0x60; //Channel 4 Setting
SendBuffer[16] = 0x60; //Channel 5 Setting
SendBuffer[17] = 0x60; //Channel 6 Setting
SendBuffer[18] = 0x60; //Channel 7 Setting
SendBuffer[19] = 0x60; //Channel 8 Setting
SendBuffer[20] = Convert.ToByte(' ');
SendBuffer[21] = Convert.ToByte(' ');
SendBuffer[22] = Convert.ToByte(' ');
SendBuffer[23] = Convert.ToByte(' ');
SendBuffer[24] = Convert.ToByte(' ');
SendBuffer[25] = Convert.ToByte(' ');
SendBuffer[26] = Convert.ToByte(' ');
SendBuffer[27] = Convert.ToByte(' ');
SendBuffer[28] = Convert.ToByte(' ');
SendBuffer[29] = Convert.ToByte(' ');
SendBuffer[30] = Convert.ToByte(' ');
SendBuffer[31] = Convert.ToByte('V');
if (debug_mode) Console.Write(" " + chon);
}
if (debug_mode) Console.WriteLine("");
bool ATR_flag = false;
sendBuffer[3] = Convert.ToByte('S'); // for enter in setting mode
if (mysetting.test_signal) sendBuffer[4] = Convert.ToByte('T');
else sendBuffer[4] = Convert.ToByte('n');
sendBuffer[5] = Convert.ToByte(48+ (mysetting.sampling_rate/250)); // '1'=250, '2'=500, '4'=1000, '8'=2000
sendBuffer[6] = Convert.ToByte(' '); // disable lead-off (enable = 'L')
sendBuffer[7] = Convert.ToByte('C'); // SRB1 connected to all Nchs
sendBuffer[8] = 0x01; //Bias Derivation to 3-0 Pchs 0xD Register ADS
sendBuffer[9] = 0x03; //Bias Derivation to 7-4 Pchs 0xE Register ADS
sendBuffer[10] = 0x0E; //Bias Derivation to 3-0 Nchs 0xD Register ADS
sendBuffer[11] = 0x0D; //Bias Derivation to 7-4 Nchs 0xE Register ADS
sendBuffer[12] = 0x60; //Channel 1 Setting
sendBuffer[13] = 0x60; //Channel 2 Setting
sendBuffer[14] = 0x60; //Channel 3 Setting
sendBuffer[15] = 0x60; //Channel 4 Setting
sendBuffer[16] = 0x60; //Channel 5 Setting
sendBuffer[17] = 0x60; //Channel 6 Setting
sendBuffer[18] = 0x60; //Channel 7 Setting
sendBuffer[19] = 0x60; //Channel 8 Setting
for (int i = 0; i < mysetting.channels_on.Length; i++)
{
if (mysetting.channels_on[i])
{
if (mysetting.gain == 1) sendBuffer[i + 12] = 0x00;
else if (mysetting.gain == 2) sendBuffer[i + 12] = 0x10;
else if (mysetting.gain == 4) sendBuffer[i + 12] = 0x20;
else if (mysetting.gain == 6) sendBuffer[i + 12] = 0x30;
else if (mysetting.gain == 8) sendBuffer[i + 12] = 0x40;
else if (mysetting.gain == 12) sendBuffer[i + 12] = 0x50;
else if (mysetting.gain == 24) sendBuffer[i + 12] = 0x60;
bias_derivation = bias_derivation + (int)Math.Pow(2, i);
// if (debug_mode) Console.WriteLine("DLL: powe= " + (int)Math.Pow(2, i));
}
else
{
sendBuffer[i + 12] = 0x81; // Channel power-down + Input shorted
}
}
if (debug_mode) Console.WriteLine("DLL: bias_derivation On/Off= " + bias_derivation.ToString("X"));
sendBuffer[8] = (byte)(bias_derivation % 16); //Bias Derivation to 3-0 Pchs 0xD Register ADS
sendBuffer[9] = (byte)(bias_derivation / 16); //Bias Derivation to 7-4 Pchs 0xE Register ADS
sendBuffer[10] = (byte)(bias_derivation % 16); //Bias Derivation to 3-0 Nchs 0xD Register ADS
sendBuffer[11] = (byte)(bias_derivation / 16); //Bias Derivation to 7-4 Nchs 0xE Register ADS
_serialPort.Open();
//_serialPort.Write(Encoding.UTF8.GetString(sendBuffer, 0, sendBuffer.Length));
_serialPort.Write(sendBuffer, 0, sendBuffer.Length);
try
{
if (debug_mode) Console.WriteLine("DLL: Waiting for device to send back acknowledge about write setting ...");
while (settingMode_flag) ;
_serialPort.Close();
return true;
}
catch (Exception)
{
return false;
};
}
public bool connect() //string device_name
{
if (dataGathering_flag) return false;
{
sendBuffer[0] = Convert.ToByte('U');
sendBuffer[1] = Convert.ToByte('8');
sendBuffer[2] = Convert.ToByte('_');
sendBuffer[3] = Convert.ToByte('S');
sendBuffer[4] = Convert.ToByte('T');
sendBuffer[5] = Convert.ToByte('8');
sendBuffer[6] = Convert.ToByte(' ');
sendBuffer[7] = Convert.ToByte('C'); // SRB1 connected to all Nchs
sendBuffer[8] = 0x01; //Bias Derivation to 3-0 Pchs 0xD Register ADS
sendBuffer[9] = 0x03; //Bias Derivation to 7-4 Pchs 0xE Register ADS
sendBuffer[10] = 0x0E; //Bias Derivation to 3-0 Nchs 0xD Register ADS
sendBuffer[11] = 0x0D; //Bias Derivation to 7-4 Nchs 0xE Register ADS
sendBuffer[12] = 0x60; //Channel 1 Setting
sendBuffer[13] = 0x60; //Channel 2 Setting
sendBuffer[14] = 0x60; //Channel 3 Setting
sendBuffer[15] = 0x60; //Channel 4 Setting
sendBuffer[16] = 0x60; //Channel 5 Setting
sendBuffer[17] = 0x60; //Channel 6 Setting
sendBuffer[18] = 0x60; //Channel 7 Setting
sendBuffer[19] = 0x60; //Channel 8 Setting
sendBuffer[20] = Convert.ToByte(' ');
sendBuffer[21] = Convert.ToByte(' ');
sendBuffer[22] = Convert.ToByte(' ');
sendBuffer[23] = Convert.ToByte(' ');
sendBuffer[24] = Convert.ToByte(' ');
sendBuffer[25] = Convert.ToByte(' ');
sendBuffer[26] = Convert.ToByte(' ');
sendBuffer[27] = Convert.ToByte(' ');
sendBuffer[28] = Convert.ToByte(' ');
sendBuffer[29] = Convert.ToByte(' ');
sendBuffer[30] = Convert.ToByte(' ');
sendBuffer[31] = Convert.ToByte('V');
}
bool ATR_flag = false;
COMPORTS device_coms = new COMPORTS();
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
//name = device_coms.FindPortByDesc("USB Serial Device");
IList com_names = new ArrayList();
com_names = device_coms.FindPortByDesc("USB Serial Device");
if (debug_mode) Console.WriteLine("DLL: Checking all COM ports ...");
if (com_names.Count > 0)
{
foreach (string item in com_names)
{
Console.WriteLine("cheking COM port " + item);
device_com_port = item;
_serialPort = new SerialPort(item);
_serialPort.ReadTimeout = 2000;
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
Console.WriteLine("send a reset command to device");
SendBuffer[3] = Convert.ToByte('R'); //reset the device
if (debug_mode) Console.WriteLine("DLL: Send a reset command to device");
sendBuffer[3] = Convert.ToByte('R'); //reset the device
_serialPort.Open();
_serialPort.Write(Encoding.UTF8.GetString(SendBuffer, 0, SendBuffer.Length));
_serialPort.Write(Encoding.UTF8.GetString(sendBuffer, 0, sendBuffer.Length));
try { _serialPort.Close(); }
catch (Exception) { };
......@@ -156,134 +272,298 @@ namespace I8Devices
_serialPort.Open();
ATR_flag = true;
}
catch (Exception)
catch (Exception )
{
Thread.Sleep(1000);
if (debug_mode) Console.WriteLine("DLL: Waiting 1s for device with "+ item + " to restart...");
//if (debug_mode) Console.WriteLine(e);
ATR_flag = false;
}
}
Console.WriteLine("sending handshake setting to device");
SendBuffer[3] = Convert.ToByte('S'); //setting mode
_serialPort.Write(Encoding.UTF8.GetString(SendBuffer, 0, SendBuffer.Length));
Console.WriteLine("waiting for device to respond...");
if (debug_mode) Console.WriteLine("DLL: Sending handshake setting to device");
sendBuffer[3] = Convert.ToByte('S'); //setting mode
_serialPort.Write(Encoding.UTF8.GetString(sendBuffer, 0, sendBuffer.Length));
if (debug_mode) Console.WriteLine("DLL: Waiting for device to respond...");
// Console.WriteLine("Press any key to continue...");
// Console.WriteLine();
// message = Console.ReadLine();
ulong i = 0;
while (AnswerToInitFlag == 0 && i < 2000000000) i++;
//ulong i = 0;
//while (answerToInit_flag == false && i < 2000000000) i++; //wait for device to respond and call DataReceivedHandler to enable AnswerToInitFlag
while (answerToInit_flag == false); //wait for device to respond and call DataReceivedHandler to enable AnswerToInitFlag
_serialPort.Close();
if (AnswerToInitFlag == 0)
Console.WriteLine("No respond detected!");
if (answerToInit_flag == false)
{
if (debug_mode) Console.WriteLine("DLL: No respond detected!");
return false;
}
else
{
if (debug_mode) Console.WriteLine("DLL: Device " + device_name + " is Connected on " + item);
return true;
}
}
if (debug_mode) Console.WriteLine("DLL: No I8Devices found!");
return false;
}
else
Console.WriteLine("No I8Devices found!");
{
if (debug_mode) Console.WriteLine("DLL: No COM port found!");
return false;
}
}
public double[] get_data()
public object getInfo(string val="all")
{
while (newData_flag == false) ;
newData_flag = false;
for (int k = 0; k < 24; k++)
{
last_data_converted[k] = (65536 * last_data_recieve[3 * k + 0]) + (256 * last_data_recieve[3 * k + 1]) + last_data_recieve[3 * k + 2];
if (last_data_converted[k] > 8388607) last_data_converted[k] = last_data_converted[k] - 16777216;
last_data_converted[k] = last_data_converted[k] * 0.536;
if (dataGathering_flag) return false;
if (debug_mode) Console.WriteLine("DLL: device_name(name): " + device_name);
if (debug_mode) Console.WriteLine("DLL: firmware_version(version): " + firmware_version);
if (debug_mode) Console.WriteLine("DLL: unic_ID(id): " + unic_ID);
if (debug_mode) Console.WriteLine("DLL: flash_data(flash): " + flash_data);
if (val == "name") return device_name;
if (val == "version") return firmware_version;
if (val == "id") return unic_ID;
if (val == "flash") return flash_data;
if (val == "all")
{
Dictionary<string, string> infoList = new Dictionary<string, string>();
infoList.Add("name", device_name);
infoList.Add("version", firmware_version);
infoList.Add("id", unic_ID);
infoList.Add("flash", flash_data);
return infoList;
}
return last_data_converted;
return false;
}
public void StartDataAcquisition()
public object getStatus(string val="all")
{
_serialPort.Open();
SendBuffer[3] = Convert.ToByte('B'); //setting mode
_serialPort.Write(Encoding.UTF8.GetString(SendBuffer, 0, SendBuffer.Length));
//Console.ReadLine();
//while (DataStopFlag == false) ;
// SendBuffer[3] = Convert.ToByte('E'); //setting mode
// _serialPort.Write(Encoding.UTF8.GetString(SendBuffer, 0, SendBuffer.Length));
// _serialPort.Close();
// Console.WriteLine(device_com_port);
// Console.WriteLine(_serialPort.PortName);
// Console.WriteLine(_serialPort.IsOpen);
// _serialPort.Open();
// _serialPort.Write(begin_command);
if (debug_mode) Console.WriteLine("DLL: Gathering Mode(mode): " + dataGathering_flag.ToString());
if (debug_mode) Console.WriteLine("DLL: sample_count(cnt): " + sample_count.ToString());
if (debug_mode) Console.WriteLine("DLL: Buffer Length(buff_len): " + newData_count.ToString());
if (val == "mode") return dataGathering_flag;
if (val == "cnt") return sample_count;
if (val == "buff_len") return newData_count;
if (val == "all")
{
Dictionary<string, object> status = new Dictionary<string, object>();
status.Add("mode", dataGathering_flag);
status.Add("cnt", sample_count);
status.Add("buff_len", newData_count);
return status;
}
if (debug_mode) Console.WriteLine("DLL: Not a valid input for getStatus");
return "nothing to respond";
}
public void StoptDataAcquisition()
public bool start()
{
if (device_name != "No Device Found")
{
if (!dataGathering_flag)
{
receiveBuffer.Clear();
newData_count = 0;
dataGathering_flag = true;
if (debug_mode) Console.WriteLine("DLL: Starting data gathering ...");
sample_count = 0;
_serialPort.Open();
sendBuffer[3] = Convert.ToByte('B'); //start mode
_serialPort.Write(Encoding.UTF8.GetString(sendBuffer, 0, sendBuffer.Length));
return true;
}
if (debug_mode) Console.WriteLine("DLL: The device is already in data gathering mode!");
return false;
}
if (debug_mode) Console.WriteLine("DLL: No device connected to start data gathering!");
return false;
}
public int stop()
{
SendBuffer[3] = Convert.ToByte('E'); //setting mode
_serialPort.Write(Encoding.UTF8.GetString(SendBuffer, 0, SendBuffer.Length));
_serialPort.Close();
DataStopFlag = true;
if (device_name != "No Device Found")
{
if (dataGathering_flag)
{
while (sample_count < 10);
dataGathering_flag = false;
sendBuffer[3] = Convert.ToByte('E'); //stop mode
_serialPort.Write(Encoding.UTF8.GetString(sendBuffer, 0, sendBuffer.Length));
_serialPort.Close();
if (debug_mode) Console.WriteLine("DLL: Data gathering has been finished.");
return sample_count;
}
if (debug_mode) Console.WriteLine("DLL: The device was not in data gathering mode to stop it, please call 'start' methode first.");
return -1;
}
if (debug_mode) Console.WriteLine("DLL: No device connected! Data gathering will remain unfinished :(");
return -2;
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
ulong ii = 0;
// SerialPort sp = (SerialPort)sender;
// string indata = sp.ReadExisting();
if (AnswerToInitFlag != 1)
if (!answerToInit_flag)
{
rec_cnt = _serialPort.Read(rec_data, 0, 32);
for (int i = 0; i < 32; i++)
byte[] rec_data = new byte[32];
_serialPort.Read(rec_data, 0, 32);
if (Convert.ToByte(rec_data[31]) == 144) // Indicate I8Device ID
{
// Console.WriteLine(Convert.ToByte(rec_data[i]));
Thread.Sleep(500);
if (Convert.ToByte(rec_data[27]) == 48) // Indicate Fascin8 device
{
device_name = "Fascin8";
firmware_version = Convert.ToByte(rec_data[24]).ToString();
unic_ID = rec_data[25].ToString() + rec_data[26].ToString();
flash_data = rec_data[28].ToString() + rec_data[29].ToString() + rec_data[30].ToString();
}
else if (Convert.ToByte(rec_data[27]) == 47) // Indicate Ultim8 device
{
device_name = "Ultim8";
firmware_version = Convert.ToByte(rec_data[24]).ToString();
unic_ID = rec_data[25].ToString() + rec_data[26].ToString();
flash_data = rec_data[28].ToString() + rec_data[29].ToString() + rec_data[30].ToString();
}
answerToInit_flag = true;
}
if (Convert.ToByte(rec_data[31]) == 144)
}
else if (settingMode_flag)
{
byte[] rec_data = new byte[32];
_serialPort.Read(rec_data, 0, 32);
if (Convert.ToByte(rec_data[31]) == 144) // Indicate I8Device ID
{
if (Convert.ToByte(rec_data[27]) == 48)
Thread.Sleep(500);
if (Convert.ToByte(rec_data[27]) == 48) // Indicate Fascin8 device
{
ulong i = 0;
// Console.WriteLine("Device Fasin8 is Connected on COM Port" + ' ' + device_com_port);
while (i < 700000000) i++;
Console.WriteLine("");
Console.WriteLine("Device Fasin8 is Connected on COM Port" + ' ' + device_com_port);
if (debug_mode) Console.WriteLine("DLL: Fascin8 writing setiing done succesfully.");
}
else if (Convert.ToByte(rec_data[27]) == 47)
else if (Convert.ToByte(rec_data[27]) == 47) // Indicate Ultim8 device
{
Console.WriteLine("");
Console.Write("Device Ultim8 is Connected on COM Port" + ' ' + device_com_port);
if (debug_mode) Console.WriteLine("DLL: Ultim8 writing setiing done succesfully.");
if (debug_mode)
{
// var sb = new StringBuilder("new byte[] { ");
int c = 0;
foreach (var b in rec_data)
{
//sb.Append(b + ", ");
Console.WriteLine(c.ToString("X") + "- " + ((byte)b).ToString("X"));
c++; if (c > 23) break;
}
// sb.Append("}");
// Console.WriteLine(sb.ToString());
}
}
else
Console.WriteLine("no valid response detected from" + _serialPort.PortName);
AnswerToInitFlag = 1;
settingMode_flag = false;
}
}
else
{
// for (int j = 0; j < 74; j++)
// {
// while (ii < 200000) ii++;
if (_serialPort.IsOpen && _serialPort.BytesToRead > 73)
if (device_name == "Fascin8" && _serialPort.IsOpen && _serialPort.BytesToRead > 73)
{
// last_data_recieve_int[j] = _serialPort.ReadByte();
rec_cnt = _serialPort.Read(last_data_recieve, 0, 74);
newData_flag = true;
cnt++;
Console.WriteLine(cnt);
byte[] last_data_recieve = new byte[74];
_serialPort.Read(last_data_recieve, 0, 74);
sample_count++;
double[] data_converted = new double[25];
data_converted[0] = sample_count;
int i = 1;
for (int k = i; k < 24 + i; k++)
{
data_converted[k] = (65536 * last_data_recieve[3 * (k - i) + 0]) + (256 * last_data_recieve[3 * (k - i) + 1]) + last_data_recieve[3 * (k - i) + 2];
if (data_converted[k] > 8388607) data_converted[k] = data_converted[k] - 16777216;
data_converted[k] = data_converted[k] * 0.536;
}
receiveBuffer.Add(data_converted);
newData_count ++;
// if (debug_mode) Console.WriteLine("DLL: Sample number " + sample_count + " has been received :)");
}
// Console.WriteLine("Data Recieved =" + last_data_recieve.GetValue(25) + " cnt = " + cnt + " rec_cnt = " + rec_cnt);
// }
else if (device_name == "Ultim8" && _serialPort.IsOpen && _serialPort.BytesToRead > 25)
{
byte[] last_data_recieve = new byte[26];
_serialPort.Read(last_data_recieve, 0, 26);
if (last_data_recieve[25] == 47)
{
sample_count++;
double[] data_converted = new double[10];
data_converted[0] = sample_count;
data_converted[1] = last_data_recieve[24];
int i = 2;
for (int k = i; k < 8 + i; k++)
{
data_converted[k] = (65536 * last_data_recieve[3 * (k - i) + 0]) + (256 * last_data_recieve[3 * (k - i) + 1]) + last_data_recieve[3 * (k - i) + 2];
if (data_converted[k] > 8388607) data_converted[k] = data_converted[k] - 16777216;
data_converted[k] = data_converted[k] * 0.536;
}
// while (ii < 100000) ii++;
// rec_cnt = _serialPort.Read(last_data_recieve, 0, 74);
// Console.WriteLine("Data Recieved =" + last_data_recieve.GetValue(25) + " cnt = " + cnt + " rec_cnt = " + rec_cnt);
// Console.WriteLine("Data Recieved =" + last_data_recieve_int.GetValue(25) + " cnt = " + cnt);
receiveBuffer.Add(data_converted);
newData_count++;
// if (debug_mode) Console.WriteLine("DLL: Sample number " + sample_count + " has been received :)");
}
else
// if (debug_mode) Console.WriteLine("DLL: Somthing is wrong!");
if (debug_mode) Console.WriteLine("DLL: Somthing is wrong!");
}
}
}
public object getData(int val=0)
{
double[][] last_data_list_temp;
if (dataGathering_flag)
{
if (val == 0)
{
while (newData_count == 0) ;
newData_count = 0;
last_data_list_temp = receiveBuffer.ToArray();
if (debug_mode) Console.WriteLine("DLL: Sending all buffered samples. " + newData_count);
receiveBuffer.Clear();
return last_data_list_temp;
}
else if (newData_count < val)
{
while (newData_count < val) ;
newData_count = 0;
last_data_list_temp = receiveBuffer.ToArray();
receiveBuffer.Clear();
if (debug_mode) Console.WriteLine("DLL: Sending all buffered samples with size of '" + val.ToString() + "' that you asked for.");
return last_data_list_temp;
}
else
{
last_data_list_temp = receiveBuffer.GetRange((int)(newData_count - val), val).ToArray();
receiveBuffer.Clear();
newData_count = 0;
if (debug_mode) Console.WriteLine("DLL: Sending last " + val.ToString() + " sample(s).");
return last_data_list_temp;
}
}
else if (newData_count > 0)
{
if (val == 0)
{
last_data_list_temp = receiveBuffer.ToArray();
receiveBuffer.Clear();
newData_count = 0;
if (debug_mode) Console.WriteLine("DLL: Sending all buffered samples.");
return last_data_list_temp;
}
else if (newData_count >= val)
{
last_data_list_temp = receiveBuffer.GetRange((int)(newData_count - val), (int)val).ToArray();
receiveBuffer.Clear();
newData_count = 0;
if (debug_mode) Console.WriteLine("DLL: Sending last " + val.ToString() + "sample(s).");
return last_data_list_temp;
}
else
{
if (debug_mode) Console.WriteLine("DLL: Device is not in gathering mode and receive buffer is less than number of samples you asked for.");
return false;
}
}
else
{
if (debug_mode) Console.WriteLine("DLL: Device is not in gathering mode and receive buffer is empty too.");
return false;
}
}
}
......
......@@ -4,3 +4,9 @@ C:\Users\i8-tech\Desktop\csharp_dlls\infinite8dll\I8Library1\obj\Debug\I8Library
C:\Users\i8-tech\Desktop\csharp_dlls\infinite8dll\I8Library1\obj\Debug\I8Library1.csproj.CoreCompileInputs.cache
C:\Users\i8-tech\Desktop\csharp_dlls\infinite8dll\I8Library1\obj\Debug\I8Library1.dll
C:\Users\i8-tech\Desktop\csharp_dlls\infinite8dll\I8Library1\obj\Debug\I8Library1.pdb
C:\Users\i8-tech\Desktop\Softwares\dll\I8Library1\bin\Debug\I8Library1.dll
C:\Users\i8-tech\Desktop\Softwares\dll\I8Library1\bin\Debug\I8Library1.pdb
C:\Users\i8-tech\Desktop\Softwares\dll\I8Library1\obj\Debug\I8Library1.csprojAssemblyReference.cache
C:\Users\i8-tech\Desktop\Softwares\dll\I8Library1\obj\Debug\I8Library1.csproj.CoreCompileInputs.cache
C:\Users\i8-tech\Desktop\Softwares\dll\I8Library1\obj\Debug\I8Library1.dll
C:\Users\i8-tech\Desktop\Softwares\dll\I8Library1\obj\Debug\I8Library1.pdb
# DLL
Infinite8 device library for use in SDKs
# I8DeviceDLL
I8DeviceDLL is a library package for a 3rd party to be able make use of Infinite8 Devices without any desctop application.
There is some classes and methodes in this dll file for users to minimize complexiti of hardware protocols.
With this pakage users can easily connect to any Infinite8 devices from many programming languege including C++, matlab and python.
## Getting Started (python)
This dll requires the following packages to work fine.
### Prerequisites
Use pip script to install `scipy` , `numpy`and `pythonnet` . These packages are used to read .dll files writen in C# and calculate some anticipated data.
```
pip install scipy
pip install numpy
pip install pythonnet
```
### Import dll file
After installing the requrment packages, import ".net for python" (clr) and make a reference (copy) of the .dll file with thess commands:
```
import clr
clr.AddReference(the absolute path of i8.dll file)
# for example: clr.AddReference('C:/Users/i8-tech/Desktop/csharp_dlls/infinite8dll/I8Library1/bin/Debug/I8Library1.dll')
```
### Instance from main class of i8devices
Just import `Device` class, And then you can use it by declaring an instance of `Device` class.
```
from I8Devices import Device
myAwsomeHolyShitImDyingForThisDevice = Device()
```
## How to Use
First of all, you should plug your device into your PC, and also install `ST micro COM Port Package` from `Device Manager` (it should be installed itself if your connected to internet).
After that type in the `Connect` method
```
myAwsomeHolyShitImDyingForThisDevice.Connect()
```
If device is successfully connected to the PC, you can use the following methods and properties to retrieve Device information, or send a command/ recieve data to/from the device:
```
# Info
myAwsomeHolyShitImDyingForThisDevice.name
myAwsomeHolyShitImDyingForThisDevice.channelsnumber
myAwsomeHolyShitImDyingForThisDevice.info
myAwsomeHolyShitImDyingForThisDevice.ReadStatus()
# get data
myAwsomeHolyShitImDyingForThisDevice.Start()
myAwsomeHolyShitImDyingForThisDevice.GetData()
myAwsomeHolyShitImDyingForThisDevice.Stop()
myAwsomeHolyShitImDyingForThisDevice.Leadoff()
# write setting
myAwsomeHolyShitImDyingForThisDevice.Set()
```
## Built With
* [C# dll .net](https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.pinchanged?view=netframework-4.8)
* [numpy](https://pypi.org/project/numpy/) - Used to calculate some shit
* [scipy](https://pypi.org/project/scipy/) - Used to calculate some real shit
## Contributing
How you fuckin' dare?
### Pull Request Process
1. Ensure any install or build dependencies are removed before the end of the layer when doing a
build.
2. Update the README.md with details of changes to the interface, this includes new environment
variables, exposed ports, useful file locations and container parameters.
3. Increase the version numbers in any examples files and the README.md to the new version that this
Pull Request would represent. The versioning scheme we use is [SemVer](http://semver.org/).
4. You may merge the Pull Request in once you have the sign-off of two other developers, or if you
do not have permission to do that, you may request the second reviewer to merge it for you.
## Versioning
We use [SemVer](http://semver.org/) for versioning. The current released version is 0.2.5.
## Authors
* **Varnos** - *Initial work* - [Varnos](http://51.75.64.148/reza)
## License
This project is licensed under the I8 Co. License - see the [Infinite8 website](https://infinite8.tech) for details. (you wish there will be some information but I gotta tell you there is not )
\ No newline at end of file
import clr
import time
clr.AddReference("C:/Users/i8-tech/Desktop/Softwares/dll/I8Library1/bin/Debug/I8Library1.dll")
from I8Devices import Device, Settings
def read_data():
dataList = []
for i in range(1000):
lastData = myAwsomeHolyShitImDyingForThisDevice.getData(5)
for j in range(lastData.Length):
# dataList.append(lastData.GetValue(j))
dataList.append(lastData[j])
return dataList
if __name__ == "__main__":
myAwsomeHolyShitImDyingForThisDevice = Device()
# myAwsomeHolyShitImDyingForThisDevice.debug_mode = True
if myAwsomeHolyShitImDyingForThisDevice.connect():
# time.sleep(1)
# mysetting = Settings()
# mysetting.test_signal = 234
# mysetting.sampling_rate = 885.5
# mysetting.channels_on[7] = False
# mysetting.gain = 3
#
# print(myAwsomeHolyShitImDyingForThisDevice.writeSetting(mysetting))
# myAwsomeHolyShitImDyingForThisDevice.start()
# all_data = read_data()
# t ime.sleep(0.5)
# all_data =[]
# lastData = myAwsomeHolyShitImDyingForThisDevice.getData(1000)
# for j in range(lastData.Length):
# all_data.append(lastData[j])
myAwsomeHolyShitImDyingForThisDevice.stop()
# time.sleep(0.5)
# print('len(all_data)::: ', len(all_data))
# myAwsomeHolyShitImDyingForThisDevice.debug_mode = True
status = myAwsomeHolyShitImDyingForThisDevice.getStatus()
# status = myAwsomeHolyShitImDyingForThisDevice.getStatus(val="cnt")
print(status['cnt'])
# print(status)
# for i in range(len(all_data)):
# # print(all_data[i].Length)
# print(all_data[i][0],' -- ', all_data[i][1])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment