我正在尝试使用 C#在Unity(单声道框架)中更改Delcom USB 7 段显示器的显示。我能够找到设备并读取序列号,但无法正确发送控制数据包。我非常肯定我正在错误地形成和/或发送数据,但我没有这种类型的通信经验并且不知道该怎么做。
我从他们的几个示例程序(都在 C++ 中)拼凑了以下代码。我正在使用 InteropServices 来使用他们提供的非托管 DLL。
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;
public class DelcomController : MonoBehaviour
{
[DllImport("DelcomDLL", EntryPoint="DelcomGetDeviceCount")]
public static extern int DelcomGetDeviceCount(uint ProductType);
[DllImport("DelcomDLL", EntryPoint="DelcomGetNthDevice")]
public static extern int DelcomGetNthDevice(uint ProductType, uint NthDevice, StringBuilder DeviceName);
[DllImport("DelcomDLL", EntryPoint="DelcomOpenDevice")]
public static extern uint DelcomOpenDevice(StringBuilder DeviceName, int Mode );
[DllImport("DelcomDLL", EntryPoint="DelcomCloseDevice")]
public static extern int DelcomCloseDevice(uint DeviceHandle);
[DllImport("DelcomDLL", EntryPoint="DelcomReadDeviceSerialNum")]
public static extern int DelcomReadDeviceSerialNum(StringBuilder DeviceName, uint DeviceHandle);
[DllImport("DelcomDLL", EntryPoint="DelcomSendPacket")]
public static extern int DelcomSendPacket(uint DeviceHandle, ref PacketStructure PacketOut);
[DllImport("DelcomDLL", EntryPoint="DelcomLEDControl")]
public static extern int DelcomLEDControl(uint DeviceHandle, int Color, int Mode);
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct PacketStructure
{
public byte Recipient;
public byte DeviceModel;
public byte MajorCmd;
public byte MinorCmd;
public byte DataLSB;
public byte DataMSB;
public byte Length;
[ MarshalAs( UnmanagedType.ByValArray, SizeConst = 8 )]
public byte[] DATA ; //Data 1 .. 8
}
private uint hUSB;
private static int DisplaySize = 6;
PacketStructure packet = new PacketStructure ();
void Start ()
{
int Result = 0;
StringBuilder DeviceName = new StringBuilder (512);
// Serach for the first match USB device, For USB IO Chips use Delcom.USBIODS
// With Generation 2 HID devices, you can pass a TypeId of 0 to open any Delcom device.
try
{
Result = DelcomGetNthDevice (3, 0, DeviceName);
}
catch
{
print ("Error in DelcomGetNthDevice ().");
}
if (Result == 0)
{ // if not found, exit
print ("Device not found!\n");
return;
}
print ("Device found: "+ DeviceName); // This prints the GUID properly..
hUSB = DelcomOpenDevice(DeviceName, 0);
print ("Device opened: " + hUSB.ToString ()); // This prints the handle fine.
int serial = DelcomReadDeviceSerialNum (DeviceName, hUSB);
print ("Device serial: " + serial.ToString ()); // This prints the correct serial.
// Next, I'm attempting to turn on all of the digits using the example in NDHIDTEST as a reference.
// The device does nothing. I'm pretty sure I'm not forming the packet properly, but I'm not exactly
// sure of what changes to make.
// Enter 8-digit mode.
DisplaySizeSetup (8);
// Turn all digits on.
packet.MajorCmd = 101;
packet.MinorCmd = 81;
packet.DataLSB = 0;
packet.DataMSB = 0;
DelcomSendPacket (hUSB, ref packet);
packet.MajorCmd = 101;
packet.MinorCmd = 80;
packet.DataLSB = 1;
packet.DataMSB = 0;
DelcomSendPacket (hUSB, ref packet); // blink off
// I'm doing this just for our test program to make sure we keep the device open for long enough.
StartCoroutine (CloseDevice ());
}
private IEnumerator CloseDevice ()
{
yield return new WaitForSeconds (5f);
DelcomCloseDevice(hUSB);
print ("Device closed.");
}
}
因此 GetNthDevice 找到了显示器,hUSB 使用生成的 DeviceName 填充了标识符,并读取了正确的序列号。接下来,我试图发送一个数据包,该数据包在他们的一个测试程序中使用,该程序应该简单地照亮显示器上的所有 LED,但它没有做任何事情。
最终,我需要知道如何设置显示屏上的四位数字。