Sending ASCII from C# to Arduino using Bluetooth

不打扰是莪最后的温柔 提交于 2019-12-11 03:51:56

问题


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.

C# code

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);
    }
}

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


回答1:


May be the same issue as I answered here.

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.

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



来源:https://stackoverflow.com/questions/11048062/sending-ascii-from-c-sharp-to-arduino-using-bluetooth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!