📄 picusbapi.cs
字号:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; // Clase para importar DLL
using PVOID = System.IntPtr;
using DWORD = System.UInt32;
namespace picusbnut
{
unsafe public class PicUSBAPI
{
#region Definici髇 de los Strings: EndPoint y VID_PID
string vid_pid_norm = "vid_04d8&pid_0011";
string out_pipe = "\\MCHP_EP1";
string in_pipe = "\\MCHP_EP1";
#endregion
#region Funciones importadas de la DLL: mpusbapi.dll
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDLLVersion();
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBGetDeviceCount(string pVID_PID);
[DllImport("mpusbapi.dll")]
private static extern void* _MPUSBOpen(DWORD instance, string pVID_PID, string pEP, DWORD dwDir, DWORD dwReserved);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBRead(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBWrite(void* handle, void* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern DWORD _MPUSBReadInt(void* handle, DWORD* pData, DWORD dwLen, DWORD* pLength, DWORD dwMilliseconds);
[DllImport("mpusbapi.dll")]
private static extern bool _MPUSBClose(void* handle);
#endregion
void* myOutPipe;
void* myInPipe;
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new picusbnut());
}
public void OpenPipes()
{
DWORD selection = 0;
myOutPipe = _MPUSBOpen(selection, vid_pid_norm, out_pipe, 0, 0);
myInPipe = _MPUSBOpen(selection, vid_pid_norm, in_pipe, 1, 0);
}
public void ClosePipes()
{
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
}
private void SendPacket(byte* SendData, DWORD SendLength)
{
uint SendDelay = 1;
DWORD SentDataLength;
OpenPipes();
_MPUSBWrite(myOutPipe, (void*)SendData, SendLength, &SentDataLength, SendDelay);
ClosePipes();
}
private void ReceivePacket(byte* ReceiveData, DWORD *ReceiveLength)
{
uint ReceiveDelay=1;
DWORD ExpectedReceiveLength = *ReceiveLength;
OpenPipes();
_MPUSBRead(myInPipe, (void*)ReceiveData, ExpectedReceiveLength, ReceiveLength, ReceiveDelay);
ClosePipes();
}
public void LedPIC(uint led)
{
byte* send_buf = stackalloc byte[2];
send_buf[0] = 0x00; // led mode
send_buf[1] = (byte)led; // led option
SendPacket(send_buf, 2); // send packet
}
public void TextPIC(string text)
{
byte* send_buf = stackalloc byte[17];
char[] buf;
send_buf[0] = 0x01; // text mode
buf = text.ToCharArray();
for (int i = 0; i < buf.GetLength(0); i++)
{
send_buf[i+1] = (byte)buf[i];
}
SendPacket(send_buf, 17); // send packet
}
public float ConvADPIC()
{
byte* send_buf = stackalloc byte[1];
byte* receive_buf = stackalloc byte[2];
float conversion = 0;
DWORD RecvLength = 2;
send_buf[0] = 0x02; // text mode
SendPacket(send_buf, 1); // send packet
ReceivePacket(receive_buf, &RecvLength);
if (receive_buf[0] == 0)
{
conversion = receive_buf[1];
}
return conversion;
}
public uint SwitchPIC()
{
byte* receive_buf = stackalloc byte[2];
uint switchst = 0;
DWORD RecvLength = 2;
ReceivePacket(receive_buf, &RecvLength);
if (receive_buf[0] == 1)
{
switchst = receive_buf[1];
}
return switchst;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -