【发布时间】: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