从C#发送ASCII到Arduino的使用蓝牙 [英] Sending ASCII from C# to Arduino using Bluetooth

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

问题描述

我想我的接口C# Windows窗体从我的笔记本电脑到Arduino <一应用HREF =htt​​p://arduino.cc/en/Main/ArduinoBoardDuemilanove相对=nofollow> Duemilanove 。蓝牙模块连接到Arduino上的TX和RX引脚。我的目标是点亮板载指示灯,当我在字母A,至今它一直未果类型。我相信蓝牙与我的笔记本电脑相连,但不应对信我pressing。

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 code

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 code错还是我缺少的是什么?

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

推荐答案

可以是相同的问题,我这里回答

May be the same issue as I answered here.

我最近涉足到这个。 Arduino的自动复位时,
  收到比其他的Arduino的大多数事情串行通信
  IDE。这就是为什么你可以从IDE而不是Node.js的发送。

I recently dabbled into this. The Arduino automatically resets when it receives serial communication from most things other than the Arduino IDE. This is why you can send from the IDE but not node.js.

我有一个乌诺,把复位和Ground.Here的一个之间的电容
  关于这个问题的一些好的信息页面。祝你好运。
  
http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆