• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by YaNakajima · Apr 29, 2015 at 02:55 PM · monodevelopwindowsbluetoothserialport

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

link text

testsimplestcommunicationunity5.zip (28.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Bluetooth on windows 32feet dll 0 Answers

Bluetooth Support on Win and OSX 1 Answer

Reading Serial data in Unity from Android app 2 Answers

Balance Board Support for Standalone Windows Via Bluetooth 0 Answers

Problems accessing Bluetooth data sent over a serial port 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges