Boson
Boson
CC#
Created by Boson on 12/30/2022 in #help
❔ Serial Communication
Hi. Nothing is being printed to the console and I have no idea why.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;

public class ArduinoController : MonoBehaviour
{
SerialPort serial;

void Start() //called once when the program is run
{
serial = new SerialPort("COM3", 9600);
serial.Open();
}

void Update() //called once every frame
{
string data = serial.ReadLine();
Debug.Log(data);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;

public class ArduinoController : MonoBehaviour
{
SerialPort serial;

void Start() //called once when the program is run
{
serial = new SerialPort("COM3", 9600);
serial.Open();
}

void Update() //called once every frame
{
string data = serial.ReadLine();
Debug.Log(data);
}
}
4 replies
CC#
Created by Boson on 12/29/2022 in #help
❔ crashing
Any idea how to prevent this code from crashing unity?
SerialPort sp = new SerialPort("COM3", 9600);
sp.Open();

While true()
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
SerialPort sp = new SerialPort("COM3", 9600);
sp.Open();

While true()
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
15 replies