问题描述
我打算发送这 50 多个字符串来制作一个分发奖品的游戏,所以我使用 Unity 开发游戏,所以我的主要想法是使用此代码作为字符串发送代码的基础想法几乎准备好了我想我在强类型的 C# 上有一些问题。我对 C# 真的很陌生,我更熟悉 Java。所以我接受各种建议
谢谢
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.Threading;
using System;
public class vendComms : MonoBehavIoUr {
// Action Constants
const string CAB1 = "02 06 00 85 00 01 8e";
const string CAB2 = "02 06 00 85 00 02 8f";
const string CAB3 = "02 06 00 85 00 03 90";
const string CAB4 = "02 06 00 85 00 04 91";
const string CAB5 = "02 06 00 85 00 05 92";
const string CAB6 = "02 06 00 85 00 06 93";
const string CAB11 = "02 06 00 85 00 0b 98";
const string CAB12 = "02 06 00 85 00 0c 99";
const string CAB13 = "02 06 00 85 00 0d 9a";
const string CAB14 = "02 06 00 85 00 0e 9b";
const string CAB15 = "02 06 00 85 00 0f 9c";
const string CAB16 = "02 06 00 85 00 10 9d";
const string CAB17 = "02 06 00 85 00 11 9e";
const string CAB18 = "02 06 00 85 00 12 9f";
const string CAB19 = "02 06 00 85 00 13 a0";
const string CAB21 = "02 06 00 85 00 15 a2";
const string CAB22 = "02 06 00 85 00 16 a3";
const string CAB23 = "02 06 00 85 00 17 a4";
const string CAB24 = "02 06 00 85 00 18 a5";
const string CAB25 = "02 06 00 85 00 19 a6";
const string CAB26 = "02 06 00 85 00 1a a7";
const string CAB27 = "02 06 00 85 00 1b a8";
const string CAB28 = "02 06 00 85 00 1c a9";
const string CAB29 = "02 06 00 85 00 1d aa";
const string CAB31 = "02 06 00 85 00 1f ac";
const string CAB32 = "02 06 00 85 00 20 ad";
const string CAB33 = "02 06 00 85 00 21 ae";
const string CAB34 = "02 06 00 85 00 22 af";
const string CAB35 = "02 06 00 85 00 23 b0";
const string CAB36 = "02 06 00 85 00 24 b1";
const string CAB37 = "02 06 00 85 00 25 b2";
const string CAB38 = "02 06 00 85 00 26 b3";
const string CAB39 = "02 06 00 85 00 27 b4";
const string CAB41 = "02 06 00 85 00 29 b6";
const string CAB42 = "02 06 00 85 00 2a b7";
const string CAB43 = "02 06 00 85 00 2b b8";
const string CAB44 = "02 06 00 85 00 2c b9";
const string CAB45 = "02 06 00 85 00 2d ba";
const string CAB46 = "02 06 00 85 00 2e bb";
const string CAB47 = "02 06 00 85 00 2f bc";
const string CAB48 = "02 06 00 85 00 30 bd";
const string CAB49 = "02 06 00 85 00 31 be";
const string CAB51 = "02 06 00 85 00 33 c0";
const string CAB52 = "02 06 00 85 00 34 c1";
const string CAB53 = "02 06 00 85 00 35 c2";
const string CAB54 = "02 06 00 85 00 36 c3";
const string CAB55 = "02 06 00 85 00 37 c4";
const string CAB56 = "02 06 00 85 00 38 c5";
const string CAB57 = "02 06 00 85 00 39 c6";
const string CAB58 = "02 06 00 85 00 3a c7";
const string CAB59 = "02 06 00 85 00 3b c8";
const string CAB00 = "02 06 00 85 00 00 00";
// Port for connecting to vending;
System.IO.Ports.SerialPort sp;
//string arrays for sending and recieving bit strings
string[] send,receive;
// string to send when ready
string to_be_sent_;
// bools to continue loops in threads
bool continueReading_;
bool continueWriting_;
//Thread readThread;
// Use this for initialization
void Start () {
sp = new SerialPort();
sp.PortName = "usbserial-14620";
sp.Baudrate = 9600;
sp.Parity = Parity.None;
sp.DataBits = 8;
sp.StopBits = StopBits.One;
sp.Handshake = Handshake.None;
send = new string[1];
receive = new string[1];
this.to_be_sent_ = CAB00;
//sp.DataReceived += get_serial_data;
try
{
sp.open();
Debug.Log("Puerto Abierto");
//// Setup the thread to poll the port on
Thread readThread = new Thread(new ThreadStart(get_serial));
this.continueReading_ = true;
readThread.Start();
}
catch (Exception e)
{
Debug.Log("Error abriendo el Puerto: " + e.Message);
}
}
string get_command(string command)
{
if (command == CAB1)
return "Gabinete 1";
else if (command == CAB2)
return "Gabinete 2";
else if (command == CAB3)
return "Gabinete 3";
else if (command == CAB4)
return "Gabinete 4";
else if (command == CAB5)
return "Gabinete 5";
else if (command == CAB6)
return "Gabinete 6";
else
return "Comando Desconocido..";
}
void get_serial()
{
while (continueReading_)
{
//sp.Read(receive,1);
// RECEIVING STRINGS
receive[0] = (string)sp.ReadByte();
Thread.Sleep(20);
sp.discardInBuffer();
UnityEngine.MonoBehavIoUr.print("Recibiendo: " + get_command(receive[0]));
}
}
void Ondisable()
{
sp.Close();
}
// Update is called once per frame
void Update () {
// Set the signal to be sent
if (Input.GetKeyDown(KeyCode.A))
{
this.to_be_sent_ = CAB1;
}
if (Input.GetKeyDown(KeyCode.B))
{
this.to_be_sent_ = CAB2;
}
if (Input.GetKeyDown(KeyCode.C))
{
this.to_be_sent_ = CAB3;
}
if (Input.GetKeyDown(KeyCode.D))
{
this.to_be_sent_ = CAB4;
}
// Send the signal
if (this.to_be_sent_ != CAB00)
{
//> set the string to be sent
send[0] = (string)this.to_be_sent_;
// Write to the lamp
sp.WriteLine(send,1 );
sp.discardOutBuffer();
print("Enviado: " + get_command(send[0]));
this.to_be_sent_ = (string)CAB00;
}
}
}
解决方法
receive[0] = (string)sp.ReadByte();
能编译吗?您不能将 byte
转换为 string
。看起来你想建立一个字节的消息。您可以通过在从端口读取时一次填充一个字节来完成此操作。收到完整消息后,将进程转换为字符串。
void get_serial()
{
byte [] receive = new byte[7];
int index = 0;
while (continueReading_)
{
// Read a byte and add to the array
byte data = sp.ReadByte();
if (data == 2) {
index = 0; // Start of msg
}
receive[index++] = data;
// Check for full msg
if (index == 7) {
if (receive[0] == 2) {
// Convert byte array to string
string formattedBytes = String.Concat(receive.Select(i => string.Format("{0:x2} ",i))).Trim();
var cmd = get_command(formattedBytes);
UnityEngine.MonoBehaviour.print("Recibiendo: " + cmd);
}
index = 0;
}
}
}
然而,我建议使用内置的 SerialPort
回调功能,而不是使用线程。看起来您尝试过,但已将其注释掉。
// sp.DataReceived += get_serial_data;
取消注释,去掉 Thread
并对 get_serial
进行一些小的修改。
byte [] receive = new byte[7];
int index = 0;
void get_serial_data()
{
while (SerialPort.BytesToRead != 0)
{
byte data = sp.ReadByte();
.... the rest is the same ....
}
}