- Home /
Communication via Bluetooth virtual serial port does not work correctly.
Hi all,
I have tried to communicate with a bluetooth sensor device via a virtual serial port using UNITY3D in Windows7/8.1. However, the communication does not work correctly.
The virtual serial port outputs a wrong byte instead of a correct byte at every first, sometimes second, byte in a response packet.
The communication works perfectly in the same program compiled by Visual Studio 2013.
Here is a sample of the correct communication. If PC send a command byte "2e" , then the device respond ACK byte "ff", response ID "2f" and a byte sequence received by immedeiately using SerialPort.Write() and ReadByte() .
WriteByte: 2e <--Command
ReadByte: ff 2f 01 00 00 00 06 00 <--Response
However, in UNITY 3D, the first byte, sometimes second too, of every response packet changes into "00".
WriteByte: 2e <--Command
ReadByte: 00 2f 01 00 00 00 06 00 <--Response
WriteByte: 2e <--Command
ReadByte: 00 00 01 00 00 00 06 00 <--Response
I will attach the sample code.
SerialHandler.cs
using System;
using System.Threading;
using System.IO.Ports;
using UnityEngine;
public class SerialHandler
{
public String comPort;
public System.IO.Ports.SerialPort serialPort = new System.IO.Ports.SerialPort();
protected Thread readThread;
public SerialHandler(string comName)
{
comPort = comName;
OpenConnection ();
}
public void WriteBytes(byte[] b, int index, int length)
{
foreach (byte wb in b)
{
Debug.Log("WriteBytes: " + wb.ToString("x2") + " Time: " + DateTime.Now.ToString("HH:mm:ss.ffff"));
}
serialPort.Write(b, index, length);
}
public void ReadByte()
{
while (serialPort.IsOpen) {
try {
int b = serialPort.ReadByte ();
Debug.Log ("ReadByte: " + b.ToString ("x2") + " Time: " + DateTime.Now.ToString ("HH:mm:ss.ffff"));
} catch(Exception e){
Debug.Log ("ReadByte Exception : " + e.Message + " Time: " + DateTime.Now.ToString ("HH:mm:ss.ffff"));
}
}
}
public void OpenConnection()
{
serialPort.BaudRate = 9600;
serialPort.PortName = comPort;
serialPort.ReadTimeout = 10000;
serialPort.WriteTimeout = 10000;
serialPort.Open();
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
System.Threading.Thread.Sleep (500);
readThread = new Thread(ReadByte);
readThread.Start ();
System.Threading.Thread.Sleep (500);
}
public void CloseConnection()
{
serialPort.Close();
System.Threading.Thread.Sleep (500);
readThread.Abort ();
System.Threading.Thread.Sleep (500);
readThread = null;
System.Threading.Thread.Sleep (500);
}
}
If someone encounterd the problem, please tell me a solution!
OS: Windows7 32bit / Windows7 64bit / Windows8.1 64bit
Device: Shimmer3 (www.shimmersensing.com)
Bluetooth2.1+EDR
UNITY v5.0.1p1 / 4.6.4p3
Your answer
Follow this Question
Related Questions
Multiple SerialPorts delay (C#) 0 Answers
Balance Board Support for Standalone Windows Via Bluetooth 0 Answers
MonoDevelop crashes when opening it 2 Answers
Accessing Bluetooth Serial Device - Windows 10 1 Answer
i am having problem with MonoDevelop 1 Answer