本文介绍了使用蓝牙将 ASCII 从 C# 发送到 Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 C# Windows Forms 应用程序从我的笔记本电脑连接到 Arduino Duemilanove.蓝牙模块连接到 Arduino 上的 Tx 和 Rx 引脚.我的目标是在我输入字母a"时点亮板载 LED,但到目前为止还没有成功.我确定蓝牙已与我的笔记本电脑连接,但它没有响应我按下的字母.

I am trying to interface my C# Windows Forms application from my laptop to the Arduino Duemilanove. A Bluetooth module is connected to the Tx and Rx pins on the Arduino. My goal is to light up the on-board LED when I type in the letter 'a' and so far it has been unsuccessful. I am sure the Bluetooth is connected with my laptop, but it is not responding to the letter I am pressing.

public partial class Form1 : Form
{
    private Guid service = BluetoothService.SerialPort;
    private BluetoothClient bluetoothClient;

    public Form1()
    {
        InitializeComponent();

        this.KeyPreview = true;
        this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
    }

    void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == 'a')
        {
            Stream peerStream = bluetoothClient.GetStream();
            Byte[] buffer = Encoding.ASCII.GetBytes("a");
            peerStream.Write(buffer, 0, buffer.Length);
        }
    }

    private void search_Click(object sender, EventArgs e)
    {
        BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
        BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
        bluetoothClient = new BluetoothClient();
        Cursor.Current = Cursors.WaitCursor;
        BluetoothDeviceInfo[] bluetoothDeviceInfo = { };
        bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10);
        comboBox1.DataSource = bluetoothDeviceInfo;
        comboBox1.DisplayMember = "DeviceName";
        comboBox1.ValueMember = "DeviceAddress";
        comboBox1.Focus();
        Cursor.Current = Cursors.Default;
    }

    private void Connect_Click(object sender, EventArgs e)
    {
        if (comboBox1.SelectedValue != null)
        {
            try
            {
                bluetoothClient.Connect(new BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue, service));
                MessageBox.Show("Connected");

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

Arduino 代码

int incomingByte = 0;   // For incoming serial data
void setup()
{
    pinMode(13, OUTPUT); // On-board LED as output
    Serial.begin(9600);     // Opens serial port, sets data rate to 9600 bit/s.
}

void loop()
{
    if (Serial.available() > 0)
    {
        // Read the incoming byte:
        incomingByte = Serial.read();

        if (incomingByte == 'a')
            digitalWrite(13, HIGH);
    }
}

我是否错误地发送了 ASCII 代码或我遗漏了什么?

Am I sending the ASCII code wrongly or what am I missing?

推荐答案

可能和我在这里回答的问题一样.

May be the same issue as I answered here.

我最近涉足了这个.Arduino 会自动重置接收来自除 Arduino 以外的大多数事物的串行通信集成开发环境.这就是为什么您可以从 IDE 发送但不能从 node.js 发送.

我有一个 Uno,并在 Reset 和 Ground 之间放了一个电容.这是一个页面上有一些关于这个主题的好信息.祝你好运.http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

I have an Uno and put a capacitor between Reset and Ground.Here's apage with some good info on the subject. Good luck.http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

这篇关于使用蓝牙将 ASCII 从 C# 发送到 Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:00