Last active
June 12, 2017 19:19
-
-
Save kasperkamperman/d30e9b95115d7461a3870ee1a9b06df5 to your computer and use it in GitHub Desktop.
Quick Example how to read comma separated strings in Unity with SerialCommUnity.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code example for this Code: | |
// https://github.com/DWilches/SerialCommUnity | |
// | |
// Below the modified SampleMessageListener to show how to parse comma separated strings | |
// Start with DemoSceneAutoPoll | |
/** | |
* SerialCommUnity (Serial Communication for Unity) | |
* Author: Daniel Wilches <[email protected]> | |
* | |
* This work is released under the Creative Commons Attributions license. | |
* https://creativecommons.org/licenses/by/2.0/ | |
*/ | |
using UnityEngine; | |
using System.Collections; | |
/** | |
* When creating your message listeners you need to implement these two methods: | |
* - OnMessageArrived | |
* - OnConnectionEvent | |
*/ | |
public class SampleMessageListener : MonoBehaviour | |
{ | |
// Invoked when a line of data is received from the serial device. | |
void OnMessageArrived(string msg) | |
{ | |
Debug.Log("Message arrived: " + msg); | |
string[] tokens = msg.Split(','); | |
//Debug.Log (tokens.Length); | |
//Debug.Log ("Value 0 is : " + tokens [0]); | |
// Convert to integers (of course when you expect an integer. | |
// There is also something lik TryParse. | |
int numVal = int.Parse(tokens[0]); | |
Debug.Log(numVal); | |
// Convert to float | |
//float floatVal = float.Parse("10.5"); | |
//Debug.Log(floatVal); | |
} | |
// Invoked when a connect/disconnect event occurs. The parameter 'success' | |
// will be 'true' upon connection, and 'false' upon disconnection or | |
// failure to connect. | |
void OnConnectionEvent(bool success) | |
{ | |
if (success) | |
Debug.Log("Connection established"); | |
else | |
Debug.Log("Connection attempt failed or disconnection detected"); | |
} | |
} | |
/* | |
// Arduino Code just to send analog values comma separated | |
// You can sent anything. | |
// define the total number of analog sensors | |
// that you want to read | |
const int numberOfSensors = 6; | |
unsigned long lastTime = 0; | |
unsigned int interval = 40; // 25 times a second | |
void setup() { | |
// initialize the serial port: | |
Serial.begin(57600); | |
} | |
void loop() { | |
if (millis() - lastTime >= interval) { | |
// loop over the sensors: | |
for (int thisSensor = 0; thisSensor < numberOfSensors; thisSensor++) { | |
// read each sensor | |
int sensorReading = analogRead(thisSensor); | |
// print its value out as an ASCII numeric string | |
Serial.print(sensorReading, DEC); | |
// if this isn't the last sensor to read, | |
// then print a comma after it | |
if (thisSensor < numberOfSensors -1) { | |
Serial.print(","); | |
} | |
} | |
// after all the sensors have been read, | |
// print a newline and carriage return | |
Serial.println(); | |
lastTime = millis(); | |
} | |
} | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment