Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serial Port Reading using Unity and dsPIC33F

Discussion in 'Scripting' started by 12407, Jul 19, 2015.

  1. 12407

    12407

    Joined:
    Jul 17, 2015
    Posts:
    11
    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.
    Code (CSharp):
    1. //Basic libraries
    2. using UnityEngine;
    3. using System.Collections;
    4. #region Using directives
    5.  
    6. //Other libraries
    7. using System;
    8. using System.Collections.Generic;
    9. using System.ComponentModel;
    10.  
    11. //Serial Port library.
    12. using System.IO.Ports;
    13.  
    14. #endregion
    15.  
    16. public class MoveRacket : MonoBehaviour
    17. {
    18. public float speed      = 30;
    19. public string axis      = "Vertical";
    20. private SerialPort serial;
    21. //public Action<byte[]> DataReceived;
    22.  
    23. void Start ()
    24. {
    25.     serial = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
    26.     if (!serial.IsOpen)
    27.     {
    28.         try
    29.         {
    30.             serial.Open();
    31.             Debug.Log("Port Opened!");
    32.         }
    33.         catch(UnityException e)
    34.         {
    35.             // Basic exception handling
    36.             Debug.LogError(e.Message);
    37.         }
    38.     }
    39.     else
    40.         Debug.LogError("Port already open.");
    41. }
    42.  
    43. void FixedUpdate ()
    44. {
    45.     string tempS = serial.ReadLine();
    46.  
    47.     if (tempS!="") {
    48.         Debug.Log("serial out "+tempS);
    49.     }
    50. }
    51. void End()
    52. {
    53.     // Close the port when the program ends.
    54.     if (serial.IsOpen)
    55.     {
    56.         try
    57.         {
    58.             serial.Close();
    59.             Debug.Log("Port closed!");
    60.         }
    61.         catch(UnityException e)
    62.         {
    63.             Debug.LogError(e.Message);
    64.         }
    65.     }
    66.   }
    67. }
     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    What does serial.ReadLine outputs and what does temps outputs in debug log?
     
  3. 12407

    12407

    Joined:
    Jul 17, 2015
    Posts:
    11
    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)
     
  4. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Sorry but this is way out of my league for now :)
    good luck
     
  5. 12407

    12407

    Joined:
    Jul 17, 2015
    Posts:
    11
    hmm, anyway thanks for your time :)
     
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    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?
     
  7. 12407

    12407

    Joined:
    Jul 17, 2015
    Posts:
    11
    Yea, the baud rate is correct. I am sending data from the microcontroller.
    Thanks :)
     
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    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:

    Code (csharp):
    1. for(byte b = serial.ReadByte(); b >= 0; b = serial.ReadByte())
    2.   Debug.Log(b);
     
    Vectrex and 12407 like this.
  9. raidho36

    raidho36

    Joined:
    Jan 4, 2014
    Posts:
    3
    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.
     
  10. 12407

    12407

    Joined:
    Jul 17, 2015
    Posts:
    11
    @eisenpony, that is of real help man, it starts reading the data, let me verify and tell you the progress :)