- Home /
Duplicate Question
Serial Data Communication using Unity
Can anyone here guide me step by step how to read data form serial port i.e Serial Data Communication with Unity? Any help would be much appreciated. Thank you. I get it worked but why my code shows only 255 as output when data received? My code is:
using UnityEngine;
using System.Collections;
#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
#endregion
public class SerialPort : MonoBehaviour {
private SerialPort sp;
void Start () {
sp = new SerialPort( "COM3", 9600, Parity.None, 8, StopBits.One);
sp.Open();
sp.ReadTimeout = 50;
}
void OnApplicationQuit() {
sp.Close();
}
void FixedUpdate() {
string tempS = "";
try {
byte tempB = (byte) sp.ReadByte();
while (tempB != 255) {
tempS += ((char) tempB);
tempB = (byte) sp.ReadByte();
}
}
catch (Exception e) {
}
if (tempS!="") {
Debug.Log("serial out "+tempS);
}
}
}
http://answers.unity3d.com/questions/37150/serial-communication-problem.html
http://answers.unity3d.com/questions/24379/receiving-information-from-a-serial-port.html
http://answers.unity3d.com/questions/293506/problems-accessing-serialport.html
http://answers.unity3d.com/questions/392664/why-does-my-com-port-not-exist.html
Follow this Question
Related Questions
Read in 2 Different Arduino Values 1 Answer
ReadLine() Function Crash 0 Answers
Unity Editor crashing on SerialPort.Open()? 3 Answers
error with system.io.ports 0 Answers