Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用I2c接口在Arduino和Raspberry PI之间发送和接收字符串数据_Python_C++_Arduino_Raspberry Pi_I2c - Fatal编程技术网

Python 使用I2c接口在Arduino和Raspberry PI之间发送和接收字符串数据

Python 使用I2c接口在Arduino和Raspberry PI之间发送和接收字符串数据,python,c++,arduino,raspberry-pi,i2c,Python,C++,Arduino,Raspberry Pi,I2c,我正在从事一个项目,该项目涉及Arduino Uno和Raspberry Pi之间的I2C通信。我试图从Pi向Arduino发送字符串数据,反之亦然 这是我的Arduino代码 #包括 #包括 #定义从站地址0x12 int flag_int_to_send_to_PI=0; int flag_int_从_收到的_PI=0; char data_to_send_PI[]=“hello PI”; 从_pi=“”接收的字符串数据_; 无效设置(){ 连线开始(从站地址); 有线接收(接收数据); W

我正在从事一个项目,该项目涉及Arduino Uno和Raspberry Pi之间的I2C通信。我试图从Pi向Arduino发送字符串数据,反之亦然

这是我的Arduino代码

#包括
#包括
#定义从站地址0x12
int flag_int_to_send_to_PI=0;
int flag_int_从_收到的_PI=0;
char data_to_send_PI[]=“hello PI”;
从_pi=“”接收的字符串数据_;
无效设置(){
连线开始(从站地址);
有线接收(接收数据);
Wire.onRequest(发送数据);
Serial.begin(9600);
标志_int _to _send _to _PI=1;
}
void循环(){
}
无效接收数据(整数字节计数){
while(Wire.available())
{
flag_int_received_from_PI=Wire.read();
if(标志\u int\u从\u PI==1接收到的\u)
{
Serial.println(“PI需要数据[]);
标志_int _to _send _to _PI=3;
}  
if(从_PI==3接收的标志_int_)
{
println(“PI想说它是ok的。”);
//从pi读取字符串:这里(如何?)
while(Wire.available())
{
从_pi+=(char)Wire.read()接收的数据;
}
串行打印(“从PI接收的数据:”);
Serial.println(从pi接收的数据);
从_pi=“”收到的数据;
}
}
}
void sendData(){
如果(标志为发送到PI==1)
{
Serial.println(“对话开始:向PI发送1”);
Wire.write(标记int到send到PI);
}
如果(flag_int_to_send_to_PI==3){
Wire.write(数据发送到PI);
}
标志_int _to _send _to _PI=0;
}
这是Raspberry Pi代码:

从smbus导入smbus
导入时间
总线=SMBus(1)
slaveAddress=0x12
从Arduino接收的数据
数据发送至Arduino=“你好,Uno”
def StringToBytes(val):
retVal=[]
对于val中的c:
检索附加(ord(c))
返回返回
而(一):
尝试:
x=总线读取字节(slaveAddress)
如果(x==1):
打印(x)
总线写入字节(slaveAddress,1)
睡眠时间(0.2)
从Arduino=总线接收数据。读取i2c块数据(slaveAddress,0,12)
打印(从Arduino收到的数据)
总线写入字节(slaveAddress,3)
打印(字符串字节(数据发送到数据)
总线写入i2c块数据(slaveAddress,0x00,StringToBytes(数据发送到Arduino))
这是串行和控制台输出

Arduino串行输出:

Conversation begin : sending 1 to PI
PI Wants data[].
PI Wants To say its ok.
Data Received From PI:
Raspberry Pi控制台:

1
[0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
[72, 101, 108, 108, 111, 32, 85, 110, 111]
它是如何工作的

Arduino向Pi发送1,以启动两次通信。
Pi发回1以确认它。
Arduino将数据字符串发送到Pi。
Pi收到数据并将3发回Arduino,表示一切正常。(但情况并不顺利)。
Pi然后将数据字符串发送到Arduino,我不知道如何在Arduino端读取该字符串,尽管我尝试过

我的问题是:

为什么Raspberry Pi在缓冲区中的每一处都接收255个?
Arduino如何获得Pi发送的字节数组