首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 嵌入开发 > 驱动开发 >

C#怎么使用DeviceIoControl控制端口输出?[

2012-08-22 
C#如何使用DeviceIoControl控制端口输出??[我现在用的是并口。地址是378h,我用c++写了一小段程序就能跑通,

C#如何使用DeviceIoControl控制端口输出??[
我现在用的是并口。地址是378h,我用c++写了一小段程序就能跑通,在并口上能测到输出,但是用C#写的同样功能的程序来测试,并口就没有任何反应,把程序贴上来,麻烦大家给看一下,谢谢了~~~

下面是C++的

C/C++ code
int main(){    HANDLE hDevice =         CreateFile("\\\\.\\HelloDDK",                    GENERIC_READ | GENERIC_WRITE,                    0,                                    // share mode none                    NULL,                                // no security                    OPEN_EXISTING,                    FILE_ATTRIBUTE_NORMAL,                    NULL );                                // no template    if (hDevice == INVALID_HANDLE_VALUE)    {        return 1;    }    else    {        printf("Successed to obtain file handle to device.");    }    DWORD dwOutput;    DWORD outputBuffer[8];    DWORD inputBuffer[1];    int i,j;    //开始输出数据    do{        inputBuffer[0] = 0x00600378;                    //Output Data Register        DeviceIoControl(hDevice, WRITE_PORT, inputBuffer, sizeof(inputBuffer), NULL, 0, &dwOutput, NULL);        for (i=0;i<850;i++){;}      }while(1);    CloseHandle(hDevice);    printf("!!!");    getchar();    return 0;}



下面是C#的

C# code
#region  DLLImport        //DeviceIoControl在C#中的引用和定义        [DllImport("kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]        internal static extern int DeviceIoControl(            IntPtr hDevice,            uint dwIoControlCode,            uint[] lpInBuffer,            int nInBufferSize,            byte[] lpOutbuffer,            int nOutBufferSize,            ref int lpByteReturned,            IntPtr lpOverlapped            );        [DllImport("kernel32.dll")]        private static extern IntPtr CreateFile(            string lpFileName,            uint dwDesireAccess,            int dwShareMode,            int lpSecurityAttributes,            int dwCreationDisposition,            int dwFlagsAndAttributes,            int hTemplateFile            );        [DllImport("kernel32.dll", SetLastError = true)]        static extern int CloseHandle(IntPtr hObject);        private const uint GENERIC_READ = 0x80000000;        private const uint GENERIC_WRITE = 0x40000000;        private const int OPEN_EXISTING = 3;        #endregion         static void Main(string[] args)        {            //设置的有关GPIO的全局变量            IntPtr gpioDevice = new IntPtr();            int bytesReturned = 0;            gpioDevice = CreateFile("\\\\.\\HelloDDK", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);            if (gpioDevice == (IntPtr)(-1))            {                //暂时不处理                Console.WriteLine("连接GPIO设备失败");            }            else            {                while (true)                {                    uint[] data = new uint[1];                    data[0] = 0x00600378;                    DeviceIoControl(gpioDevice, 0x01, data, sizeof(uint), null, 0, ref bytesReturned, IntPtr.Zero);                    for (int j = 0; j < 850; j++) { }                                        Console.WriteLine(DeviceIoControl(gpioDevice, 0x02, data, 4, null, 0, ref bytesReturned, IntPtr.Zero));                    Console.WriteLine("******");                }            }        }


小弟分不多了,麻烦各位知道的给解释一下,我看了下C#中参数的名字也都对得上,但就是死活没输出。。。

有积分了再加分

[解决办法]
你的C#程序哪一步执行不成功啊?

单步调试一下,是不是对应的函数没有真正加载或者是参数设置不对?
[解决办法]
看了一下你的C#代码,显然,你的DeviceIoControl参数有问题。第二个参数应该你定义的是IOCTL,怎么回事01或者02呢?


[解决办法]
[DllImport("kernel32.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
internal static extern int DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
uint[] lpInBuffer, //C#的数组不能当指针用
int nInBufferSize,
byte[] lpOutbuffer, //
int nOutBufferSize,
ref int lpByteReturned,
IntPtr lpOverlapped
);

热点排行