我想用c#将字符串发送到串行端口以实现统一,但有点卡住了

我想用c#将字符串发送到串行端口以实现统一,但有点卡住了,c#,string,unity3d,serial-port,C#,String,Unity3d,Serial Port,嗯,我打电话要发送这50+个字符串来制作一个分发奖品的游戏,所以我使用Unity来开发游戏,所以我的主要想法是使用这段代码作为字符串发送代码的基础。在我的想法中,这段代码几乎准备好了。我想我对强类型C#有一些问题。我对C#真的很陌生,我更喜欢Java。所以我接受各种建议 谢谢 using UnityEngine; using System.Collections; using System.IO.Ports; using System.Threading; using System; pu

嗯,我打电话要发送这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, 0, 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, 0, 1 );
 
            sp.DiscardOutBuffer();
            print("Enviado:       " + get_command(send[0]));
 
            this.to_be_sent_ = (string)CAB00;
        }
 
    }
}
receive[0]=(字符串)sp.ReadByte()编译吗?不能将
字节
强制转换为
字符串
。看起来您想要建立一个字节的消息。可以通过在从端口读取数据时一次填充一个字节的数组来实现这一点。当您收到完整消息时,将其转换为进程字符串

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;
取消注释,丢失
线程
,并对
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 ....
    }
}

端口名称应类似于
“COM1”
“\\.\COM1”
。假设这是Windows.Im当前在Mac上工作,也使用TTL到RS232与机器通信