gpt4 book ai didi

c# - 使用蓝牙将 ASCII 从 C# 发送到 Arduino

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:22 25 4
gpt4 key购买 nike

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

C#代码

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 码有误还是我遗漏了什么?

最佳答案

可能和我回答的是同一个问题here .

I recently dabbled into this. The Arduino automatically resets when itreceives serial communication from most things other than the ArduinoIDE. 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 apage with some good info on the subject. Good luck.http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

关于c# - 使用蓝牙将 ASCII 从 C# 发送到 Arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11048062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com