【问题标题】:how can in receive data from serial port in unity?如何统一从串口接收数据?
【发布时间】:2022-01-22 19:50:11
【问题描述】:

我想在微控制器(ARM Stm32F4)Unity软件制作的程序之间建立全双工 连接 /strong> 通过 C#。此连接必须通过串行端口进行。我需要这两个部分之间的双向连接。 首先,程序发送的数据(用unity软件制作)可以向微控制器(ARM Stm32F4)发送命令,然后微控制器必须检查接收到的数据并验证完成命令给Unity软件 如果数据正确,则将下一个数据发送给程序。我的问题是我的unity做的程序没有收到微控制器发送的完成数据。

如何进行全双工连接?

*
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO.Ports;
using System.IO;
using UnityEditor;
using SerialPortUtility;
using System;
public class gcode : MonoBehaviour
{
    string str;
    string str1;
    public SerialPort sina;
    public InputField sendgcode;
    public InputField plusoffset;
    public InputField minusoffset;
    public InputField lenghtoffset;
    public InputField counter;
    public Button plus_inc;
    public Button plus_dec;
    public Button minus_inc;
    public Button minus_dec;
    public Button lenght_inc;
    public Button lenght_dec;
    string board;
    
    Array[] inserial;
    //int resend = 0;
    // Start is called before the first frame update
    void Start()
    {
        sina = new SerialPort("com1", 115200);
        sina.ReadTimeout = 100;
        
        sina.Open();
        sina.DtrEnable = true;
    }
    // Update is called once per frame
    void Update()
    {
        //sina.Close();
            //str = sina.ReadLine();
            //sendgcode.text = str;
        
    }
    public void sendserial()
    {
        
        str = sendgcode.text;
        TextWriter a40 = new StreamWriter("c:\\1.txt");
        a40.WriteLine(str);
        a40.Close();
        str = "";
        TextWriter a41 = new StreamWriter("c:\\2.txt");
        TextWriter a43 = new StreamWriter("c:\\3.txt");
        TextWriter a22 = new StreamWriter("c:\\22.txt");
        TextWriter plus = new StreamWriter("c:\\p.txt");
        TextWriter minus = new StreamWriter("c:\\m.txt");
        TextWriter lenght = new StreamWriter("c:\\t.txt");
        System.IO.StreamReader file = new System.IO.StreamReader("c:\\1.txt");
        while ((str = file.ReadLine()) != null)
        {
            str1 = str.Replace(" ", "");
            str1 = str1.Replace("L", "\r\n");
            str1 = str1.Replace("A", "\r\n");
            str1 = str1.Replace("S", "");
            a41.WriteLine(str1);
        }
        a41.WriteLine("S");
        a41.Close();
        file.Close();
        System.IO.StreamReader file22 = new System.IO.StreamReader("c:\\2.txt");
        while ((str = file22.ReadLine()) != "S")
        {
            if (str.Length >= 1) { a22.WriteLine(str); }
        }
        a22.WriteLine("S");
        a22.Close();
        file22.Close();
        System.IO.StreamReader file1 = new System.IO.StreamReader("c:\\22.txt");
        while ((str = file1.ReadLine()) != "S")
        {
            int idx = str.IndexOf('-');
            if (idx >= 0)
            {
                if (str.Length == 2)
                {
                    str = str.Replace("-", "-00");
                    a43.WriteLine(str);
                }
                else if (str.Length == 3)
                {
                    str = str.Replace("-", "-0");
                    a43.WriteLine(str);
                }
                else
                {
                    a43.WriteLine(str);
                }
            }
            else if (str.Length == 1)
            {
                a43.WriteLine("00" + str);
            }
            else if (str.Length == 2)
            {
                a43.WriteLine("0" + str);
            }
            else
            {
                a43.WriteLine(str);
            }
        }
        a43.WriteLine("S");
        a43.Close();
        file1.Close();
        str1 = null;
        int c = 0;
        System.IO.StreamReader file2 = new System.IO.StreamReader("c:\\3.txt");
        while ((str = file2.ReadLine()) != "S")
        {
            if (str.Length > 2)
            {
                str1 = str1 + str;                          //read line by line to 4 line
            }
            c++;
            if (c == 4)                                     //end read 4 lines
            {
                c = 0;
                int n = 0;
                str1 = "#" + str1;
                int sp;
                
                do
                {
                    sina.WriteLine(str1);
                    board = sina.ReadExisting();
                    Debug.Log(board);               
                }
                while (board != str1);
                sina.WriteLine("$");
                str1 = null;
            }
             
            
        }
        /*
        System.Threading.Thread.Sleep(1000);
        sina.WriteLine(str1);                               //send last line of gcode     
        file2.Close();
        System.Threading.Thread.Sleep(1000);
        sina.WriteLine("p" + plusoffset.text);
        System.Threading.Thread.Sleep(1000);
        sina.WriteLine("m" + minusoffset.text);
        System.Threading.Thread.Sleep(1000);
        sina.WriteLine("t" + lenghtoffset.text);
        System.Threading.Thread.Sleep(1000);
        System.IO.StreamReader filecrc = new System.IO.StreamReader("c:\\3.txt");
        int crc = 0;
        int s1;
        int Poffset = 0;
        int Moffset = 0;
        int Toffset = 0;
        int Ecount = 0;
        while ((str = filecrc.ReadLine()) != "S")
        {
            int.TryParse(str, out s1);
            crc = crc + s1;
        }
        filecrc.Close();
        int.TryParse(plusoffset.text, out Poffset);
        int.TryParse(minusoffset.text, out Moffset);
        int.TryParse(lenghtoffset.text, out Toffset);
        int.TryParse(counter.text, out Ecount);
        crc = crc + Poffset;
        crc = crc + Moffset;
        crc = crc + Toffset;
        crc = crc + Ecount;
        System.Threading.Thread.Sleep(1000);
        sina.WriteLine("M" + crc.ToString());
        System.Threading.Thread.Sleep(1000);
        sina.WriteLine("E" + counter.text);
        plus.WriteLine(plusoffset.text); plus.Close();
        minus.WriteLine(minusoffset.text); minus.Close();
        lenght.WriteLine(lenghtoffset.text); lenght.Close();
        */
    }
    public void plusinc()
    {
        int value;
        int.TryParse(plusoffset.text, out value);
        value++;
        plusoffset.text = value.ToString();
    }
    public void plusdec()
    {
        int value;
        int.TryParse(plusoffset.text, out value);
        value--;
        plusoffset.text = value.ToString();
    }
    public void minusinc()
    {
        int value;
        int.TryParse(minusoffset.text, out value);
        value++;
        minusoffset.text = value.ToString();
    }
    public void minusdec()
    {
        int value;
        int.TryParse(minusoffset.text, out value);
        value--;
        minusoffset.text = value.ToString();
    }
    public void lenghtinc()
    {
        int value;
        int.TryParse(lenghtoffset.text, out value);
        value++;
        lenghtoffset.text = value.ToString();
    }
    public void lenghtdec()
    {
        int value;
        int.TryParse(lenghtoffset.text, out value);
        value--;
        lenghtoffset.text = value.ToString();
    }
    public void load_offest()
    {
        System.Threading.Thread.Sleep(500);
        System.IO.StreamReader offset = new System.IO.StreamReader("c:\\offset.txt");
        string stroffset;
        stroffset = offset.ReadLine();
        plusoffset.text = stroffset;
        stroffset = offset.ReadLine();
        minusoffset.text = stroffset;
        stroffset = offset.ReadLine();
        lenghtoffset.text = stroffset;
    }
    public void clear()
    {
        sendgcode.text = "";
        plusoffset.text = "";
        minusoffset.text = "";
        lenghtoffset.text = "";
    }
    void OnSerialLine(string line)
    {
        Debug.Log("Got a line: " + line);
    }
}*```

【问题讨论】:

    标签: c# unity3d serial-port microcontroller


    【解决方案1】:

    在您的do - while 循环中,您不断地编写字符串str1。这看起来已经很奇怪了。

    然后您执行board = sina.ReadExisting(),它会返回所有当前可用的流和缓冲区内容。

    根据编码在 SerialPort 对象的流和输入缓冲区中读取所有立即可用的字节。

    因为这是一个 并且没有 sich 概念,因为“消息”可能是多行/消息,或者也只是一个的一部分 => 这不太可能与您所拥有的完全匹配立即发送。

    您需要提出一个适当的协议,例如在消息之间使用特殊的分隔符。

    宁可使用ReadLine,它会一直读取到下一次遇到配置的NewLine 字符(默认为\n)或使用ReadTo(string) 作为string 模式。

    两者都将“冻结”,直到收到相应的字符,确保在继续您的代码之前完全收到整个消息。

    所以不是

    do
    {
        sina.WriteLine(str1);
        board = sina.ReadExisting();
        Debug.Log(board);               
    }
    while (board != str1);
    sina.WriteLine("$");
    

    你可能更愿意做类似的事情,例如

    sina.WriteLine(str1);
    do
    {
        board = sina.ReadLine();
        Debug.Log(board);               
    }
    while (board != str1);
    sina.WriteLine("$");
    

    如果循环是您想要使用的。


    一般来说,我总是使用两个单独的线程来发送和接收。除非您有一个明确的协议,您只需要能够在逻辑中的某些插槽中接收。

    【讨论】:

      猜你喜欢
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多