I am trying to read a serial port with Unity and get a puzzling IOException.
The bare bones of the C# Class Serial Library dll is:
using System.IO.Ports;
namespace SerialLib
{
public class SerialClass
{
static SerialPort sp;
//Constructor
public SerialClass()
{
}
public string GetValue(string portName)
{
{
string result = “”;
sp = new SerialPort();
sp.PortName = portName;
sp.Open();
if (sp.IsOpen)
{
sp.WriteLine("?"); //Character to query serial device
result = sp.ReadTo("\r");
sp.Close();
}
return result;
}
}
}
}
A simple Windows Form to test this follows. This works fine!
using System.Windows.Forms;
using SerialLib;
namespace SerialTest
{
public partial class Form1 : Form
{
SerialClass myPort;
public Form1()
{
InitializeComponent();
myPort = new SerialClass();
string value =myPort.GetEncoderValue("COM11");
label1.Text = value;
}
}
}
However, similar code in Unity results in IOException: The port ‘COM11’ does not exist.
using UnityEngine;
using System.Collections;
using SerialLib;
public class SerialTestScript : MonoBehaviour {
SerialClass myPort;
// This method is for initialization and test.
void Start () {
myPort = new SerialClass();
string value =myPort.GetEncoderValue("COM11");
print(value);
}
}
Any help with solving this would be greatly appreciated.
Thanks!