title: Unity3D - HC-05 bluetooth module connected to Arduino Uno: Reads always timeout
body: <p>I am trying to use an HC-05 module connected to an Arduino Uno to wirelessly send data from an IMU as input to a Unity3D simulation. I have confirmed that the module works using PuTTY and it sends and receives properly. When i connect in unity and try to read from the HC-05, it always times out. I've tried to change the <code>sp.ReadLine()</code> to <code>sp.Read()</code> to see if it was just missing the newline character but no difference.</p>
  
  <p>This is the Unity code</p>
  
  <pre><code>using System;
  using System.Text;
  using System.Collections;
  using System.IO.Ports;`
  
  using UnityEngine;
  
  public class PlayerController : MonoBehaviour
  {
  private Rigidbody rb;
  Vector3 origin,newPos;
  SerialPort sp;
  string text;
  int trials = 0;
  float speed;
  
  void Start()
  {
      rb = GetComponent&lt;Rigidbody&gt;();
      origin = rb.position;
      sp = new SerialPort("\\\\.\\COM4", 9600);
      if (!sp.IsOpen)
      {
          print("Opening COM4, baud 9600");
          sp.Open();
          sp.ReadTimeout = 100;
          sp.Handshake = Handshake.None;
          if (sp.IsOpen) { print("Open"); }
  
      }
  }
  
  void reset()
  {
      rb.position = origin;
  }
  
  void Update()
  {
      string data;
  
      float r = Input.GetAxis("Cancel");
      Vector3 toPos=new Vector3();
  
      if (!sp.IsOpen)
      {   trials++;
          sp.Open();
          print(trials);
      }
      if (r != 0.0f)
      {
          reset();
      }
  
  
      if (sp.IsOpen)
      {
          //print("SP is OPEN");
          data = sp.ReadLine();
          sp.BaseStream.Flush();
  
          //if (data != null) {
              //print("data recieved");
              text = data.ToString();
              //float step = speed * Time.deltaTime;
              //toPos.Set(data[6],data[7],data[8]);
              //rb.rotation.SetLookRotation(toPos,rb.position);
          //}
  
      }
  
  }
  
  private void OnGUI()
  {
      text=GUI.TextField(new Rect(10, 10, 200, 20), text);
  }
  </code></pre>
  
  <p>This is the Arduino code</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  #include &lt;SoftwareSerial.h&gt;// import the serial library
  #include &lt;Wire.h&gt;//I2C library
  
  enum Ascale {
    AFS_2G = 0,
    AFS_4G,
    AFS_8G,
    AFS_16G
  };
  
  enum Gscale {
    GFS_250DPS = 0,
    GFS_500DPS,
    GFS_1000DPS,
    GFS_2000DPS
  };
  
  enum Mscale {
    MFS_14BITS = 0,
    MFS_16BITS
  };
  
  
  uint8_t Gscale = GFS_250DPS;
  uint8_t Ascale = AFS_2G;
  uint8_t Mscale = MFS_16BITS;
  uint8_t Mmode = 0x02;
  float aRes, gRes, mRes;
  
  int intPin = 12;
  int myLed = 13;
  
  int16_t accelCount[3];
  int16_t gyroCount[3];
  int16_t magCount[3];
  
  int16_t tempCount; 
  float temperature; 
  float SelfTest[6];
  
  
  SoftwareSerial ser(10, 11); // RX, TX
  int BluetoothData; // the data given from Computer
  float AccelResult;
  float AccelScale=16384;
  float GyroResult;
  float GyroScale=32.8;
  float AltResult;
  bool isInit=false;
  
  void writeTo(byte device, byte toAddress, byte val) {
    Wire.beginTransmission(device);
    Wire.write(toAddress);
    Wire.write(val);
    Wire.endTransmission();
  }
  
  void readFrom(byte device, byte fromAddress, int num, byte result[]) {
    Wire.beginTransmission(device);
    Wire.write(fromAddress);
    Wire.endTransmission();
    Wire.requestFrom((int)device, num);
    int i = 0;
    while (Wire.available()) {
      result[i] = Wire.read();
      i++;
    }
  }
  
  void setup() {
    Wire.begin();
    initMPU9250();
    float magCalibration[3];
    initAK8963(magCalibration);
    ser.begin(9600);
  }
  
  void loop() {
    if (ser.available()){
      if(ser.read()=='b'){
      //ser.println("hello");
      getData();
      }
  }
  delay(50);// prepare for next data ...
  }
  
  void getData(){
  
      readAccelData(accelCount);
      ser.println("ACCEL DATA------------------");
      ser.print("  X-Axis Accel "); ser.println(accelCount[0]);
      ser.print("  Y-Axis Accel "); ser.println(accelCount[1]);
      ser.print("  Z-Axis Accel "); ser.println(accelCount[2]);
  
      readGyroData(gyroCount);
      ser.println("GYRO DATA-------------------");
      ser.print("  X-Axis Gyro "); ser.println(gyroCount[0]);
      ser.print("  Y-Axis Gyro "); ser.println(gyroCount[1]);
      ser.print("  Z-Axis Gyro "); ser.println(gyroCount[2]);
  
      readMagData(magCount);
      ser.println("MAG DATA--------------------");
      ser.print("  X-Axis Mag "); ser.println(magCount[0]);
      ser.print("  Y-Axis Mag "); ser.println(magCount[1]);
      ser.print("  Z-Axis Mag "); ser.println(magCount[2]);
      ser.println("TEMP DATA-------------------");
      tempCount=readTempData();
      ser.print("  Temp "); ser.println(tempCount);
      ser.println("");
  }
  </code></pre>
  
  <p>This the output from PuTTY</p>
  
  <pre><code>ACCEL DATA------------------
    X-Axis Accel -9665
    Y-Axis Accel -11690
    Z-Axis Accel -3847
  GYRO DATA-------------------
    X-Axis Gyro 177
    Y-Axis Gyro 181
    Z-Axis Gyro 130
  MAG DATA--------------------
    X-Axis Mag 118
    Y-Axis Mag 418
    Z-Axis Mag -176
  TEMP DATA-------------------
    Temp 1050
  </code></pre>
  
  <p>This is the exception</p>
  
  <pre><code>TimeoutException: The operation has timed-out.
  System.IO.Ports.WinSerialStream.Read (System.Byte[] buffer, Int32 offset, Int32 count)
  System.IO.Ports.SerialPort.read_byte ()
  System.IO.Ports.SerialPort.ReadTo (System.String value)
  System.IO.Ports.SerialPort.ReadLine ()
  (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:ReadLine ()
  PlayerController.Update () (at Assets/PlayerController.cs:59)
  </code></pre>
  
  <p>I apologize for the lengthy post and am grateful for any and all help.
  Thanks!</p>