I posted yesterday about how I am converting a C program to C# as my first C# project here: Convert from C to C#, or make DLL?. I have made a lot of progress, but I am stuck on a section. I am not sure how I should go about recreating this unsigned char 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 the function in C# that takes state_buf using PInvoke http://files.maximintegrated.com/sia_bu/licensed/docs/1-wire_sdk_win/TMEX/romm1rxq.html
C# Code that I am using to import the function from the api:
[DllImport("IBFS64.dll")]
public static extern short TMRom(
int session_handle,
byte state_buf,
[MarshalAs(UnmanagedType.LPStr)]StringBuilder ROM
);
I saw that byte was the C# equivalent, but I am not fully sure how to use it.
You guys have been so helpful thus far, and I am really pleased with the progress I am making on this.
This is what I have gotten done in C# since yesterday:
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();
//result = TMSearch(session_handle, state_buffer, 1, 1, 0xEC);
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 the TMEX driver version
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");
// check for valid range of PortNum
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
{
// get a session handle to the requested port
session_handle = TMExtendedStartSession(port_num, port_type, IntPtr.Zero);
if (session_handle > 0)
{
// check to see if TMSetup has been done once
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 was added to keep track of the serial number strings
j++;
//memset(serial[j], 0, strlen(serial[j]));
flag = TMNext(session_handle, state_buf);
if (flag > 0)
{
//ROM[0] = 0;
flag = TMRom(session_handle, state_buf, ROM);
for (i = 7; i >= 0; i--)
{
//This section was changed from the original
//copies raw number into string
//sprintf(serialtmp, "%02X", ROM[i]);
//strcat(serial[j], serialtmp);
}
Console.WriteLine("{0} \n", serial[j]);
}
else
Console.WriteLine("end of search\n");
}
// close the opened session
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
);
}
}
Let me know if there is anything I should change. I appreciate the help!
For the C language array:
unsigned char state_buf[5125] ;
try the C#
byte[] state_buf = new byte[5125] ;
byte
is an unsigned 8-bit number with the domain 0-255. If you want your octets signed, use sbyte
(domain -128 through +127).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With