抱歉
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace DeviceApplication1
{
unsafe public partial class Form1 : Form
{
const UInt32 OPEN_EXISTING = 3;
const UInt32 GENERIC_READ = 0x80000000;
const UInt32 GENERIC_WRITE = 0x40000000;
const Int32 INVALID_HANDLE_VALUE = -1;
const UInt32 PWM_start = 0x02;
private IntPtr hPort;
//导入API
[DllImport("coredll.dll")]
public static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode, IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes, IntPtr hTemplateFile);
[DllImport("coredll.dll")]
public static extern bool DeviceIoControl(IntPtr hDevice, UInt32 dwIoControlCode, UInt32* lpInBuffer, UInt32 nInBufferSize, Byte[] lpOutBuffer, UInt32 nOutBufferSize, UInt32 lpBytesReturned, IntPtr lpOverlapped);
UInt32 voice = 1000;
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
hPort = CreateFile("PWM1:", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
if (hPort == (IntPtr)INVALID_HANDLE_VALUE)
{
MessageBox.Show("Open PWM Driver Fail");
}
}
private void button4_Click(object sender, EventArgs e)
{
Byte[] pout = new Byte[10];
voice += 10 ;
if (voice >= 1500)
voice = 1500;
textBox1.Text = voice+"";
fixed (UInt32* p = &voice)
{
DeviceIoControl(hPort, PWM_start, p, 4, pout, 0, 0, IntPtr.Zero);
}
}
private void button2_Click(object sender, EventArgs e)
{
Byte[] pout = new Byte[10];
voice -= 10;
if(voice<=500)
voice =500;
textBox1.Text = voice + "";
fixed (UInt32* p = &voice)
{
DeviceIoControl(hPort, PWM_start, p, 4, pout, 0, 0, IntPtr.Zero);
}
}
}
}