如何通过Arduino蓝牙模块将数据实时传递到运行python脚本的PC?
我的电路中有一个力敏电阻FSR,我想让我的Arduino通过蓝牙将这些数据传送到我的PC,运行python脚本 以下是我在本项目中使用的蓝牙屏蔽: 我试图模仿这里的例子,但这两种情况都没有涉及到Arduino蓝牙与PC的蓝牙交互的情况,当我使用他们的草图时,代码甚至无法上传 操作系统:Windows 10 以下是我的FSR的代码:如何通过Arduino蓝牙模块将数据实时传递到运行python脚本的PC?,python,c,bluetooth,arduino,Python,C,Bluetooth,Arduino,我的电路中有一个力敏电阻FSR,我想让我的Arduino通过蓝牙将这些数据传送到我的PC,运行python脚本 以下是我在本项目中使用的蓝牙屏蔽: 我试图模仿这里的例子,但这两种情况都没有涉及到Arduino蓝牙与PC的蓝牙交互的情况,当我使用他们的草图时,代码甚至无法上传 操作系统:Windows 10 以下是我的FSR的代码: const int fsrAnalogPin = A0; int fsrReading; void setup(void) { // put your setu
const int fsrAnalogPin = A0;
int fsrReading;
void setup(void) {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop(void) {
// put your main code here, to run repeatedly:
fsrReading = analogRead(fsrAnalogPin);
int num = fsrReading/3;
Serial.print("Analog reading = ");
Serial.println(num);
delay(50);
}
以下是尚未实现的Python脚本Bluetooth的代码:
import serial
serialArduino = serial.Serial('COM4', 9600)
while True:
while (serialArduino.inWaiting() == 0):
pass
valueRead =(serialArduino.readline())
print(valueRead)
我可以在我的FSR代码和Python代码中更改什么来通过蓝牙发送和接收数据?在您的代码中,您没有初始化到屏蔽的串行连接 根据所使用的Arduino板,您应该选择eshiled使用的串行端口 如果您使用Uno yo,则必须使用软件串行库与您的代码进行通信,如本例所示
#include <SoftwareSerial.h>
#define RxD 7
#define TxD 6
SoftwareSerial BlueToothSerial(RxD,TxD);
void setup()
{
Serial.begin(38400);
BlueToothSerial.begin(38400);
delay(500);
}
void loop()
{
if(BlueToothSerial.available())
{
Serial.print(char(BlueToothSerial.read()));
}
if(Serial.available())
{
BlueToothSerial.print(char(Serial.read()));
}
}
如果您使用Mega-check serial,请检查屏蔽所使用的序列号,并修改上面的代码
您没有说明您的PC上运行的操作系统取决于您必须选择的操作系统通信方式。对于Windows,如果要使用COM端口,必须先与arduino配对。然后为屏蔽提供的服务添加COM端口,即SPP一次
一旦你得到了COM号码,你就可以在PC端的脚本中使用它,并从Arduino读取数据
更好的方法是在没有COM端口的情况下与蓝牙设备通信。如果您使用Windows,则可以使用在Python脚本中添加什么?从蓝牙设备读取的代码。或者,如果您的Bluetoth设备与PC配对,并且为SPP服务创建了vCOM4,则保持原样