C# 使用I2C windows iot和Arduino写入数据
因此,我有一个Arduino和Raspberry pi 3,连接了windows IoT,如下图所示: []1 我想通过I2C将RPI用作主控设备来读取和写入Arduino从机。到目前为止,我有以下代码: 船长: 奴隶阿杜伊诺:C# 使用I2C windows iot和Arduino写入数据,c#,arduino,raspberry-pi,i2c,windowsiot,C#,Arduino,Raspberry Pi,I2c,Windowsiot,因此,我有一个Arduino和Raspberry pi 3,连接了windows IoT,如下图所示: []1 我想通过I2C将RPI用作主控设备来读取和写入Arduino从机。到目前为止,我有以下代码: 船长: 奴隶阿杜伊诺: include <Wire.h> #define SLAVE_ADDRESS 0x40 byte response[1]; // this data is sent to PI void setup() { Wire.begin(SLAVE_AD
include <Wire.h>
#define SLAVE_ADDRESS 0x40
byte response[1]; // this data is sent to PI
void setup() {
Wire.begin(SLAVE_ADDRESS); // join i2c bus with address slaveAddress
Wire.onReceive(I2CReceived);
Wire.onRequest(I2CRequest);
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void I2CRequest() {
response[0] = (byte)17;
Wire.write(response, 2); // return data to PI
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void I2CReceived(int howMany) {
Serial.println("test");
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
包括
#定义从站地址0x40
字节响应[1];//此数据被发送到PI
无效设置(){
Wire.begin(SLAVE_ADDRESS);//使用地址slaveAddress连接i2c总线
线接收(I2CReceived);
在线请求(I2CRequest);
}
void循环(){
延迟(100);
}
//每当主机请求数据时执行的函数
//此函数已注册为事件,请参阅setup()
void I2CRequest(){
响应[0]=(字节)17;
write(response,2);//将数据返回到PI
}
//每当从主机接收到数据时执行的函数
//此函数已注册为事件,请参阅setup()
收到无效I2C(整数多少){
序列号。打印号(“测试”);
而(1
我可以成功地从Arduino读取数据并将其打印到windows IoT上的文本块。但我也想向Arduino写入文本。我尝试了一些方法,但无效。请有人解释如何写入数据
我真的需要一些帮助,我不是专业的程序员,所以请尽量简单。如果我当前的代码有问题,请发表评论,以便我可以尝试改进我的代码。我认为问题是由于缺少
串行。begin
,事实上从主控器发送到从控器的数据已经收到,只是没有打印出来。p请在slaver上使用以下代码重试
#include <Wire.h>
#define SLAVE_ADDRESS 0x40
byte response[1]; // this data is sent to PI
static int index = 0;
void setup() {
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS); // join i2c bus with address slaveAddress
Wire.onReceive(I2CReceived);
Wire.onRequest(I2CRequest);
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void I2CRequest() {
Serial.println("I2C-Request");
response[0] = (byte) index ++ ;
Wire.write(response, 2); // return data to PI
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void I2CReceived(int howMany) {
Serial.println("I2C-Received");
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
#包括
#定义从站地址0x40
字节响应[1];//此数据被发送到PI
静态int指数=0;
无效设置(){
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS);//使用地址slaveAddress连接i2c总线
线接收(I2CReceived);
在线请求(I2CRequest);
}
void循环(){
延迟(100);
}
//每当主机请求数据时执行的函数
//此函数已注册为事件,请参阅setup()
void I2CRequest(){
Serial.println(“I2C请求”);
响应[0]=(字节)索引++;
write(response,2);//将数据返回到PI
}
//每当从主机接收到数据时执行的函数
//此函数已注册为事件,请参阅setup()
收到无效I2C(整数多少){
Serial.println(“I2C接收”);
而(1
我用我的Arduino UNO进行了测试,它可以工作。接收到的数据可以在串行监视器中显示
在提供的代码中,
//Wire.onReceive(I2CReceived);
被注释,因此事件未注册。请打开此代码注册接收事件,然后重试。很抱歉,上载了错误的代码,未注释的代码仍然无法使用。我想改为发送字符串/字符,您知道我该怎么做吗。
#include <Wire.h>
#define SLAVE_ADDRESS 0x40
byte response[1]; // this data is sent to PI
static int index = 0;
void setup() {
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS); // join i2c bus with address slaveAddress
Wire.onReceive(I2CReceived);
Wire.onRequest(I2CRequest);
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void I2CRequest() {
Serial.println("I2C-Request");
response[0] = (byte) index ++ ;
Wire.write(response, 2); // return data to PI
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void I2CReceived(int howMany) {
Serial.println("I2C-Received");
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}