Serial Port Reading using Unity and dsPIC33F

I am sending 14bytes of data from the dsPIC side where the 14th byte is newline. Now how can I read the serialport data using Unity5 Game engine. I have read several blogs using various methods (using read(), readline(), readBytes() and so on). A common suggestion is to use serial.Read and when I tried this, the Unity is getting stuck. The sample program listed below also cause the same issue. Could someone provide a proper way of handling this.

Thanks and Regards, Akhil.

//Basic libraries
using UnityEngine;
using System.Collections;
#region Using directives

//Other libraries
using System;
using System.Collections.Generic;
using System.ComponentModel;

//Serial Port library.
using System.IO.Ports;

#endregion

public class MoveRacket : MonoBehaviour
{
public float speed      = 30;
public string axis      = "Vertical";
private SerialPort serial;
//public Action<byte[]> DataReceived;

void Start ()
{
    serial = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
    if (!serial.IsOpen)
    {
        try
        {
            serial.Open();
            Debug.Log("Port Opened!");
        }
        catch(UnityException e)
        {
            // Basic exception handling
            Debug.LogError(e.Message);
        }
    }
    else
        Debug.LogError("Port already open.");
}

void FixedUpdate ()
{
    string tempS = serial.ReadLine();

    if (tempS!="") {
        Debug.Log("serial out "+tempS);
    }
}
void End()
{
    // Close the port when the program ends.
    if (serial.IsOpen)
    {
        try
        {
            serial.Close();
            Debug.Log("Port closed!");
        }
        catch(UnityException e)
        {
            Debug.LogError(e.Message);
        }
    }
  }
}

What does serial.ReadLine outputs and what does temps outputs in debug log?

Serial. readine() suppose to output the bytes that I am sending through UART, but the unity freezes before the in the readline statement kind of indicating there is nothing to read (and that is what displaying on the debug log)

Sorry but this is way out of my league for now :slight_smile:
good luck

hmm, anyway thanks for your time :slight_smile:

Are you sure you have the baud rate correct?

Also, ReadLine waits for a newline symbol. Is that really what you want? Is this a keyboard you are reading from or a controller?

Yea, the baud rate is correct. I am sending data from the microcontroller.
Thanks :slight_smile:

Since ReadLine is synchronous and your code is blocked here, then I can only assume your source is not providing a newline symbol. Try the following:

for(byte b = serial.ReadByte(); b >= 0; b = serial.ReadByte())
  Debug.Log(b);
2 Likes

You should really use USB output on your board - and flashed to be a HID device. Most computers nowdays don’t even have a serial output, and those that do, only have one - which is occupied by your board. Since you’re using PIC, you can easily find a replacement chip that also supports USB among other things. Further, I imagine your board is some kind of controller, possibly MIDI or outright game controller, both of which can be implemented as driverless HID device (midi keyboard or pedal, mouse, joystick, racing wheel, etc.) that are universally supported by all modern OSes with no need to set anything up or write special scripts.

@eisenpony , that is of real help man, it starts reading the data, let me verify and tell you the progress :slight_smile: