GithubHelp home page GithubHelp logo

lucifer926 / serialcommunity Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dwilches/ardity

0.0 1.0 0.0 164 KB

Assets for integrating Arduino and Unity (or Unity and any hardware that communicates over a COM port)

C# 100.00%

serialcommunity's Introduction

SerialCommUnity

Assets for integrating Arduino and Unity (or Unity and any hardware that communicates over a COM port)

Instructions

There are three scenes that show how to read/write data from/to a serial device. There is a prefab that you need to add to your scene, this prefab will do all the thread management, queue synchronization and exception handling for you.

If you need a program to test your Unity scene I have created the following Arduino program that sends a heartbeat message each 2 seconds. It also recognizes two input messages and reacts to them, this is for bidirectional communication for the scene "DemoSceneUserPoll_ReadWrite". So if you want to use that scene just run it and press the keys 'A' or 'Z' to see what you Arduino has to say about it.

Possible Problem When Importing This Package

If you get this error message:

Assets/SerialComm/Scripts/SerialThread.cs(9,17): error CS0234: The type or namespace name 'Ports' does not exist in the namespace 'System.IO'. Are you missing an assembly reference?

It's because the current "API Compatibility Level" of your Unity project is set to ".NET 2.0 Subset", which doesn't contain the classes necessary for serial communication. Do this to solve the problem:

Go to Edit -> Project Settings -> Player, and under "Other Settings" find an option that reads "Api Compatibility Level" and change it from ".NET 2.0 Subset" to ".NET 2.0".

Sample Arduino Program

This sample communicates with any of the scenes called: DemoScene_AutoPoll* or DemoScene_UserPoll*.

unsigned long last_time = 0;

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    // Print a heartbeat
    if (millis() > last_time + 2000)
    {
        Serial.println("Arduino is alive!!");
        last_time = millis();
    }

    // Send some message when I receive an 'A' or a 'Z'.
    switch (Serial.read())
    {
        case 'A':
            Serial.println("That's the first letter of the abecedarium.");
            break;
        case 'Z':
            Serial.println("That's the last letter of the abecedarium.");
            break;
    }
}

Sample with Tear-Down function

This sample has a tear-down function (use it with the scene DemoScene_SampleTearDown), it will be executed when the Unity program stops. This sample expects you to be using an Arduino UNO, if not, change the number of the pin to which the LED is connected.

unsigned long last_time = 0;
int ledPin = 13;

void setup()
{
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);
}

void loop()
{
    // Print a heartbeat
    if (millis() > last_time + 2000)
    {
        Serial.println("Arduino is alive!!");
        last_time = millis();
    }

    // Send some message when I receive an 'A' or a 'Z'.
    switch (Serial.read())
    {
        case '1':
            digitalWrite(ledPin, HIGH);
            Serial.println("Lights are ON");
            break;
        case '2':
            digitalWrite(ledPin, LOW);
            Serial.println("Lights are OFF");
            break;
        
        // Execute tear-down functionality
        case 'X':
            for (int i = 0; i < 10; i++)
            {
                digitalWrite(ledPin, HIGH);
                delay(100);
                digitalWrite(ledPin, LOW);
                delay(100);
            }
            
            // This message won't arrive at Unity, as it is already in the
            // process of closing the port
            Serial.println("Tearing down some work inside Arduino");
            break;
    }
}

Sample with custom delimiter

// This is the separator configured in Unity
#define SEPARATOR 255

unsigned long last_time = 0;
int ledPin = 13;

byte responseMessage[] = { 65, 66, 67, SEPARATOR };
byte aliveMessage[] = { 65, 76, 73, 86, 69, 33, SEPARATOR };

void setup()
{
    Serial.begin(9600);

    aliveMessage[8] = SEPARATOR;
}

void loop()
{
    // Print a heartbeat
    if (millis() > last_time + 2000)
    {
        Serial.write(aliveMessage, sizeof(aliveMessage));
        Serial.flush();
        last_time = millis();
    }
    
    // React to "commands"
    if (Serial.read() == ' ')
    {
        Serial.write(responseMessage, sizeof(responseMessage));
        Serial.flush();
    }
}

COM port names

To open a COM port from SerialCommUnity you need to use one of these naming conventions:

  • COM1, COM2, ... for COM1 through COM9
  • \\.\COM10, \\.\COM11, ... for COM10 and up

License

This work is released under the Creative Commons Attributions license.

If you use this library please let me know, so I know my work has been useful to you :)

CC Attribution

serialcommunity's People

Contributors

dwilches avatar

Watchers

James Cloos avatar

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.