Accessing Bluetooth device over virtual serial port - does not work in Unity

I am trying to access a BlueSentry wireless bluetooth module, which is normally accessible through a virtual serial port. I managed to receive the data in Visual C# Express 2010, using the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace BluetoothTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort sp = new SerialPort("COM13", 9600, Parity.None, 8, StopBits.One);
            sp.Open();
            sp.Write("2");

            char[] strm = new char[15];

            while (true)
            {
                sp.Write("*");
                sp.Read(strm, 0, 15);
                if (strm[0] == '-')
                {
                    for (int i = 0; i < strm.Length; i++)
                    {
                        Console.Write(strm[i]);
                        if (i == strm.Length - 1)
                        {
                            Console.Write(strm[i] + "\n");
                        }
                    }
                }
            }
        }
    }
}

However, when I tried accessing the device through Unity, I got the following exception:

IOException: The port `COM13' does not exist.

The C# scipt I am using is the following:

using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

public class Bluetooth : MonoBehaviour {
	
	SerialPort sp;

	char[] strm = new char[15];
	
	void Start(){
		sp = new SerialPort("COM13", 9600, Parity.None, 8, StopBits.One);
		sp.Open();
		sp.Write("2");
	}

	void Update(){
		sp.Write("*");
		sp.Read(strm, 0, 15);

		if (strm[0] == '-')
		{
			for (int i = 0; i < strm.Length; i++)
			{
				Debug.Log(strm[i]);
				if (i == strm.Length - 1)
				{
					Debug.Log(strm[i] + "\n");
				}
			}
		}
	}
}

I would appreciate any input.

first check the correct name serial port SerialPort.GetPortNames Method (System.IO.Ports) | Microsoft Learn

I did that. The port exists. I get a Debug statement returning “COM13” using the above method (plus, COM13 is listed in the Device Manager whenever the bluetooth receiver is plugged in, PLUS the same - more or less - code works fine in Visual C#). This is immediately followed by the exception that says that COM13 does not exist. Makes no sense…

here’s my Start() function:

void Start(){

		string[] ports = SerialPort.GetPortNames();
		for(int i=0; i<ports.Length; i++)
			Debug.Log (ports[i]); // This returns "COM12" and "COM13"

		sp = new SerialPort(ports[1], 9600, Parity.None, 8, StopBits.One); // This says COM13 doesn't exist
		sp.Open();
		sp.Write("2");
	}

SOLVED

I needed to use a special syntax to access COM ports higher than COM9: “\\.\COM13” instead of simply “COM13”.

1 Like

You saved my life dude, thank you :slight_smile:

1 Like

Thank you SilverWizard, worked like a charm! :smile: Saved our project! :slight_smile: