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
Related
I have a MCU that is sending data to serial port. I want Unity3D to read the data. So far, nothing I have found works and I'm looking for some help on getting Unity to read the serial port.
Expected behavior is for the serial data to be saved to a string in order to perform operations with the data contained.
At the moment, when I run the Unity editor everything is fine until I actually try to read the serial port. As soon as an attempt is made to read the serial port the Unity editor totally freezes. There are no error logs, no console errors within the editor (since it is frozen), and the editor does not respond to my inputs (clicks, scrolls, etc.).
The solution at this point is to open the task manager and end the task for the Unity editor.
My current implementation is utilizing threads, however, I'm looking for the best practice to read serial port. Perhaps coroutines? Anyway, here's what I have so far.
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.IO.Ports;
using UnityEngine;
public class Arduino_Read_Serial : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private bool enableReadThread = false;
private SerialPort data_stream = new SerialPort("COM6", 115200);
[SerializeField] private string receivedString;
private Thread readThread;
void Start()
{
data_stream.WriteTimeout = 300;
data_stream.ReadTimeout = 5000;
data_stream.DtrEnable = true;
data_stream.RtsEnable = true;
data_stream.Open();
if (data_stream.IsOpen)
{
readThread = new Thread(ReadThread);
}
}
void Update()
{
if (enableReadThread && data_stream.IsOpen)
{
readThread.Start();
enableReadThread = false;
}
else
{
Debug.Log("Pulse...");
}
if (receivedString != "")
{
//readThread.
}
//string[] datas = receivedString.Split(',');
//Debug.Log(datas);
}
void ReadThread()
{
while (data_stream.IsOpen)
{
receivedString = data_stream.ReadLine();
}
}
public void OnApplicationQuit()
{
Debug.Log("Thread stopped...");
readThread.Abort();
}
}
Any insight on some best practices for reading the serial port in Unity would be supremely appreciated.
These are the answers I attempted to use but none of them are working:
[1] Unity Serial.ReadLine() issue
[2] https://answers.unity.com/questions/1696938/arduino-serial-delegate-help.html
[3] https://answers.unity.com/questions/1735855/unity-crashes-when-connecting-to-a-serial-port.html
[4] https://forum.unity.com/threads/cant-use-system-io-ports.141316/
[5] https://www.youtube.com/watch?v=5ElKFY3N1zs
I´m looking for a way to activate the configuration and update the boot project via C#.
My Twincat 3 project is compiled and all necessary file are in the /_Boot folder.
Next step is a C# programm (actually unit tests) that loads and executes the project on my PLC.
So far I have read through Beckhoff Information System, but couldn´t find any hint.
You need the Twincat Automation Interface API in order to activate your configuration and start the PLC.
An example from the official documentation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE100;
using System.IO;
using TCatSysManagerLib;
namespace ActivatePreviousConfiguration{
class Program
{
static void Main(string[] args)
{
Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(t);
dte.SuppressUI = false;
dte.MainWindow.Visible = true;
EnvDTE.Solution sol = dte.Solution;
sol.Open(#"C:\Temp\SolutionFolder\MySolution1\MySolution1.sln");
EnvDTE.Project pro = sol.Projects.Item(1);
ITcSysManager sysMan = pro.Object;
sysMan.ActivateConfiguration();
sysMan.StartRestartTwinCAT();
}
}
}
There are also many other things you can do with this api, for example generate code for your PLC..
You can find the documentation here:
Automation Interface pdf
If you only have the _Boot folder at your disposal, you just have to copy the content of _Boot\TwinCAT RT(x64)\Plc to your target boot folder C:\TwinCAT\3.1\Boot\Plc and start the PLC via ADS-Command.
The PLC will boot with the replaced compiled project.
Here an example from the official ADS-Documentation for starting the plc:
static void Main(string[] args)
{
//Create a new instance of class TcAdsClient
TcAdsClient tcClient = new TcAdsClient();
try
{
// Connect to local PLC - Runtime 1 - TwinCAT2 Port=801, TwinCAT3 Port=851
tcClient.Connect(851);
Console.WriteLine(" PLC Run\t[R]");
Console.WriteLine(" PLC Stop\t[S]");
Console.WriteLine("\r\nPlease choose \"Run\" or \"Stop\" and confirm with enter..");
string sInput = Console.ReadLine().ToLower();
//Process user input and apply chosen state
do{
switch (sInput)
{
case "r": tcClient.WriteControl(new StateInfo(AdsState.Run, tcClient.ReadState().DeviceState)); break;
case "s": tcClient.WriteControl(new StateInfo(AdsState.Stop, tcClient.ReadState().DeviceState)); break;
default: Console.WriteLine("Please choose \"Run\" or \"Stop\" and confirm with enter.."); sInput = Console.ReadLine().ToLower(); break;
}
} while (sInput != "r" && sInput != "s");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
finally
{
tcClient.Dispose();
}
}
I'm trying to pass messages with NetMQ in C# UWP to python.
The python acts as Subscriber, and the C# as Publisher.
When I use C# .Net Core, I can see messages get to the python subscriber, but when I use C# UWP, nothing happens, though the code is exactly the same and I can see Publisher is sending the messages.
The code in python: (Working)
import zmq
import time
def subscribe():
port = "6789"
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:%s" % port)
topicfilter = "abcde"
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
while True:
string = socket.recv()
print string
subscribe()
The code in .Net Core: (Working)
using System.Threading;
using System.Threading.Tasks;
using NetMQ;
using NetMQ.Sockets;
namespace Examples
{
static partial class Program
{
public static void Main(string[] args)
{
Publisher();
}
public static void Publisher()
{
Task.Run(async () =>
{
using (var pubSocket = new PublisherSocket())
{
pubSocket.Bind("tcp://*:6789");
for (var i = 0; i < 10; i++)
{
pubSocket.SendFrame("abcde" + i.ToString());
Thread.Sleep(1000);
}
}
});
}
}
}
But the code in UWP (Not working):
using NetMQ;
using NetMQ.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System;
namespace test_NetMQ_UWP
{
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
DataContext = this;
}
// this event happen when I click on a button in MainPage.xaml
private void Publisher_Click(object sender, RoutedEventArgs e)
{
Task.Run(async () =>
{
using (var pubSocket = new PublisherSocket())
{
pubSocket.Bind("tcp://*:6789");
for (var i = 0; i < 10; i++)
{
pubSocket.SendFrame("abcde" + i.ToString());
Thread.Sleep(1000);
}
}
});
}
}
}
What am I doing wrong?
It's normal behavior. You're using an IP loopback address for Network communications between a UWP app and a different process (a different UWP app or a desktop app). This is restricted by network isolation.
You could run your server and client on different machine to test. Please see the document How to enable loopback and troubleshoot network isolation (Windows Runtime apps). It has explained this scenario:
Loopback is permitted only for development purposes. Usage by a Windows Runtime app installed outside of Visual Studio is not permitted. Further, a Windows Runtime app can use an IP loopback only as the target address for a client network request. So a Windows Runtime app that uses a DatagramSocket or StreamSocketListener to listen on an IP loopback address is prevented from receiving any incoming packets.
In your case, if you just want to test if the UWP app can send message to your python subscriber successfully. You could run the UWP app on another machine. I used your code to make a UWP app to send message and make a console application as subscriber which is run on a different machine. The console application can receive the message.
Please note that because your UWP app need to access the Network at runtime, you need to enable the Netwrok capabilities(Internet(Client) Internet(Client & Server) Private Networks(Client & Server)) in Package.appxmanifest file.
I want to run the Skeinforge slicer program written in Python inside my Windows Phone 8 C# application. I have determined that I should probably use IronPython to do this, I have already determined that I can run Skeinforge inside the ipy.exe terminal I got when I installed IronPython. My problem though is that I am struggling to figure out how to host and run a Python script with IronPython inside Windows Phone 8. I have also already managed to get a simple hello world script running inside a Desktop Windows Forms application that transfers the applications console output to the Debug console with the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DebugWriter debugW = new DebugWriter();
Console.SetOut(debugW);
}
private void button1_Click(object sender, EventArgs e)
{
TextWriter tw = new StreamWriter("Test.py");
tw.Write(scriptBox.Text);
tw.Close();
try
{
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("Test.py");
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
}
}
}
}
And this is the DebugWriter:
class DebugWriter : TextWriter
{
private StringBuilder content = new StringBuilder();
public DebugWriter()
{
Debug.WriteLine("Writing console to debug");
}
public override void Write(char value)
{
base.Write(value);
if (value == '\n')
{
Debug.WriteLine(content.ToString());
content = new StringBuilder();
}
else
{
content.Append(value);
}
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
I have no idea how to even add the IronPython libraries to my Windows Phone 8 application though as the standard libraries won't import. I have though tried compiling the apparently now defunct Windows Phone 7 libraries with the master source code and I can import these libraries, but I get absolutely no response on the debug terminal when I try to run my hello world script.
Do any of you have any idea how to get this woring in Windows Phone 8, if you know how to do this in Windows 8/Metro/RT then that would also probably work for WP8.
UPDATE:
I have looked at the debug output again and I seem to get this error when trying to use the WP7 libraries to run a hello world script:
A first chance exception of type 'System.NotImplementedException' occurred in Microsoft.Scripting.DLL
Error: The method or operation is not implemented.
I managed to get Skeinforge running on a modified version of IPY. You can get the source for my application here: http://skeinforgewp8.codeplex.com/
I have a library that handles reading and writing a cache file. This library is used by a Windows Service and several instances of a console application on the same machine. The console application runs when a user logs in.
I am getting occasional IO errors saying the cache file is in use by another process. I assume that collisions are occurring between the different application instances and service trying to read and write at the same time.
Is there a way to lock the file when it is in use and force all other requests to "wait in line" to access the file?
private void SaveCacheToDisk(WindowsUser user) {
string serializedCache = SerializeCache(_cache);
//encryt
serializedCache = AES.Encrypt(serializedCache);
string path = user == null ? ApplicationHelper.CacheDiskPath() :
_registry.GetCachePath(user);
string appdata = user == null ? ApplicationHelper.ClientApplicationDataFolder() :
_registry.GetApplicationDataPath(user);
if (Directory.Exists(appdata) == false) {
Directory.CreateDirectory(appdata);
}
if (File.Exists(path) == false) {
using (FileStream stream = File.Create(path)) { }
}
using (FileStream stream = File.Open(path, FileMode.Truncate)) {
using (StreamWriter writer = new StreamWriter(stream)) {
writer.Write(serializedCache);
}
}
}
private string ReadCacheFromDisk(WindowsUser user) {
//cache file path
string path = user == null ? ApplicationHelper.CacheDiskPath() :
_registry.GetCachePath(user);
using (FileStream stream = File.Open(path, FileMode.Open)) {
using (StreamReader reader = new StreamReader(stream)) {
string serializedCache = reader.ReadToEnd();
//decrypt
serializedCache = AES.Decrypt(serializedCache);
return serializedCache;
}
}
}
Sure, you could use a mutex and permit access only when holding the mutex.
You could use a cross-process EventWaitHandle. This lets you create and use a WaitHandle that's identified across processes by name. A thread is notified when it's its turn, does some work, and then indicates it's done allowing another thread to proceed.
Note that this only works if every process/thread is referring to the same named WaitHandle.
The EventWaitHandle constructors with strings in their signature create named system synchronization events.
One option you could consider is having the console applications route their file access through the service, that way there's only one process accessing the file and you can synchronise access to it there.
One way of implementing this is by remoting across an IPC channel (and here's another example from weblogs.asp.net). We used this technique in a project for the company I work for and it works well, with our specific case providing a way for a .net WebService to talk to a Windows Service running on the same machine.
Sample based on the weblogs.asp.net example
Basically what you need to do with the code below is create a Solution, add two Console Apps (one called "Server" and the other called "Client" and one Library to it. Add a reference to the Library to both console apps, paste the code below in and add a reference to System.Runtime.Remoting to both Server & Console.
Run the Server app, then run the client app. Observe the fact that the server app has a message passed to it by the client. You can extend this to any number of messages/tasks
// Server:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
namespace RemotingSample
{
public class Server
{
public Server()
{
}
public static int Main(string[] args)
{
IpcChannel chan = new IpcChannel("Server");
//register channel
ChannelServices.RegisterChannel(chan, false);
//register remote object
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(RemotingSample.RemoteObject),
"RemotingServer",
WellKnownObjectMode.SingleCall);
Console.WriteLine("Server Activated");
Console.ReadLine();
return 0;
}
}
}
// Client:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using RemotingSample;
namespace RemotingSample
{
public class Client
{
public Client()
{
}
public static int Main(string[] args)
{
IpcChannel chan = new IpcChannel("Client");
ChannelServices.RegisterChannel(chan);
RemoteObject remObject = (RemoteObject)Activator.GetObject(
typeof(RemotingSample.RemoteObject),
"ipc://Server/RemotingServer");
if (remObject == null)
{
Console.WriteLine("cannot locate server");
}
else
{
remObject.ReplyMessage("You there?");
}
return 0;
}
}
}
// Shared Library:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
namespace RemotingSample
{
public class RemoteObject : MarshalByRefObject
{
public RemoteObject()
{
Console.WriteLine("Remote object activated");
}
public String ReplyMessage(String msg)
{
Console.WriteLine("Client : " + msg);//print given message on console
return "Server : I'm alive !";
}
}
}
Check out the TextWriter.Synchronized method.
http://msdn.microsoft.com/en-us/library/system.io.textwriter.synchronized.aspx
This should let you do this:
TextWriter.Synchronized(writer).Write(serializedCache);