一、Unity端发送消息,Arduino端接收消息 通过串口通信
Arduino端
#include <Arduino.h>
#define PIN_KEY 5
uint item;
void setup() {
item = 0;
Serial.begin(115200);
pinMode(PIN_KEY, OUTPUT);
}
void loop() {
if(Serial.available()>0)
{
item = Serial.read();
}
if(item == 'a')
{
digitalWrite(PIN_KEY,HIGH);
}
if(item == 'b')
{
digitalWrite(PIN_KEY,LOW);
}
}
Unity端
public class Test : MonoBehaviour
{
SerialPort port = new SerialPort("COM4", 115200);
public Button Btn_Open;
public Button Btn_Close;
private void Start()
{
port.Open();
port.ReadTimeout = 1;
Btn_Open.onClick.AddListener(() => {
port.WriteLine("a");
});
Btn_Close.onClick.AddListener(() => {
port.WriteLine("b");
});
}
}
实现串口通信,点击开灯按钮,灯亮。关灯按钮,灯灭。
二、Unity端接收消息,Arduino端发送消息 通过串口通信
Arduino端
#include <Arduino.h>
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("a");
delay(1000);
Serial.println("o");
delay(1000);
}
Unity端
1.导入Ardity插件(在Unity商城中找)
2.打开读写实例场景
3.添加读写的实例脚本文章来源:https://www.toymoban.com/news/detail-661180.html
4.文章来源地址https://www.toymoban.com/news/detail-661180.html
/**
* Ardity (Serial Communication for Arduino + Unity)
* Author: Daniel Wilches <dwilches@gmail.com>
*
* This work is released under the Creative Commons Attributions license.
* https://creativecommons.org/licenses/by/2.0/
*/
using UnityEngine;
using System.Collections;
/**
* Sample for reading using polling by yourself, and writing too.
*/
public class SampleUserPolling_ReadWrite : MonoBehaviour
{
public MeshRenderer Cube;
public SerialController serialController;
// Initialization
void Start()
{
serialController = GameObject.Find("SerialController").GetComponent<SerialController>();
Debug.Log("Press A or Z to execute some actions");
}
// Executed each frame
void Update()
{
//---------------------------------------------------------------------
// Send data
//---------------------------------------------------------------------
// If you press one of these keys send it to the serial device. A
// sample serial device that accepts this input is given in the README.
if (Input.GetKeyDown(KeyCode.N))
{
Debug.Log("Sending n");
serialController.SendSerialMessage("n");
}
if (Input.GetKeyDown(KeyCode.M))
{
Debug.Log("Sending m");
serialController.SendSerialMessage("m");
}
//---------------------------------------------------------------------
// Receive data
//---------------------------------------------------------------------
string message = serialController.ReadSerialMessage();
if (message == null)
return;
// Check if the message is plain data or a connect/disconnect event.
if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_CONNECTED))
Debug.Log("Connection established");
else if (ReferenceEquals(message, SerialController.SERIAL_DEVICE_DISCONNECTED))
Debug.Log("Connection attempt failed or disconnection detected");
else
{
if (message.Equals("a"))
{
Cube.material.color = Color.red;
}
if (message.Equals("o"))
{
Cube.material.color = Color.white;
}
Debug.Log("Message arrived: " + message);
}
}
}
到了这里,关于Unity Arduino 串口通信的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!