Hi,
I'm experiencing quite a problem and I hope someone can help me. For information, this is partly linked to another question because this explains what I did to get to my problem:
http://answers.unity3d.com/questions/515264/importing-cc-function-from-dll.html#answer-515366
The problem is that Unity completely freeze when I update a script in visual studio and then switch back to the unity editor.
Here's my code and setup:
I have a simple scene with a camera and a "Test" script applied on it. That test script reference a "DLLTest" dll to get data from a custom controller device. That device sends a number through USB each 500ms.
The "DLLTest" is the interface that reads the data from the device. It contains 2 classes and reference to a C++ DLL called AID.dll. that dll also reference another dll called FDT2XX.dll. Those 2 dll are placed in the folder /Assets/Plugins. All other files are at the root of the project.
here's the implementations
The "Test" script applied the the main camera:
using UnityEngine;
using System.Collections;
using DLLTest;
public class Test : MonoBehaviour {
MyUtilities utils;
// Use this for initialization
void Start ()
{
utils = new MyUtilities();
if(utils.StartRead())
print("Success");
else
print("Failed");
}
// Update is called once per frame
void Update ()
{
string s = utils.GetStatus();
if ( s != null)
print(s);
else
print("Crap!");
}
}
The 2 classes in the DLLTest
using System;
using USBTransfer;
namespace DLLTest
{
public class MyUtilities
{
private HardwareComm hardwareComm;
private string lastData = "";
bool newData = false;
public bool StartRead()
{
// How to use the HardwareComm
hardwareComm = new HardwareComm();
hardwareComm.Received += DataReceive;
bool link = hardwareComm.startLinking();
return link;
}
public string GetStatus()
{
if (newData)
{
newData = false;
return lastData;
}
return null;
}
// Receive data
private void DataReceive(object sender, String data)
{
newData = true;
lastData = data;
}
}
}
This second class has the read logic
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
namespace USBTransfer
{
public delegate void DataReceiveHandler(object sender, String data);
class HardwareComm
{
// Event handler
public event DataReceiveHandler Received;
private Thread th;
// Import DLL
[DllImport("AID")]
static extern uint FT_ListDevices();
[DllImport("AID")]
static extern uint FT_Open();
[DllImport("AID")]
static extern uint FT_Close();
[DllImport("AID")]
static extern uint FT_Write([MarshalAs(UnmanagedType.LPArray)] byte[] p_data, ulong size);
[DllImport("AID")]
static extern uint FT_SetBaudRate(ulong data);
[DllImport("AID")]
static extern uint FT_GetStatus(ref ulong rxsize, ref ulong txsize);
[DllImport("AID")]
static extern uint FT_SetBitMode(byte mask, byte enable);
[DllImport("AID")]
static extern uint FT_Read([MarshalAs(UnmanagedType.LPArray)] byte[] p_data, ulong size);
[DllImport("AID")]
static extern uint FT_EE_Read(ref ushort vid, ref ushort pid, ref ushort power);
[DllImport("AID")]
static extern uint FT_EE_Program(ushort power);
[DllImport("AID")]
static extern uint FT_EE_ProgramToDefault();
[DllImport("AID")]
static extern uint KCAN_Send(uint channel, uint id, uint dlc, [MarshalAs(UnmanagedType.LPArray)] byte[] p_data);
[DllImport("AID")]
static extern uint KCAN_Receive(ref uint channel, ref uint id, ref uint dlc, [MarshalAs(UnmanagedType.LPArray)] byte[] p_data);
[DllImport("AID")]
static extern uint KCAN_Init(uint baudraute);
public HardwareComm()
{
}
///
/// This method link to the hardware.
/// Return false if no devices connected, true otherwise.
///
public Boolean startLinking()
{
uint nbOfDevices = FT_ListDevices();
if (nbOfDevices < 1)
{
// Don't detect the board game
return false;
}
FT_Open(); // Open RS232 PORT equivalent
FT_SetBaudRate(9600); // Set baud rate to 9600, 8bit(default), no parity(default), no protection(default)
//Start thread
th = new Thread(LoopReading);
th.Start();
return true;
}
///
/// Structure to receive data
///
protected virtual void OnReceive(String data)
{
if (Received != null)
Received(this, data);
}
private void LoopReading()
{
while (true)
{
byte[] data = new Byte[64];
uint i, length;
String temp = "";
length = FT_Read(data, 1);
for (i = 0; i < length; i++)
{
temp += Convert.ToString(data[i], 16);
}
OnReceive(temp);
}
}
}
}
Note that this code works. If I play the game and then start the device, the numbers will show in the console. However, if I start the device and then click play, the Editor will freeze before printing anything. Also, even if the editor is stopped and well, if I make any change in my test script, the Editor freeze when I switch back to it. The only way to continue is to kill Unity in the task manager and restart it.
I'm working with Unity 4.2 with the Pro trial on Windows 7
What am I doing wrong here?
Thank you for your help.
↧