C# 控制cp2103芯片 GPIO口
2018-04-09 09:58
453 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace CP2103
{
class Program
{
static void Main(string[] args)
{
Int32 numOfDevices = 0;
Int32 retVal = CP210x.GetNumDevices(ref numOfDevices);
IntPtr handle = IntPtr.Zero;
Byte[] prtNum = new Byte[1];
UInt16[] latch = new UInt16[4];
UInt16 mask = 0x01;
if (numOfDevices > 0)
{
retVal = CP210x.Open(0, ref handle);
retVal = CP210x.GetPartNumber(handle, prtNum);
if (prtNum[0] == 3) //cp21"03"
{
//设置GPIO为101
retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 0), (UInt16)(0x01 << 0));
retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 1), (UInt16)(0x00 << 1));
retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 2), (UInt16)(0x01 << 2));
}
retVal = CP210x.ReadLatch(handle, latch);
retVal = CP210x.Close(handle);
}
Console.WriteLine(latch[0]);
Console.WriteLine(retVal.ToString());
}
}
public class CP210x
{
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_GetNumDevices(ref Int32 numOfDevices);
public static Int32 GetNumDevices(ref Int32 numOfDevices)
{
return CP210x_GetNumDevices(ref numOfDevices);
}
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_Open(Int32 deviceNum, ref IntPtr handle);
public static Int32 Open(Int32 deviceNum, ref IntPtr handle)
{
return CP210x_Open(deviceNum, ref handle);
}
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_Close(IntPtr handle);
public static Int32 Close(IntPtr handle)
{
return CP210x_Close(handle);
}
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_GetPartNumber(IntPtr handle, Byte[] lpbPartNum);
public static Int32 GetPartNumber(IntPtr handle, Byte[] lpbPartNum)
{
return CP210x_GetPartNumber(handle, lpbPartNum);
}
[DllImport("CP210xRuntime.dll")]
private static extern Int32 CP210xRT_WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch);
public static Int32 WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch)
{
return CP210xRT_WriteLatch(handle, mask, latch);
}
[DllImport("CP210xRuntime.dll")]
private static extern Int32 CP210xRT_ReadLatch(IntPtr handle, UInt16[] lpLatch);
public static Int32 ReadLatch(IntPtr handle, UInt16[] lpLatch)
{
return CP210xRT_ReadLatch(handle, lpLatch);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace CP2103
{
class Program
{
static void Main(string[] args)
{
Int32 numOfDevices = 0;
Int32 retVal = CP210x.GetNumDevices(ref numOfDevices);
IntPtr handle = IntPtr.Zero;
Byte[] prtNum = new Byte[1];
UInt16[] latch = new UInt16[4];
UInt16 mask = 0x01;
if (numOfDevices > 0)
{
retVal = CP210x.Open(0, ref handle);
retVal = CP210x.GetPartNumber(handle, prtNum);
if (prtNum[0] == 3) //cp21"03"
{
//设置GPIO为101
retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 0), (UInt16)(0x01 << 0));
retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 1), (UInt16)(0x00 << 1));
retVal = CP210x.WriteLatch(handle, (UInt16)(mask << 2), (UInt16)(0x01 << 2));
}
retVal = CP210x.ReadLatch(handle, latch);
retVal = CP210x.Close(handle);
}
Console.WriteLine(latch[0]);
Console.WriteLine(retVal.ToString());
}
}
public class CP210x
{
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_GetNumDevices(ref Int32 numOfDevices);
public static Int32 GetNumDevices(ref Int32 numOfDevices)
{
return CP210x_GetNumDevices(ref numOfDevices);
}
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_Open(Int32 deviceNum, ref IntPtr handle);
public static Int32 Open(Int32 deviceNum, ref IntPtr handle)
{
return CP210x_Open(deviceNum, ref handle);
}
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_Close(IntPtr handle);
public static Int32 Close(IntPtr handle)
{
return CP210x_Close(handle);
}
[DllImport("CP210xManufacturing.dll")]
private static extern Int32 CP210x_GetPartNumber(IntPtr handle, Byte[] lpbPartNum);
public static Int32 GetPartNumber(IntPtr handle, Byte[] lpbPartNum)
{
return CP210x_GetPartNumber(handle, lpbPartNum);
}
[DllImport("CP210xRuntime.dll")]
private static extern Int32 CP210xRT_WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch);
public static Int32 WriteLatch(IntPtr handle, UInt16 mask, UInt16 latch)
{
return CP210xRT_WriteLatch(handle, mask, latch);
}
[DllImport("CP210xRuntime.dll")]
private static extern Int32 CP210xRT_ReadLatch(IntPtr handle, UInt16[] lpLatch);
public static Int32 ReadLatch(IntPtr handle, UInt16[] lpLatch)
{
return CP210xRT_ReadLatch(handle, lpLatch);
}
}
}
相关文章推荐
- 用C#控制TQ2440开发板上的LED小灯(C# GPIO学习笔记)
- 使用GPIO控制SPI接口的AD芯片//来源不详
- 用C#控制TQ2440开发板上的LED小灯(C# GPIO学习笔记)
- 使用GPIO控制SPI接口的AD芯片 (转)
- 用C#控制TQ2440开发板上的LED小灯(C# GPIO学习笔记)
- ok6410 LED/GPIO 控制 C#程序
- 通过API实现C#对硬件的控制(一)
- C#流程控制语句
- c# 控制音量大小 CoreAudioApi
- C# 多线程控制 通讯
- 用C#实现蜘蛛/爬虫程序的多线程控制
- C#开发基于wince SDK的PWM控制软件
- CAN总线技术2--CAN网络控制芯片SJA1000
- C# 下控制 WebBrower 以及 HtmlDocument
- C#从一个类中调用控制主窗体的控件
- 树莓派与GPIO:74HC595控制LED数码管
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- 继续树莓派GPIO:两片74HC595控制一个8x8LED点阵显示爱心
- C#获取显示器屏幕数量 控制winform显示到哪一个屏幕
- 【C#与.NET程序设计】(2)- C#基本数据类型及控制结构