0

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);
        }
    }
  }
}
AM_87
  • 13
  • 5
  • at which point did your program hung? it looks to me that the `ReadLine()` will hang in `FixedUpdate` if no more data to read. – Paul Chen Jul 20 '15 at 03:15
  • In the fixed update it shows no data to be read and it hangs there. Thanks for your reply. – AM_87 Jul 20 '15 at 14:00

0 Answers0