Go Down

Topic: Serial Communication with Unity3D - C# programming w/o plugins (HELP) (Read 9300 times) previous topic - next topic

Robin2


Serial communication between applications on your computer (such as Unity3D) is not easy


I do hope this is not an attempt to encourage the OP to use your product.

Serial comms is quite simple.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Rickmc3280

#16
Jun 04, 2014, 11:54 pm Last Edit: Jun 04, 2014, 11:58 pm by Rickmc3280 Reason: 1
I was actually about to say that...

However, if you are not trying to sell your product then you should know better.  This is a forum of doers, not quitters.  I've failed many times on this project but learned enough to get this working.  If I wanted to give in, where I am at right now, I have already accomplished more than my friends/family have thought that I would...  I might not know a whole lot at this moment, but you can bet your ### I'm not gonna give up because it is "not easy".  

Struggles and Challenges are what hone our skills.  Challenge accepted.

Found this -  http://forum.arduino.cc/index.php?topic=244589.0

Also good luck on your kickstarter- https://www.kickstarter.com/projects/997925261/1930854601?token=67359394
Hopefully it will help someone, but not myself I am afraid.

you are trying to sell your product :( shame shame.  Although, I do wish you much success on your product... you've already proven you are a doer, but dont try to sell your potential clients short.

Rickmc3280

Here is some code by Caliber Mengsk in his thread here -http://forum.arduino.cc/index.php/topic,40001.0.html

I am posting these just in case anyone found my thread using keywords and didnt check out his yet.  That and its easier for me to find now and very much related, but using a different system that I am.

Code: [Select]
using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class arduinoSerialCommunication : MonoBehaviour {

     SerialPort stream = new SerialPort("COM3", 19200); //Set the port (com3) and the baud rate (9600, is standard on most devices)
     string receivedData = "EMPTY";
     public string sendToArduino = "1";

     void Start () {
           stream.Open(); //Open the Serial Stream.
           stream.Write("1");
     }

     void Update () {
           if(stream.BytesToRead >=11)//my arduino will always send back 11 bytes of information, so wait til it's that large to avoid dropping data.
           {
                 receivedData = stream.ReadLine();
                 stream.WriteLine(sendToArduino);
           }
     }

     void OnGUI()
     {
           GUI.Label(new Rect(10,10,300,100), receivedData); //Display new values
     }
}


and

Code: [Select]
#include <Wire.h>
#include <string.h>
#include "nunchuck_funcs.h"

byte accx,accy,zbut,cbut;

int joyx, joyy;

void setup()
{
   Serial.begin(19200);
   nunchuck_setpowerpins();
   nunchuck_init(); // send the initilization handshake
}

void loop()
{
 if(Serial.available()>0)
 {
   Serial.flush();
   nunchuck_get_data();
   zbut = nunchuck_zbutton();
   cbut = nunchuck_cbutton();
   joyx = nunchuck_joyx();
   joyy = nunchuck_joyy();
   if(joyx <100) //ADDED TO KEEP BYTE SIZES THE SAME
   {
     Serial.print("0");
   }
   Serial.print((int)joyx);
   Serial.print(",");
   if(joyy <100) //ADDED TO KEEP BYTE SIZES THE SAME
   {
     Serial.print("0");
   }
   Serial.print((int)joyy);
   Serial.print(",");
   Serial.print((int)zbut);
   Serial.print(",");
   Serial.print((int)cbut);
   Serial.println();
 }
}



as well as...

Code: [Select]
void setup()
{
 Serial.begin(38400);
}

void loop()
{
 float values[] = {analogRead(0),analogRead(1),analogRead(2)};
 Serial.flush();
 Serial.print(map(values[0],0,1023,0,359));
 Serial.print(",");
 Serial.print(map(values[1],0,1023,0,359));
 Serial.print(",");
 Serial.print(map(values[2],0,1023,0,359));

 Serial.println();
 delay(30);
}


and  

Code: [Select]
using UnityEngine;
using System.Collections;
using System.IO.Ports;

public class serialRotation : MonoBehaviour {

     SerialPort stream = new SerialPort("COM4", 38400); //Set the port (com4) and the baud rate (9600, is standard on most devices)
     float[] lastRot = {0,0,0}; //Need the last rotation to tell how far to spin the camera
     Vector3 rot;
     Vector3 offset;
     
     
     void Start () {
           stream.Open(); //Open the Serial Stream.
     }
     
     // Update is called once per frame
     void Update () {
           string value = stream.ReadLine(); //Read the information
           string[] vec3 = value.Split(','); //My arduino script returns a 3 part value (IE: 12,30,18)
           if(vec3[0] != "" && vec3[1] != "" && vec3[2] != "") //Check if all values are recieved
           {                  
                 rot = new Vector3(float.Parse(vec3[0]),float.Parse(vec3[1]),float.Parse(vec3[2]));
                             //Read the information and put it in a vector3
                 transform.rotation = Quaternion.Slerp(transform.rotation,
                                                                             Quaternion.Euler(0,rot.x,rot.y),
                                                                             Time.deltaTime*3);
                             //Take the vector3 and apply it to the object this script is applied.
                 stream.BaseStream.Flush(); //Clear the serial information so we assure we get new information.
           }
     }
     
     void OnGUI()
     {
           string newString = "Connected: " + transform.eulerAngles;
           GUI.Label(new Rect(10,10,300,100), newString); //Display new values
           GUI.Label(new Rect(10,30,300,100), "\t" + rot);
     }
}

sureshn

@Rickmc3280:  Thought Arduinotrix Studio and Arduinotrix SDK would help you with your arduino project/quest w/o plugins.  Intelligent serial input/output system was successfully implemented as core engines within Arduinotrix Studio in Windows and Arduinotrix SDK in C++.  Serial communications in C++ was easy.  Intelligent system with command processing to communicate with arduino without data errors took some time to build and is now feature complete.  Meanwhile, I am also looking at Unity3D to learn more.

Rickmc3280

Your UI/Graphics component does not express the full functionality of your design.  I do find it interesting, but what I really mean to say is my hands on experience will help me more to finish my project.  I would highly recommend UI or maybe using Unity for your UI.  That could potentially put you out there and maybe I can use for my project if I better understood your product.

Go Up