Giter VIP home page Giter VIP logo

unityserialport's Introduction

What is this ?

This is a script to make easy to use serial port in Unity. Especially parsing lines and values when data is TSV formatted.

A demo is provided with a sample Arduino sketch to generate sample serial data.

The script component helps sending and receiving data from a serial port. It detects line breaks and notifies the attached gameObject of new lines as they arrive.

Install

Download the latest package and import in your project.

You do not need to clone or download the entire project unless you want to modify it (and then make a pull request).

How to use

See the demo in Assets/Serial/Demo/Serial Demo.unity

Usage 1: Receive data when you expect line breaks

  • drop this script to a gameObject
  • check the NotifyLines parameter
  • create a script on the same gameObject to receive new line notifications
  • add the OnSerialLine() function, here is an example
void OnSerialLine(string line) {
  Debug.Log("Got a line: " + line);
}

Usage 2: Receive data (when you don't expect line breaks)

  • drop this script to a gameObject
  • from any script, use the static props Serial.ReceivedBytesCount, Serial.ReceivedBytes and don't forget to call ClearReceivedBytes() to avoid overflowing the buffer

Usage 3: Send data

  • from any script, call the static functions Serial.write() or Serial.writeLn()
  • if not not already, the serial port will be opened automatically.

Configuration

Drop the SerialConfig component to an empty GameObject in your scene and configure:

  • the preferred ports
  • the speed
  • whether you wand debug informations in console

Troubleshooting

Error CS0234 Ports does not exist in the namespace

You may get the following error:

error CS0234: The type or namespace name `Ports' does not exist in the namespace `System.IO'. Are you missing an assembly reference?

Solution:

First make sure the correct platform is selected in File | Build Settings. It should be "PC, Mac & Linux standalone". Other platforms are not supported. In some circonstances, the setting switches back to another platform.

Then go to Edit | Project Settings | Player | PC, Mac & Linux Standalone settings | Other Settings | Optimization | API Compatibility Level and select ".Net 2.0". The other option does not contain the Ports namespace.

In some older version of Unity, you would find this option in: File | Build Settings | Optimization | API Compatibility Level: .Net 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.