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
  4. Dismiss Notice

Yet another SerialPort thread...

Discussion in 'Scripting' started by niw3, Nov 10, 2015.

  1. niw3

    niw3

    Joined:
    Apr 7, 2015
    Posts:
    2
    Hi folks,

    I searched through many threads here after trying my own solution with regular SerialPort class of .NET/mono. In case anyone is looking for a similar simple solution here it is.

    TLDR Summary: I read from the serial port byte by byte into a buffer, where I consume it 5 bytes at a time.

    So I define a serialPort object like this:
    Code (CSharp):
    1. private System.IO.Ports.SerialPort serialPort;
    2. private List<byte> inputBuffer;
    3. private int messageLength;
    4.  
    5. void Start ()
    6. {
    7.     messageLength = 5;
    8.     inputBuffer = new List<byte>(1000);
    9.     serialPort = new System.IO.Ports.SerialPort();
    10.     serialPort.BaudRate = 115200;
    11.     serialPort.PortName = "COM7";
    12.     serialPort.ReadTimeout = 1;
    13.     try {
    14.         serialPort.Open();
    15.         if (!serialPort.IsOpen)
    16.             Debug.LogError("Could not open Serial Port");
    17.         else
    18.             Debug.Log("Serial port is open!");
    19.     }
    20.     catch(Exception exc)
    21.     {
    22.         Debug.LogException(exc);
    23.     }
    24. }
    So my Update becomes:

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4. // clear input buffer if already filled
    5. ReadFromInputBuffer();
    6.  
    7. int byteRead = 0;
    8. try
    9. {
    10. while (true)
    11. {
    12. byteRead = serialPort.ReadByte();
    13. if (byteRead == -1)
    14. break;
    15. inputBuffer.Add((byte)byteRead);
    16. }
    17. }
    18. catch
    19. {
    20. // timeout exceptions are supressed
    21. }
    22. }
    23.  
    24.  
    I read from the input buffer like this:
    Code (CSharp):
    1. private void ReadFromInputBuffer()
    2. {
    3.     while (inputBuffer.Count >= messageLength)
    4.     {
    5.         byte[] buffer = new byte[messageLength];
    6.         inputBuffer.CopyTo(0, buffer, 0, messageLength);
    7.         inputBuffer.RemoveRange(0, messageLength);
    8.      
    9.         // handle data in buffer ...
    10.     }
    11. }
    Cheers

    Edit: Some background:
    For some reason you cannot use DataReceived event or read BytesToRead property of SerialPort class in Unity.
     
  2. luigis

    luigis

    Joined:
    Oct 30, 2013
    Posts:
    25
    It's a hold post but seems the only real, rapid solution!! Thanks a lot!!
    So how it's work, why read from a buffer doesn't slow down the frame read?