I am using Unity 3D for an application I need to use Serial Port but in Unity, there's a missing implementation of event DataReceived
and others linked functions. Therefore I think the solution is to make my own DLL that can manage Serial Port data and call it from unity as an external DLL.
I wrote the code below. the code in my class library project (just a serial port froze opening for the test)
Code:
using System;
using System.IO.Ports;
namespace serialPortManager2
{
public class laserManager
{
private SerialPort laserComPort;
public bool open()
{
bool laserSerialPortDetected = false;
// ouverture du port série pour pilotage du laser
foreach (string portName in SerialPort.GetPortNames())
{
if (portName == "COM4") laserSerialPortDetected = true;
}
if (laserSerialPortDetected)
{
laserComPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
laserComPort.Open();
if (laserComPort.IsOpen)
{
// timeout de lecture d'une mesure laser
laserComPort.ReadTimeout = 5000;
return true;
}
else return false;
}
else return false;
}
}
}
Code to reference DLL in Unity:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public static class SPNativePlugIn {
// The name of the external library containing the native functions
private const string LIBRARY_NAME = "serialPortManager2";
[DllImport(LIBRARY_NAME)] public static extern bool open();
}
My problem is that I get this error: EntryPointNotFoundException: open
I guess I have to integrate the laserManager
class somewhere but I do not know the syntax, I tried quite a few things but I can not find it.
Create a folder named "Plugins" in the "Assets" Folder then put your .dll file in the "Plugins" folder and import it in your script in this way using dllFileName;
This works for every .dll not include in the unity default library
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.