LoginSignup
riririki
@riririki (riki nona)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Unityエラー:IOException: No such file or directory

前提

UnityとESP32をBlueTooth接続する時に出たエラーです。

ESP32にArduino IDEを介してBlueTooth機能をつけ、Unityと接続しています。
Macで開発し、PC側ではBlueTooth接続もすべてうまく行きました。
アプリが完成し、Androidビルドしました。
Android端末とESP32をBlueTooth接続させ、これも成功しました。
しかしアプリを起動してBlueTooth機能を使おうとすると、以下の「IOException: No such file or directory」が出ます。(コンソールのアセットを導入しているのでアプリ側でもデバッグできています)
このエラーのせいで、Bluethoothでデータの送受信ができません。

解決したいこと

下記エラーの解決

発生している問題・エラー

IOException: No such file or directory

System.IO.Ports.SerialPortStream.ThrowIOException () (at <480201908ed94ddaa0561e2e6e0ddd4e>:0)
System.IO.Ports.SerialPortStream..ctor (System.String portName, System.Int32 baudRate, System.Int32 dataBits, System.IO.Ports.Parity parity, System.IO.Ports.StopBits stopBits, System.Boolean dtrEnable, System.Boolean rtsEnable, System.IO.Ports.Handshake handshake, System.Int32 readTimeout, System.Int32 writeTimeout, System.Int32 readBufferSize, System.Int32 writeBufferSize) (at <480201908ed94ddaa0561e2e6e0ddd4e>:0)
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream..ctor(string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
System.IO.Ports.SerialPort.Open () (at <480201908ed94ddaa0561e2e6e0ddd4e>:0)
(wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort.Open()
SerialHandler.Open () (at Assets/SerialHandler.cs:41)
SerialHandler.Awake () (at Assets/SerialHandler.cs:23)

Arduino側のコード(ESP32に渡している)

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

// Pin Assingment
const int buttonA = 14; 
int pastA = 0;
BluetoothSerial SerialBT;

void setup() {
  SerialBT.begin("Riki's.ESP32"); //Bluetooth device name
  
  
  pinMode(buttonA, INPUT_PULLUP);

}

void loop() {
  
  if(digitalRead(buttonA) == LOW){

    switch(pastA){
      case 0:
      SerialBT.println("A");
      pastA = 1;
      break;

      case 1:
      break;
      
    }
    
  }
  
  else {

    switch(pastA){
      case 0:
      
      break;

      case 1:
      SerialBT.println();
      pastA = 0;
      break;
    }
    
  }
  delay(100);
}

Unity側のSerialHandlerコード(ゲーム内オブジェクトに渡している)

using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;

public class SerialHandler : MonoBehaviour
{
    public delegate void SerialDataReceivedEventHandler(string message);
    public event SerialDataReceivedEventHandler OnDataReceived;

    public string portName = "/tty.ESP32-ESP32SPP";
    public int baudRate    = 9600;

    private SerialPort serialPort_;
    private Thread thread_;
    private bool isRunning_ = false;

    private string message_;
    private bool isNewMessageReceived_ = false;

    void Awake()
    {
        Open();
    }

    void Update()
    {
        if (isNewMessageReceived_) {
            OnDataReceived(message_);
        }
    }

    void OnDestroy()
    {
        Close();
    }

    private void Open()
    {
        serialPort_ = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);
        serialPort_.Open();

        isRunning_ = true;

        thread_ = new Thread(Read);
        thread_.Start();
    }

    private void Close()
    {
        isRunning_ = false;

        if (thread_ != null && thread_.IsAlive) {
            thread_.Join();
        }

        if (serialPort_ != null && serialPort_.IsOpen) {
            serialPort_.Close();
            serialPort_.Dispose();
        }
    }

    private void Read()
    {
        while (isRunning_ && serialPort_ != null && serialPort_.IsOpen) {
            try {
                // if (serialPort_.BytesToRead > 0) {
                    message_ = serialPort_.ReadLine();
                    isNewMessageReceived_ = true;
                // }
            } catch (System.Exception e) {
                Debug.LogWarning(e.Message);
            }
        }
    }

    public void Write(string message)
    {
        try {
            serialPort_.Write(message);
        } catch (System.Exception e) {
            Debug.LogWarning(e.Message);
        }
    }
}

Unity側のObjectHandlerコード(ゲーム内オブジェクトに渡している)

public class ObjectController : MonoBehaviour
{
    public SerialHandler serialHandler;
    public float delta = 0.01f;
    private float deltaPos;
    private Vector3 pos;
    private bool pastData = false; //Yes bang

    void Start()
    {
        Debug.Log("Start");
        serialHandler.OnDataReceived += OnDataReceived;
    }

    void OnDataReceived(string message)
    {
        
        var data = message.Split(
            new string[] { "\n" }, System.StringSplitOptions.None);

        CustomEvent.Trigger(gameObject, "Bluetooth", data);






    }
}

SirialHandlerのシリアルポートには、「/dev/tty.ESP32-ESP32SPP」を入れています。
スクリーンショット 2022-04-01 15.18.22.png

これは、ターミナルで確認したシリアルポートです。
スクリーンショット 2022-04-01 15.21.33.png

自分で試したこと

おそらくシリアルポート周りに問題があると考えました
シリアルポートに入れる値は階層になっていてはだめかもしれないという情報をみつけたので「/tty.ESP32-ESP32SPP」をシリアルポートに設定しましたがだめでした。

0

No Answers yet.

Your answer might help someone💌