同时串行读写

问题描述 投票:0回答:1

我有一个项目需要同时串行读写。我从 Arduino 接收一些数据并将它们显示在界面的相关区域中。在读取时的确切时间,我需要通过串口向 Arduino 发送一些数据。每次阅读和写作都可以完美地发挥作用。但是,如果我在读取任务运行时启动写入任务,则读取任务将停止。书写完成后,从上次中断的地方继续阅读。我知道阅读和写作是同时进行的。但我没能成功。

这是执行读取过程的脚本

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Linq;
using IO;
using System.IO.Ports;
using TMPro;
using System.Threading;
using UnityEngine.Events;
 
public class Serial : MonoBehaviour{

    // stream variable
    public SerialPort stream = new SerialPort();
    public string receivedString = "";
    // threading
    Thread readThread;
    private static List<Action> executeOnMainThread = new List<Action>();

    public void StartConnection()
    {
        stream.PortName = "COM4";
        stream.BaudRate = 9600;
        stream.ReadTimeout = 600000;
        stream.WriteTimeout = 600000;
    
        if (!stream.IsOpen)
        {
            stream.Open();
            readThread = new Thread(ReadFromSerial);
            readThread.Start();
        }
    }
    void ReadFromSerial()
    {
        while (stream.IsOpen)
        {
            string value = stream.ReadLine();
    Debug.Log(value);
            stream.BaseStream.Flush();
        }
    }
    void Update () 
    {
        lock (executeOnMainThread)
        {
            foreach (var action in executeOnMainThread)
            {
                action();
            }
            executeOnMainThread.Clear();
        }
    }  
}

这是编写任务脚本

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Linq;
using IO;
using System.IO.Ports;
using TMPro;
using System.Threading;
using UnityEngine.Events;

public class SimulationHandler : MonoBehaviour
{
    [SerializeField] GameObject serialobject;
    private Serial serial;
    
    string presureCSVFileName = "";
    List<string> presureData = new List<string>();

    // threading
    Thread simPThread;
    void Start()
   {
        serial = serialobject.GetComponent<Serial>(); 
    }
    
    public void SelectCSV()
    {
        presureCSVFileName = "C:/Users/Pc/Downloads/simp.csv";
        GetPresureData();
    }

    void GetPresureData()
    {
        using (StreamReader reader = new StreamReader(presureCSVFileName))
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                string[] cells = line.Split(',');
                presureData.Add(cells[3]);
            }
        }
    }
    void SIMPThreadFunction()
    {
        for (int i = 0; i < presureData.Count ; i++)
        {
            if (serial.stream.IsOpen)
            {
                string simpData = presureData[i];
                serial.stream.Write(simpData);
                serial.stream.BaseStream.Flush();
                Debug.Log(simpData);
                Thread.Sleep(1000);
            }     
        }
        Debug.Log("SIMP done!");
    }

    public void SIMP()
    {
        Debug.Log("SIMP start!");
        simPThread = new Thread(SIMPThreadFunction);
        simPThread.Start();
    }
}

我尝试通过删除不相关的代码来共享相关代码。 是缓冲区问题还是其他问题?我该如何完成这项任务?我希望你们能帮助我。预先感谢您。

c# unity-game-engine serial-port
1个回答
0
投票

这是因为Arduino。统一代码运行良好。我在连接到 Xbee 时尝试了该程序,一切正常。 Arduino 无法同时读写,这就是为什么我在向 Arduino 发送数据时无法接收任何数据。我曾以为我的统一代码是错误的,但事实并非如此!问题出在 Arduino 上。

© www.soinside.com 2019 - 2024. All rights reserved.