many ppls asked me about usb communication with LG sgold phones using widows api's :-)
here my source in c#
When phone is connected without battery, it will be in Flash interface for a short time ==> phone is connected with path : \\.\USB1
1- Import windows dll's
SETUPAPI.dll and
KERNEL32.dll Code:
//*********************** API **********************
public const uint DIGCF_PRESENT = 0x00000002;
public const uint DIGCF_INTERFACE_DEVICE = 0x00000010;
public const int FILE_FLAG_OVERLAPPED = 0x40000000;
public static IntPtr INVALID_HANDLE = new IntPtr(-1);
SafeFileHandle deviceHandle;
int numberOfBytesWritten = 0; int numberOfBytesRead = 0;
IntPtr unManagedBuffer;
IntPtr unManagedOverlapped;
Boolean success;
//______________setupapi_______________
[DllImport("setupapi.dll", SetLastError = true)]
public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
uint Enumerator, IntPtr hwndParent, uint Flags);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoList,
uint DeviceInfoData, ref Guid InterfaceClassGuid, uint MemberIndex,
SP_DEVICE_INTERFACE_DATA DevInterfaceData);
[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Ansi)]
public static extern bool SetupDiGetDeviceInterfaceDetail
(IntPtr DeviceInfoSet, SP_DEVICE_INTERFACE_DATA DevInterfaceData,
byte[] DetailData, int DataSize, ref int RequiredSize,
SP_DEVINFO_DATA DeviceInfoData);
[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiDestroyDeviceInfoList
(IntPtr DeviceInfoSet);
//______________endsetupapi____________
//______________kernel32_____________
[DllImport("Kernel32.dll")]
static extern SafeFileHandle CreateFile(string filename,
[MarshalAs(UnmanagedType.U4)]FileAccess fileaccess,
[MarshalAs(UnmanagedType.U4)]FileShare fileshare,
int securityattributes,
[MarshalAs(UnmanagedType.U4)]FileMode creationdisposition,
int flags, IntPtr template);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool DeviceIoControl(
IntPtr hDevice, uint IoControlCode,
byte[] InBuffer, int InBufSize,
byte[] OutBuffer, int OutBufSize,
ref int BytesReturned, OVERLAPPED OverLapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool WriteFile(SafeFileHandle hFile, byte[] lpBuffer,
Int32 nNumberOfBytesToWrite, ref Int32 lpNumberOfBytesWritten,
IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadFile(SafeFileHandle hFile, IntPtr lpBuffer,
Int32 nNumberOfBytesToWrite, ref Int32 lpNumberOfBytesWritten,
IntPtr lpOverlapped);
//______________endkernel32__________
2- some needed functions (not all needed for LG)
Code:
//___________________________________
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class SP_DEVINFO_DATA
{
public int cbSize;
public Guid ClassGuid;
public uint DevInst;
public uint Reserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class SP_DEVICE_INTERFACE_DATA
{
public int cbSize;
public Guid InterfaceClassGuid;
public uint Flags;
public uint Reserved;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class OVERLAPPED
{
public uint Internal;
public uint InternalHigh;
public uint Offset;
public uint OffsetHigh;
public IntPtr hEvent;
}
//___________________________________
3 - Read/write HEX data (managed buffer) :
Code:
public string read_write_hex1(SafeFileHandle devicehandle, byte[] command, int commandlenght, byte[] inputReportBuffer)
{
unManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length);
numberOfBytesRead = 0;
numberOfBytesWritten = 0;
success = WriteFile(devicehandle, command, commandlenght, ref numberOfBytesWritten,
unManagedOverlapped);
Thread.Sleep(5);
success = ReadFile(devicehandle, unManagedBuffer, inputReportBuffer.Length,
ref numberOfBytesRead, IntPtr.Zero);
Marshal.Copy(unManagedBuffer, inputReportBuffer, 0, numberOfBytesRead);
byte[] tempReDim = new byte[inputReportBuffer.Length];
if (inputReportBuffer != null)
{
System.Array.Copy(inputReportBuffer, tempReDim,
System.Math.Min(inputReportBuffer.Length, tempReDim.Length));
inputReportBuffer = tempReDim;
}
string hex = "";
string h = "";
foreach (byte inputReportBuffer_element in inputReportBuffer)
{
h = inputReportBuffer_element.ToString("X");
if (h.Length < 2)
{
hex = hex + "0" + h;
}
else
{
hex = hex + h;
}
}
return hex;
}
//_____________end readwrite_hex1________
4- Read/Write string data :
Code:
public string read_write_str(SafeFileHandle devicehandle, byte[] command, int commandlenght, byte[] inputReportBuffer)
{
unManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length);
numberOfBytesRead = 0;
numberOfBytesWritten = 0;
success = WriteFile(devicehandle, command, commandlenght, ref numberOfBytesWritten,
IntPtr.Zero);
Thread.Sleep(5);
success = ReadFile(devicehandle, unManagedBuffer, inputReportBuffer.Length,
ref numberOfBytesRead, unManagedOverlapped);
Marshal.Copy(unManagedBuffer, inputReportBuffer, 0, numberOfBytesRead);
byte[] tempReDim = new byte[inputReportBuffer.Length];
if (inputReportBuffer != null)
{
System.Array.Copy(inputReportBuffer, tempReDim,
System.Math.Min(inputReportBuffer.Length, tempReDim.Length));
inputReportBuffer = tempReDim;
}
string rest = "";
string hex = "";
int n = 0;
foreach (byte inputReportBuffer_element in inputReportBuffer)
{
if (n > commandlenght - 1)
{
hex = hex + inputReportBuffer_element.ToString("X");
if (inputReportBuffer_element <= 0x20)
{
rest = rest + " ";
}
else
{
rest = rest + Convert.ToChar(inputReportBuffer_element);
}
}
n += 1;
}
return rest.Trim();
}
//_____________end readwrite_str________
5 - use of thoese functions :
as I said the phone is connected with path \\.\USB1, we need to open this path with CreateFile function :
part of my code :
Code:
string devpath = "\\\\.\\USB1";
int i = 0; progressBar1.Visible = true;
progressBar1.Value = 0; progressBar1.Maximum = 100;
while (i <= 2000000) //waiting for phone connexion
{
deviceHandle = CreateFile(devpath, FileAccess.ReadWrite,
FileShare.ReadWrite, 0, FileMode.Open, 128, IntPtr.Zero);
if (deviceHandle.IsInvalid == false)
{
break;
}
progressBar1.Value = i / 20000;
i = i + 1;
}
read/write
Code:
byte[] iRB = new byte[20];
SR = read_write_hex1(deviceHandle, pr2, pr2.Length, iRB);
pr2: our hex data which we want to send to phone;
phone response is readed as hex and returned in SR;
iRB: our buffer;
PS: we can use separated functions to read/write (one for read and one for write), it's better I think, in my soft I use a separated functions
for more infos about windows api's :
pinvoke.net: the interop wiki!
./wbr