Commit 59453173 authored by reza's avatar reza

Initial Commit

Initial Commit:
clr.AddReference("address of dll")
from I8Devices import Device
myDevice = Device()
myDevice.ConnectToDevice()
myDevice.StartDataAcquisition()
cntx = 0
datach1 = []
while cntx < 20000:
    data = myDevice.get_data()
    cntx+=1
    datach1.append(data[0])
myDevice.StoptDataAcquisition()
parent dc8ffef8
Pipeline #67 canceled with stages
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Threading;
using System.Management;
using System.Collections;
using System.Text;
using System.Threading.Tasks;
namespace I8Devices
{
public class COMPORTS
{
public IList FindPortByDesc(string Desc)
{
IList suspect_coms = new ArrayList();
foreach (ManagementObject obj in FindPorts())
{
try
{
if (obj["Description"].ToString().ToLower().Equals(Desc.ToLower()))
{
string comName = ParseCOMName(obj);
if (comName != null)
suspect_coms.Add(comName);
}
}
catch (Exception e)
{
// Console.WriteLine(e);
// Console.WriteLine("fuck3");
}
}
return suspect_coms;
// return null;
}
static ManagementObject[] FindPorts()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPEntity");
List<ManagementObject> objects = new List<ManagementObject>();
foreach (ManagementObject obj in searcher.Get())
{
objects.Add(obj);
}
return objects.ToArray();
}
catch (Exception e)
{
// Console.WriteLine(e);
// Console.WriteLine("fuck1");
return new ManagementObject[] { };
}
}
static string ParseCOMName(ManagementObject obj)
{
string name = obj["Name"].ToString();
int startIndex = name.LastIndexOf("(");
int endIndex = name.LastIndexOf(")");
if (startIndex != -1 && endIndex != -1)
{
name = name.Substring(startIndex + 1, endIndex - startIndex - 1);
return name;
}
return null;
}
}
public class Device
{
static int AnswerToInitFlag = 0;
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
{
{
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");
com_names = device_coms.FindPortByDesc("USB Serial Device");
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
_serialPort.Open();
_serialPort.Write(Encoding.UTF8.GetString(SendBuffer, 0, SendBuffer.Length));
try { _serialPort.Close(); }
catch (Exception) { };
while (ATR_flag != true) //wating for device to reinitializing...
{
try
{
_serialPort.Open();
ATR_flag = true;
}
catch (Exception)
{
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...");
// Console.WriteLine("Press any key to continue...");
// Console.WriteLine();
// message = Console.ReadLine();
ulong i = 0;
while (AnswerToInitFlag == 0 && i < 2000000000) i++;
_serialPort.Close();
if (AnswerToInitFlag == 0)
Console.WriteLine("No respond detected!");
}
}
else
Console.WriteLine("No I8Devices found!");
}
public double[] get_data()
{
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;
}
return last_data_converted;
}
public void StartDataAcquisition()
{
_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);
}
public void StoptDataAcquisition()
{
SendBuffer[3] = Convert.ToByte('E'); //setting mode
_serialPort.Write(Encoding.UTF8.GetString(SendBuffer, 0, SendBuffer.Length));
_serialPort.Close();
DataStopFlag = true;
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
ulong ii = 0;
// SerialPort sp = (SerialPort)sender;
// string indata = sp.ReadExisting();
if (AnswerToInitFlag != 1)
{
rec_cnt = _serialPort.Read(rec_data, 0, 32);
for (int i = 0; i < 32; i++)
{
// Console.WriteLine(Convert.ToByte(rec_data[i]));
}
if (Convert.ToByte(rec_data[31]) == 144)
{
if (Convert.ToByte(rec_data[27]) == 48)
{
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);
}
else if (Convert.ToByte(rec_data[27]) == 47)
{
Console.WriteLine("");
Console.Write("Device Ultim8 is Connected on COM Port" + ' ' + device_com_port);
}
else
Console.WriteLine("no valid response detected from" + _serialPort.PortName);
AnswerToInitFlag = 1;
}
}
else
{
// for (int j = 0; j < 74; j++)
// {
// while (ii < 200000) ii++;
if (_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);
}
// Console.WriteLine("Data Recieved =" + last_data_recieve.GetValue(25) + " cnt = " + cnt + " rec_cnt = " + rec_cnt);
// }
// 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);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{76A81749-DBF0-47E7-A044-8F506305A1C5}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>I8Library1</RootNamespace>
<AssemblyName>I8Library1</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29230.47
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "I8Library1", "I8Library1.csproj", "{76A81749-DBF0-47E7-A044-8F506305A1C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{76A81749-DBF0-47E7-A044-8F506305A1C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76A81749-DBF0-47E7-A044-8F506305A1C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76A81749-DBF0-47E7-A044-8F506305A1C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76A81749-DBF0-47E7-A044-8F506305A1C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EA95706F-57DD-4072-911E-28CBADF8ABBE}
EndGlobalSection
EndGlobal
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("I8Library1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("I8Library1")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("76a81749-dbf0-47e7-a044-8f506305a1c5")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
C:\Users\i8-tech\Desktop\csharp_dlls\infinite8dll\I8Library1\bin\Debug\I8Library1.dll
C:\Users\i8-tech\Desktop\csharp_dlls\infinite8dll\I8Library1\bin\Debug\I8Library1.pdb
C:\Users\i8-tech\Desktop\csharp_dlls\infinite8dll\I8Library1\obj\Debug\I8Library1.csprojAssemblyReference.cache
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
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