1

In a C# application which controls actual hardware movement, how does the communication take place?

If a PC is connected to other hardware via USB and the low level programming has already been done for the device (assume with C), how would high-level C# send and receive commands from the device? Would it just be a case of connecting to the DLL of the C code and doing it that way?

I'm looking for a generalised discussion on such high-level programming to driver scenarios. This is more of a hypothetical scenario however, so there aren't any details I can give to clue you in.

Thanks.

4

2 回答 2

2

是的,通常有一个带有C您调用的导出的 .DLL。

有时您可能会发现很难使用 P/Invoke 直接从 C# 调用某些导出。在这种情况下,您通常可以编写一个简单的CDLL 来包装调用并提供与 P/Invoke 更兼容的替代接口。

我还必须与看似文件的设备进行交互。在这些情况下,您必须CreateFile()使用特殊的文件名语法通过 Windows API 函数打开文件:

"\\.\deviceName"

我还必须使用 P/Invoke 来使用一些 Windows API USB 函数,例如此处所述。

如您所见,有几种方法可以访问硬件库。

于 2013-06-15T15:58:49.287 回答
2

与硬件的通信通常涉及分层堆栈。

application
---------------------------------------------------
glue code (assembly) (managed code)
---------------------------------------------------
user space library (static or DLL) (unmanaged code)
---------------------------------------------------
user space driver interface
---------------------------------------------------
kernel space driver
---------------------------------------------------
hardware

其中一些层可能是可选的。

于 2013-06-15T16:26:29.543 回答