I am going to make a basic communication between Unity and Arduino. The problem that i have is that when i start sending data from unity to arduino, it's working properly. But when i start reading serial from Arduino. I got Access denied error !!!
After i read more about the issue, i can't find the real reason for that. I need to understand why something like that happens.
using UnityEngine;
using System.Collections;
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
public class Program : MonoBehaviour {
public SerialPort mySerialPort = new SerialPort("COM7", 9600);
public volatile float speed=0;
volatile bool isPassed = true;
GameObject cube ;
public GUIStyle style ;
// int readInterval = 4;
// int alreadyCounter = 0;
// string rxString = "";
void Start () {
isPassed = true;
speed = 0;
//mySerialPort.ReadTimeout = 25;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.DtrEnable = true;
cube = GameObject.FindGameObjectWithTag ("cube");
/*foreach(string str in SerialPort.GetPortNames())
{
Debug.Log(string.Format("Existing COM port: {0}", str));
};
*/
try{
mySerialPort.Open();
}catch(Exception ex)
{
print("Start Error Opening : " + ex.ToString());
}
/*
new Thread (delegate() {
updateSpeed();
}).Start();
*/
}
void OnGUI(){
GUI.Box (new Rect(100,100,100,100),"Speed : " + speed , style);
}
// Update is called once per frame
void Update () {
cube.transform.Rotate(Vector3.up * speed *Time.deltaTime);
if (mySerialPort.IsOpen) {
speed = float.Parse (mySerialPort.ReadLine ());
} else {
if(mySerialPort!= null)
{
mySerialPort.Close();
mySerialPort.Open();
}
}
/*if (isPassed == true) {
new Thread(delegate() {
if(mySerialPort.IsOpen) {
updateSpeed();
isPassed = false;
} else {
try {
mySerialPort.Close();
mySerialPort.Open();
} catch (Exception ex) {
print("Update Error Opening : " + ex.ToString());
}
}
}).Start();
}*/
}
public void updateSpeed()
{
while (true) {
speed = float.Parse(mySerialPort.ReadLine ());
print ("Speed = " + speed);
Thread.Sleep (60);
mySerialPort.BaseStream.Flush ();
}
}
void OnDestroy ()
{
mySerialPort.Close();
}
}
When you call Close the SerialPort is disposed as you can read on the MSDN page. It also suggest to not try to reopen it immediately after closing it as it might not be closed yet.
Also since the SerialPort will be disposed you would have to create a new one. I would suggest to not closing and opening the port in Update when it's not open yet. Open it once in Start and just close it in OnDestroy.
edit
It seems you don't have to create a new SerialPort when you called Close on it. So it can be reused after "waiting some time".
Update:
Now, you can use Microsoft .net open source framework instead of the unity default, the serial communication works like a charm there.
Related
I am currently trying to build a windows forms app that gets sensor data from an arduino via the serial com.
when checking in the arduino IDE the data gets writen into the serial port correctly.
But i can't figure out how to read the data via c#.
class Program
{
static SerialPort SP;
static void Main(string[] args)
{
SP = new SerialPort();
SP.PortName = "COM7";
SP.BaudRate = 9600;
SP.Handshake = System.IO.Ports.Handshake.RequestToSend;
SP.Open();
while (true)
{
Console.WriteLine(DateTime.Now.ToString() + " : " + SP.ReadLine());
}
}
}
My guess is that the Port is not properly set up, but i have no idea what i am missing.
The Goal is just to receive strings from the arduino, i do not necessarily need to send any data to the arduino.
edit: i am working with an arduino micro
Did you close Arduino IDE?
You need to add a wait code before reading from the port
Below is a working example:
private SerialPort _currentPort = new SerialPort("COM7", 9600);
private readonly object _sync = new object();
public bool Open()
{
_currentPort.Encoding = Encoding.UTF8;
_currentPort.DtrEnable = true;
_currentPort.ReadTimeout = 2000;
try
{
if (!_currentPort.IsOpen)
lock (_sync)
{
if (_currentPort.IsOpen)
return true;
_currentPort.Open();
System.Threading.Thread.Sleep(1500);
}
}
catch (Exception e)
{
//_localLogger?.Error($"{_currentPort.PortName}, {e.Message}", e);
return false;
}
return _currentPort.IsOpen;
}
public bool Subscribe()
{
try
{
if (Open())
{
_currentPort.DataReceived += CurrentPortOnDataReceived;
return true;
}
return false;
}
catch (Exception e)
{
//_localLogger?.Error($"{_currentPort.PortName}, {e.Message}", e);
return false;
}
}
private void CurrentPortOnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (!_currentPort.IsOpen)
{
//_localLogger.Info($"{_currentPort} is closed");
Open();
}
Console.WriteLine(_currentPort.ReadExisting());
}
I got stuck in solving a problem in my current project. I'm coding a simulation in WPF/C# and included a Unity-Application in the window using the window-handles/hwnd pretty much like "Programmer" did in this answer. He also suggested namedpipes for the communication.
But the communication just works in one way - The server (WPF-App) is able to send messages to the client (the embedded Unity-App). If I try it the other way around, the whole Unity-App hungs up and stops working. As far as I was able to figure it out, as soon as I flush the writer or add the AutoFlush and then write something Unity gets stuck.
It is interesting, that the InBufferSize of the server is 0. Might this be the Problem?
This is my C#-Script in Unity assigned to a GameObject:
public class pipeCommunicationScript : MonoBehaviour
{
NamedPipeClientStream client;
public GameObject console;
static Text consoleText;
StreamReader reader;
StreamWriter writer;
int counter = 0;
static bool _threadRunning;
Thread _thread;
void Start()
{
consoleText = console.GetComponent<Text>();
consoleText.text = "trying to set up client";
_thread = new Thread(StartClient);
_thread.Start();
}
void StartClient()
{
_threadRunning = true;
//Client
consoleText.text = "Attempting to connect to pipe...";
client = new NamedPipeClientStream(".","UnitySimulationPipe", PipeDirection.InOut);
if (!client.IsConnected) { client.Connect(); }
consoleText.text = "Connected to pipe.";
writer = new StreamWriter(client);
reader = new StreamReader(client);
writer.AutoFlush = true;
InvokeRepeating("sendThroughPipe", 5.5f, 5.5f);
while (_threadRunning)
{
consoleText.text = "IN:" + reader.ReadLine();
}
_threadRunning = false;
}
void sendThroughPipe()
{
try
{
client.WaitForPipeDrain();
writer.WriteLine("test" + counter);
}
catch(Exception e)
{
consoleText.text = "Exception while sending: " + e;
}
consoleText.text = "OUT: test" + counter;
counter++;
}
void OnDisable()
{
if (_threadRunning)
{
_threadRunning = false;
_thread.Join();
}
}
}
And this my Server-Script:
void StartServer()
{
Task.Factory.StartNew(() =>
{
Console.WriteLine("Connection initialisation started");
var server = new NamedPipeServerStream("UnitySimulationPipe", PipeDirection.InOut);
server.WaitForConnection();
while (!server.IsConnected)
{
Console.WriteLine("Connection not initialized");
server.WaitForConnection();
}
Console.WriteLine("Connection initialized");
reader = new StreamReader(server);
writer = new StreamWriter(server);
writer.AutoFlush = true;
writer.WriteLine("startConnection");
printOnStatusBar("OUT: startConnection");
connected = true;
Console.WriteLine("Current pipe readable: " + server.CanRead);
Console.WriteLine("Pipe InBufferSize: " + server.InBufferSize);
while (true)
{
Console.WriteLine("start while again");
if (reader.Peek() > 0)
{
var l = reader.ReadLine();
Console.WriteLine(reader.ReadLine());
Console.WriteLine("read finished");
server.WaitForPipeDrain();
}
Thread.Sleep(500);
}
});
}
The most probable answer is, in fact, what you mentionned (bufferSize).
You should check for the initialyzers parameters of every object you use.
Don't worry, I'm sure you'll find the answer just by searching there. I used to create such code in the same way, it should work well.
Okay, I changed my initilisation of the server-object to (explanation of different parameters here):
var server = new NamedPipeServerStream("UnitySimulationPipe", PipeDirection.InOut, 1, 0, 0, 1024, 1024);
But I personally think, that this was just a minor mistake. The mainreason why my code didn't worked was, that I missed to call
server.WaitForPipeDrain();
after
writer.WriteLine("startConnection");
printOnStatusBar("OUT: startConnection");
in my server-code.
I am testing node.js as TCP server and Unity3d as client for my up coming project.
I create a simple chat interface with unity3d that can send and receive data from the server. Everything seems to work fine until I test with 2 clients, one in Unity editor and another with Unity application, whenever I send the message, the server immediately pickup and display in terminal. But the client side only update the display interface when I click to focus on the client, be it Unity editor or the Unity application itself.
Below are my codes:
Node.js
net = require('net');
var clients = [];
net.createServer(function (socket) {
socket.name = socket.remoteAddress + ":" + socket.remotePort
clients.push(socket);
socket.write("Welcome " + socket.name + "\n");
broadcast(socket.name + " joined the chat\n", socket);
socket.on('data', function (data) {
broadcast(socket.name + "> " + data, socket);
});
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
broadcast(socket.name + " left the chat.\n");
});
function broadcast(message, sender) {
clients.forEach(function (client) {
if (client === sender) return;
client.write(message);
});
process.stdout.write(message)
}
}).listen(5000);
// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5000\n");
client.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Net.Sockets;
using System.IO;
public class client : MonoBehaviour {
private bool socketReady;
private TcpClient socket;
private NetworkStream stream;
private StreamWriter writer;
private StreamReader reader;
public Text servermessage;
public InputField input;
// Use this for initialization
void Start () {
ConnectToServer ();
}
// Update is called once per frame
void Update () {
if(socketReady){
if(stream.DataAvailable){
string data = reader.ReadLine ();
if(data != null){
OnIncomingData (data);
}
}
}
}
public void ConnectToServer(){
if(socketReady)
return;
string host = "127.0.0.1";
int port = 5000;
try
{
socket = new TcpClient(host,port);
stream = socket.GetStream();
writer = new StreamWriter(stream);
reader = new StreamReader(stream);
socketReady = true;
Debug.Log("socket :"+ reader.ToString());
}
catch (System.Exception e)
{
Debug.Log("socket error :"+ e);
}
}
private void OnIncomingData(string data){
Debug.Log ("server : "+ data);
servermessage.text = data;
}
public void OnOutgoingData(string data){
if (!socketReady)
return;
if (input.text == "") {
writer.WriteLine (data);
} else {
writer.WriteLine (input.text);
}
writer.Flush ();
input.text = "";
}
}
What is missing? How can I make it that I can see the immediate update on all my client interface?
This is not caused by your code, but due to the Unity editor. By default the unity editor does not run applications in the background.
You can turn run-in-background on inEdit > Project Settings > Settings-Player
There should be a field Run in background. Turn it on, and it should update even when you are not focused
See Also Run In background
I have an issue about the server-client communication.
I googled around but I did not find a solution to this.
Right now I am using 32feet in order to get in touch 2 or more (till 7) BT clients to 1 BT server.
I need to broadcast a message from the server to every device in the same time, but I don't know how to do it.
The only way I figured out was to use the list of connection in order to send the message one per time, but it means a delay between each message sent (around 100 ms per device). Unfortunately it means to have a large delay on the last one.
Can someone please give me an advice on how to solve this problem?
Is there a way to broadcast the message to all devices in the same time?
If it can be helpfull, here there is the handle of connection and reading from devices.
Thanks for your help
private void btnStartServer_Click(object sender, EventArgs e)
{
btnStartClient.Enabled = false;
ConnectAsServer();
}
private void ConnectAsServer()
{
connessioniServer = new List<BluetoothClient>();
// thread handshake
Thread bluetoothConnectionControlThread = new Thread(new ThreadStart(ServerControlThread));
bluetoothConnectionControlThread.IsBackground = true;
bluetoothConnectionControlThread.Start();
// thread connessione
Thread bluetoothServerThread = new Thread(new ThreadStart(ServerConnectThread));
bluetoothServerThread.IsBackground = true;
bluetoothServerThread.Start();
}
private void ServerControlThread()
{
while (true)
{
foreach (BluetoothClient cc in connessioniServer)
{
if (!cc.Connected)
{
connessioniServer.Remove(cc);
break;
}
}
updateConnList();
Thread.Sleep(0);
}
}
Guid mUUID = new Guid("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");
private void ServerConnectThread()
{
updateUI("server started");
BluetoothListener blueListener = new BluetoothListener(mUUID);
blueListener.Start();
while (true)
{
BluetoothClient conn = blueListener.AcceptBluetoothClient();
connessioniServer.Add(conn);
Thread appoggio = new Thread(new ParameterizedThreadStart(ThreadAscoltoClient));
appoggio.IsBackground = true;
appoggio.Start(conn);
updateUI(conn.RemoteMachineName+" has connected");
}
}
private void ThreadAscoltoClient(object obj)
{
BluetoothClient clientServer = (BluetoothClient)obj;
Stream streamServer = clientServer.GetStream();
streamServer.ReadTimeout=1000;
while (clientServer.Connected)
{
try
{
int bytesDaLeggere = clientServer.Available;
if (bytesDaLeggere > 0)
{
byte[] bytesLetti = new byte[bytesDaLeggere];
int byteLetti = 0;
while (bytesDaLeggere > 0)
{
int bytesDavveroLetti = streamServer.Read(bytesLetti, byteLetti, bytesDaLeggere);
bytesDaLeggere -= bytesDavveroLetti;
byteLetti += bytesDavveroLetti;
}
updateUI("message sent from "+clientServer.RemoteMachineName+": " + System.Text.Encoding.Default.GetString(bytesLetti));
}
}
catch { }
Thread.Sleep(0);
}
updateUI(clientServer.RemoteMachineName + " has gone");
}
private void updateUI(string message)
{
Func<int> del = delegate()
{
textBox1.AppendText(message + System.Environment.NewLine);
return 0;
};
Invoke(del);
}
private void updateConnList()
{
Func<int> del = delegate()
{
listaSensori.Items.Clear();
foreach (BluetoothClient d in connessioniServer)
{
listaSensori.Items.Add(d.RemoteMachineName);
}
return 0;
};
try
{
Invoke(del);
}
catch { }
}
I don't exactly understand how you do it right now (the italian names are not helping...) but maybe my solution can help you.
first of all, bluetooth classic does not support broadcast. so you have to deliver at one at a time.
i do connect to 7 serial port devices at a time, using 7 threads. then i tell every thread to send data. this is very close to same time, but of course not exactly.
let me know if that helps or if you need a code example.
I am just a beginner in c#. I am now trying to interface arduino with a GUI application. And i need a small function to automatically detect the port which I have connected Arduino. I tried using nested "try and catch" blocks but it failed. can anyone suggest a good way to automatic select the port in which arduino is connected and open that port such that we can move directly to coding other switches that do different functions in that arduino.
Recently i had the same situation and i wrote this method to check for our device, all you need to set your device to send specific Pattern on Specific input. In this example if you send 0x33 then your device have to send 0x8A to identify itself.
public enum SerialSignal
{
SendSync = 0x33,
ReceiveSync = 0x8A,
}
private static SerialPort _arduinoSerialPort ;
/// <summary>
/// Polls all serial port and check for our device connected or not
/// </summary>
/// <returns>True: if our device is connected</returns>
public static bool Initialize()
{
var serialPortNames = SerialPort.GetPortNames();
foreach (var serialPortName in serialPortNames)
{
try
{
_arduinoSerialPort = new SerialPort(serialPortName) { BaudRate = 9600 };
_arduinoSerialPort.Open();
_arduinoSerialPort.DiscardInBuffer();
_arduinoSerialPort.Write(new byte[] { (int)SerialSignal.SendSync }, 0, 1);
var readBuffer = new byte[1];
Thread.Sleep(500);
_arduinoSerialPort.ReadTimeout = 5000;
_arduinoSerialPort.WriteTimeout = 5000;
_arduinoSerialPort.Read(readBuffer, 0, 1);
// Check if it is our device or Not;
if (readBuffer[0] == (byte)SerialSignal.ReceiveSync){
return true;
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception at Serial Port:" + serialPortName + Environment.NewLine +
"Additional Message: " + ex.Message);
}
// if the send Sync repply not received just release resourceses
if (_arduinoSerialPort != null) _arduinoSerialPort.Dispose();
}
return false;
}
public partial class Form1 : Form
{
SerialPort serial = new SerialPort();
static SerialPort cport;
public Form1()
{
InitializeComponent();
button1.Enabled = true;
button2.Enabled = false;
button3.Enabled = false;
}
private void button1_Click(object sender, EventArgs e)
{
int i;
try
{
string[] ports = SerialPort.GetPortNames();
foreach(string newport in ports)
{
cport = new SerialPort(newport, 9600);
cport.Open();
cport.WriteLine("A");
int intReturnASCII = serial.ReadByte();
char returnMessage = Convert.ToChar(intReturnASCII);
if (returnMessage == 'B')
{
button2.Enabled = true;
break;
}
else
{
cport.Close();
}
}
}
catch (Exception )
{
Console.WriteLine("No COM ports found");
}
}
I undertand that I'm a bit late, But I have created a simple and free C# NuGet library that allows for interaction between the host PC and an Arduino board!
Examples in the ReadMe.txt file.
ArduinoFace - NuGet