I posted yesterday about how I convert a C # program as my first C # project here: Convert from C to C # or create a DLL? . I made great progress, but I was stuck on the section. I'm not sure how I should recreate this unsigned array
C code:
unsigned char state_buf[5125];
//--------------------------------------------------------------------------
// Main of iSerial64
//
void main(int argc, char **argv)
{
char refresh,buf[200];
short flag,i,didsetup=0;
short ROM[9];
short PortNum,PortType;
long SHandle;
char serialtmp[2], serial[10][17];
int j = -1;
....
This is a function in C # that accepts state_buf using PInvoke http://files.maximintegrated.com/sia_bu/licensed/docs/1-wire_sdk_win/TMEX/romm1rxq.html
C # code that I use to import functions from api:
[DllImport("IBFS64.dll")]
public static extern short TMRom(
int session_handle,
byte state_buf,
[MarshalAs(UnmanagedType.LPStr)]StringBuilder ROM
);
I saw that the byte was the equivalent of C #, but I'm not quite sure how to use it.
You guys have been so helpful so far, and I am very pleased with the progress that I am making on this.
, # :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace PInvokeTest {
class Program {
static void Main(string[] args)
{
int session_handle = 0;
int result = 0;
int flag = 0;
int didsetup = 0;
int defPort = 0;
int i = 0, j = -1;
short type_test = 0;
byte state_buf = 0;
short port_num = 0, port_type = 1;
StringBuilder ID_buf = new StringBuilder();
StringBuilder serial = new StringBuilder();
StringBuilder serialtmp = new StringBuilder();
StringBuilder ROM = new StringBuilder();
defPort = TMReadDefaultPort(out port_num,out port_type);
if (defPort == 1)
{
Console.Write("Default Device: ");
switch (port_type)
{
case 1:
Console.Write("DS9097E serial adapter ");
break;
case 2:
Console.Write("Parallel adapter ");
break;
case 5:
Console.Write("DS90907U serial adapter ");
break;
case 6:
Console.Write("DS9490 USB adapter ");
break;
}
Console.Write("on port # {0}", port_num);
}
else
Console.Write("No Device found");
Get_Version(ID_buf);
Console.WriteLine("\nDriver: {0}", ID_buf);
type_test = TMGetTypeVersion(port_type, ID_buf);
if (type_test < 0)
{
Console.WriteLine("\nNo Hardware Driver for this type found!\n");
Console.Write("Press any key to exit");
Console.ReadKey();
Environment.Exit(0);
}
else
Console.WriteLine("Driver Found");
if ((port_num < 1) || (port_num > 15))
{
Console.WriteLine("ERROR, invalid port requested: {0}\n", port_num);
Console.Write("Press any key to exit");
Console.ReadKey();
Environment.Exit(0);
}
do
{
session_handle = TMExtendedStartSession(port_num, port_type, IntPtr.Zero);
if (session_handle > 0)
{
if (didsetup == 0)
{
flag = TMSetup(session_handle);
if (flag == 1 || flag == 2)
{
Console.Write("TMSetup complete {0}", flag);
didsetup = 1;
}
else
{
Console.Write("ERROR doing setup {0}", flag);
break;
}
}
else
{
j++;
flag = TMNext(session_handle, state_buf);
if (flag > 0)
{
flag = TMRom(session_handle, state_buf, ROM);
for (i = 7; i >= 0; i--)
{
}
Console.WriteLine("{0} \n", serial[j]);
}
else
Console.WriteLine("end of search\n");
}
TMEndSession(session_handle);
}
}
while (flag > 0);
Console.ReadKey();
}
[DllImport("IBFS64.dll")]
public static extern int TMExtendedStartSession(
short PortNum,
short PortType,
IntPtr EnhancedOptions
);
[DllImport("IBFS64.dll")]
public static extern short TMSearch(
int session_handle,
byte state_buffer,
short p1,
short p2,
short p3
);
[DllImport("IBFS64.dll")]
public static extern short TMReadDefaultPort(
out short port_num,
out short port_type
);
[DllImport("IBFS64.dll")]
public static extern short Get_Version(
[MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
);
[DllImport("IBFS64.dll")]
public static extern short TMGetTypeVersion(
short port_type,
[MarshalAs(UnmanagedType.LPStr)]StringBuilder ID_buf
);
[DllImport("IBFS64.dll")]
public static extern short TMSetup(
int session_handle
);
[DllImport("IBFS64.dll")]
public static extern short TMNext(
int session_handle,
byte state_buf
);
[DllImport("IBFS64.dll")]
public static extern short TMRom(
int session_handle,
byte state_buf,
[MarshalAs(UnmanagedType.LPStr)]StringBuilder ROM
);
[DllImport("IBFS64.dll")]
public static extern short TMEndSession(
int session_handle
);
}
}
, -, . !