📄 picusbapi.cs
字号:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices; // To Import MPUSBAPI.DLL library File
using PVOID = System.IntPtr;
using DWORD = System.UInt32;
namespace USB_PIC18_GUI
{
unsafe public class PicUSBAPI
{
#region VID, PID String Definations
string vid_pid_norm = "vid_04d8&pid_0011";
string out_pipe = "\\MCHP_EP1";
string in_pipe = "\\MCHP_EP1";
#endregion
#region Functions Imported from DLL file 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;
// public const int MaxPacketSize = 32;
#region Low Level Pipe Functions
public void OpenPipes() //Called before every bus transaction
{
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() //Should be called after completion of bus transaction
{
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
}
#endregion
#region Packet Send/Receive Functions
public void SendPacket(byte* SendData, DWORD SendLength) //Sends a packet to bus
{
uint SendDelay = 1;
DWORD SentDataLength;
OpenPipes();
_MPUSBWrite(myOutPipe, (void*)SendData, SendLength, &SentDataLength, SendDelay);
ClosePipes();
}
public void ReceivePacket(byte* ReceiveData, DWORD *ReceiveLength) //Receives a packet from the bus
{
uint ReceiveDelay=1;
DWORD ExpectedReceiveLength = *ReceiveLength;
OpenPipes();
_MPUSBRead(myInPipe, (void*)ReceiveData, ExpectedReceiveLength, ReceiveLength, ReceiveDelay);
ClosePipes();
}
#endregion
//Coustom Functions
public void SendString(string String)
{
byte* send_buf = stackalloc byte[String .Length];
char[] buf;
buf = String.ToCharArray();
for (int i = 0; i < buf.GetLength(0); i++)
send_buf[i] = (byte)buf[i];
send_buf[0] = 1;
SendPacket(send_buf, 32); // send packet
}
public byte [] GetByteArray(int Length)
{
byte* receive_buf = stackalloc byte[Length ];
DWORD RecvLength = (uint)Length ;
byte[] rxd = new byte[Length ];
ReceivePacket(receive_buf, &RecvLength);
for (int i = 0; i < Length ; i++)
rxd[i] = receive_buf[i];
return rxd;
}
public void SendByteArray(byte[] ArrayToSend, int Length)
{
byte* SendBuffer = stackalloc byte[Length];
for (int i = 0; i < Length; i++)
SendBuffer[i] = ArrayToSend[i];
SendPacket(SendBuffer, (uint)Length);
}
public uint ReadAdc(byte samples, byte channel,byte delay)
{
uint Retint = 0;
byte* SendBuff = stackalloc byte[4];
byte* RxBuff= stackalloc byte [3];
DWORD RxLength = 3;
SendBuff[0] = channel;
SendBuff[1] = samples;
SendBuff[2] = delay;
SendBuff[3] = (byte )'R';
SendPacket(SendBuff, 4);
ReceivePacket(RxBuff, &RxLength);
Retint = (uint )(RxBuff[2] * 256) + RxBuff[1];//join byte0 and byte1
return Retint ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -