繁体   English   中英

NXT转PC蓝牙短信

[英]NXT to pc bluetooth text message

如何从nxt向PC发送蓝牙文本消息并在PC中读取?

我用这个:

     byte[] byteOut = new byte[65];
        int i = 0;
        try
        {
            byteOut[0] = (byte)(TextBox1.TextLength + 5);
            //number bytes in output message
            byteOut[1] = 0x0;
            //should be 0 for NXT
            byteOut[2] = 0x00;
            //&H0 = reply expected &H80 = no reply expected
            byteOut[3] = 0x9;
            //Send Bluetooth
            byteOut[4] = 0x0;
            //Box Number - 1
            byteOut[5] = (byte)(TextBox1.TextLength + 1);
            //message size with null terminator
            //copy bytes into output array
            for (i = 1; i <= TextBox1.TextLength; i++)
            {
                byteOut[(i + 5)] = Convert.ToByte(Asc(TextBox1.Text.Substring((i - 1), 1)));
            }

            byteOut[TextBox1.TextLength + 6] = 0x0;
            //add null terminator
            SerialPort1.Write(byteOut, 0, TextBox1.TextLength + 7);
            //send message


            // I try to use this for reading but it doesn't work good. It gets a lot                           of numbers and no text.

            char[] read1 = new char[65];

            SerialPort1.Read(read1, 0, TextBox1.TextLength + 7);

            string box1 = "";



            for (int i1 = 0; i1 < read1.Length; ++i1)
            {
                box1 += read1[i1];
            }

            MessageBox.Show(box1);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

我试图用它来阅读,但效果不好。 它得到很多数字,没有文本。

我该怎么办?

将此功能添加到您的代码中:

 private string NXTSendCommandAndGetReply(byte[] Command)
    {
        string r = "";
        Byte[] MessageLength = { 0x00, 0x00 };
        MessageLength[0] = (byte)Command.Length;
        BluetoothConnection.Write(MessageLength, 0, MessageLength.Length);
        BluetoothConnection.Write(Command, 0, Command.Length);
        int length = BluetoothConnection.ReadByte() + 256 * BluetoothConnection.ReadByte();
        for (int i = 0; i < length; i++)
            r += BluetoothConnection.ReadByte().ToString("X2");
        return r;
    }

那么您可以使用以下方式发送号码:

private void send(int a)
{
    byte[] NxtMessage = { 0x00, 0x09, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 };
    NxtMessage[2] = (byte)(0);
    int tmp = (int)a;
    for (int ByteCtr = 0; ByteCtr <= 3; ByteCtr++)
    {
        NxtMessage[4 + ByteCtr] = (byte)tmp;
        tmp >>= 8;
    }
    NXTSendCommandAndGetReply(NxtMessage);
}

发送文本与此:

public void send(string s)
{
    byte[] NxtMessage = new byte[5 + s.Length];
    NxtMessage[0] = 0x00;
    NxtMessage[1] = 0x09;
    NxtMessage[2] = 0x00;
    NxtMessage[3] = (byte)(s.Length + 1);
    byte[] array = Encoding.ASCII.GetBytes(s);
    for (int ByteCtr = 0; ByteCtr < array.Length; ByteCtr++)
    {
        NxtMessage[4 + ByteCtr] = array[ByteCtr];
    }
    NxtMessage[4 + s.Length] = 0x00;
    NXTSendCommandAndGetReply(NxtMessage);
}

从nxt获取编号:

private int read(int mailbox)
{
    byte[] NxtMessage = { 0x00, 0x13, 0x00, 0x00, 0x01 };
    NxtMessage[2] = (byte)(mailbox - 1 + 10);
    string r = NXTSendCommandAndGetReply(NxtMessage);
    return Convert.ToInt32((r[10] + "" + r[11]), 16);
}

并从nxt获取文本:

private string read_txt(int mailbox)
{
    byte[] NxtMessage = { 0x00, 0x13, 0x00, 0x00, 0x01 };
    NxtMessage[2] = (byte)(mailbox - 1 + 10);
    string r = NXTSendCommandAndGetReply(NxtMessage);
    int a = 0;
    string recieved = "";
    for (int i = 10; ; i += 2)
    {
        a = int.Parse(r[i] + "" + r[i+1], System.Globalization.NumberStyles.HexNumber);
        if (a == 0)
            break;
        recieved += char.ConvertFromUtf32(a);
    }
    return r;
}

检出http://geekbrothers.org/index.php/categories/electronics-computers/20-video-control-robot ,了解使用蓝牙向nxt发送号码并下载源代码的一种非常有用的方法。 并结帐http://geekbrothers.org/index.php/teachings我将放置一个完整的教程,以很快地与nxt之间收发蓝牙。 如果您还有其他问题,可以使用geekbrothers contact-us表格,也可以发送电子邮件至info@geekbrothers.org

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM