Sending output to USBm device (Scent Palette)
Does anyone ever used a Scent Palette in Unity or any USBmicro device ?
Its a simple I/O device. It comes with a .dll library that you can use to communicate with the device. Here is the relevant documentation : http://www.usbmicro.com/documentation/programming-overview/c%E2%81%84-net-information/
See below for my code. In public class USB, I define the pins as Byte [] (8 under A, 8 under B), and invoke the relevant commands in the library.
In Void Start(), I’m able to access the .dll library within Unity. I've put the library in the root folder. I’m able to use the USBm_FindDevices and the USBm_DeviceValid commands to detect that the device is correctly connected and working.
However, in Void Update, when I’m sending outputs to the device, results doesn’t follow the intended pins.
With USBm_WriteDevice, the code opens and closes randomly pins under A, regardless of the byte[] I’ve inputted.
With USBm_SetBit and USBm_ResetBit, the code always opens or closes the first A pin, again, regardless of the byte[] I’ve inputted.
In both cases, I’m unable to send commands to the B set of pins.
When I’m using the sample software provided with the scent palette, or those provided on the USBmicro website, I’m able to properly operate the machine. This is strange, since these software are based on the sample .dll library.
Does anyone knows what could be the problem ?
using UnityEngine;
using System.Runtime.InteropServices;
using System.Text;
public class ScentPalette : MonoBehaviour
{
public class USBm
{
public static byte BitA0 = 0x00;
public static byte BitA1 = 0x01;
public static byte BitA2 = 0x02;
public static byte BitA3 = 0x03;
public static byte BitA4 = 0x04;
public static byte BitA5 = 0x05;
public static byte BitA6 = 0x06;
public static byte BitA7 = 0x07;
public static byte BitB0 = 0x08;
public static byte BitB1 = 0x09;
public static byte BitB2 = 0x0A;
public static byte BitB3 = 0x0B;
public static byte BitB4 = 0x0C;
public static byte BitB5 = 0x0D;
public static byte BitB6 = 0x0E;
public static byte BitB7 = 0x0F;
[DllImport("USBm.dll")]
public static extern bool USBm_FindDevices();
[DllImport("USBm.dll")]
public static extern int USBm_NumberOfDevices();
[DllImport("USBm.dll")]
public static extern bool USBm_DeviceValid(int Device);
[DllImport("USBm.dll")]
public static extern bool USBm_About(StringBuilder About);
[DllImport("USBm.dll")]
public static extern bool USBm_Version(StringBuilder Version);
[DllImport("USBm.dll")]
public static extern bool USBm_Copyright(StringBuilder Copyright);
[DllImport("USBm.dll")]
public static extern bool USBm_DeviceMfr(int Device, StringBuilder Mfr);
[DllImport("USBm.dll")]
public static extern bool USBm_DeviceProd(int Device, StringBuilder Prod);
[DllImport("USBm.dll")]
public static extern int USBm_DeviceFirmwareVer(int Device);
[DllImport("USBm.dll")]
public static extern bool USBm_DeviceSer(int Device, StringBuilder dSer);
[DllImport("USBm.dll")]
public static extern int USBm_DeviceDID(int Device);
[DllImport("USBm.dll")]
public static extern int USBm_DevicePID(int Device);
[DllImport("USBm.dll")]
public static extern int USBm_DeviceVID(int Device);
[DllImport("USBm.dll")]
public static extern bool USBm_DebugString(StringBuilder DBug);
[DllImport("USBm.dll")]
public static extern bool USBm_RecentError(StringBuilder rError);
[DllImport("USBm.dll")]
public static extern bool USBm_ClearRecentError();
[DllImport("USBm.dll")]
public static extern bool USBm_SetReadTimeout(uint TimeOut);
[DllImport("USBm.dll")]
public static extern bool USBm_ReadDevice(int Device, byte[] inBuf);
[DllImport("USBm.dll")]
public static extern bool USBm_WriteDevice(int Device, byte[] outBuf);
[DllImport("USBm.dll")]
public static extern bool USBm_CloseDevice(int Device);
[DllImport("USBm.dll")]
public static extern bool USBm_WriteA(int Device, byte[] data );
[DllImport("USBm.dll")]
public static extern bool USBm_WriteB(int Device, byte[] data);
[DllImport("USBm.dll")]
public static extern bool USBm_SetBit(int Device, byte[] bit);
[DllImport("USBm.dll")]
public static extern bool USBm_ResetBit(int Device, byte[] bit);
[DllImport("USBm.dll")]
public static extern bool USBm_WriteABit(int device, byte[] and_term, byte[] or_term);
[DllImport("USBm.dll")]
public static extern bool USBm_WriteBBit(int device, byte[] and_term, byte[] or_term);
[DllImport("USBm.dll")]
public static extern bool USBm_DirectionAOut(int device);
[DllImport("USBm.dll")]
public static extern bool USBm_DirectionBOut(int device);
[DllImport("USBm.dll")]
public static extern bool USBm_DirectionA(int device, byte[] dir0, byte[] dir1);
[DllImport("USBm.dll")]
public static extern bool USBm_DirectionB(int device, byte[] dir0, byte[] dir1);
[DllImport("USBm.dll")]
public static extern bool USBm_ReadA(int Device, byte[] data);
[DllImport("USBm.dll")]
public static extern bool USBm_ReadB(int Device, byte[] data);
[DllImport("USBm.dll")]
public static extern bool USBm_InitPorts(int device);
}
public void Start()
{
if (!USBm.USBm_FindDevices())
{
Debug.Log("No Device Connected");
return;
}
else
{
Debug.Log("Device Detected");
Debug.Log(USBm.USBm_DeviceValid(0));
USBm.USBm_InitPorts(0);
USBm.USBm_DirectionAOut(0);
USBm.USBm_DirectionBOut(0);
}
}
public void Update()
{
if (Input.GetKeyDown("space"))
{
byte[] bit = { USBm.BitA0 };
USBm.USBm_SetBit(0, bit);
}
if (Input.GetKeyDown("b"))
{
byte[] bit = { USBm.BitA0 };
USBm.USBm_ResetBit(0, bit);
}
}
}