gpt4 book ai didi

python - 通过 serial1 将 Arduno Yun pin 信息发送到 linino python 脚本,通过 UDP 发送到 Max

转载 作者:可可西里 更新时间:2023-11-01 02:43:43 26 4
gpt4 key购买 nike

现在尝试让我的 arduino 草图通过 python 脚本发送。我无法让它发送过来。

Arduino 代码。

    /* Sketch for reading 4 analogue inputs for glove
*/

// Setup pin locations
int flexPin0 = A0; //analog pin 0
int flexPin1 = A1; //analog pin 1
int flexPin2 = A2; //analog pin 2
int flexPin3 = A3; //analog pin 3

int inByte = 0;

void setup(){
Serial.begin(9600);
while (!Serial) {
;
}
establishContact();
}

void loop(){
// Read values
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();

int flexSensorReading0 = analogRead(flexPin0);
delay(10);
int flexSensorReading1 = analogRead(flexPin1);
delay(10);
int flexSensorReading2 = analogRead(flexPin2);
delay(10);
int flexSensorReading3 = analogRead(flexPin3);
delay(10);

// Output results to serial
Serial.write(flexSensorReading0);
Serial.write(flexSensorReading1);
Serial.write(flexSensorReading2);
Serial.write(flexSensorReading3);

delay(50); //just here to slow down the output for easier reading
}
}

void establishContact() {
while (Serial.available() <= 0) {
Serial.println('A'); // send a capital A
delay(500);
}
}

Python脚本

#!/usr/bin/python
import socket
import serial

ser = serial.Serial('/dev/ttyATH0', 9600)


while ser.read() != 'A':
# do nothing
pass


UDP_IP = "192.168.1.242" #Max IP address
UDP_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP

while True: # ser.readline() == 'A':
data_raw = read(8) #read 8 bytes
sock.sendto(bytes(data_raw), (UDP_IP, UDP_PORT)) #sends the byte
ser.write('A')

#recvmsg=sock.recv(1024) #read response
#print recvmsg
sock.close()

现在得到错误

Traceback (most recent call last):
File "/mnt/sda1/udpgit.py", line 8, in <module>
while ser.read() != 'A':
File "/usr/lib/python2.7/site-packages/pyserial-2.7-py2.7.egg/serial/serialposix.py", line 475, in read
raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

当 arduino 端有一个空白草图时,python 草图运行,但当我尝试使用 serial1 时,它会立即出现该错误。

我正在使用 Arduino Yun 尝试通过 wifi 与 Max 通信!谢谢!

最佳答案

克服无限循环所需的一切。你只想等到你得到一个 'A',所以将条件从“始终为真”(1) 更改为“直到我们读到一个 'A'”

while ser.readline() != 'A':
//do nothing

然后打开套接字

UDP_IP = "192.168.1.242"   #Uno IP address
UDP_PORT = 8888
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP

现在我们需要读取一个 arduino 读数,它由 8 个字节组成(每个读数都是一个 int,所以 2 个字节),而且你没有写“行尾”字符(\n)所以 readLine()不会工作;

data_raw = read(8) #read 8 byte
sock.sendto(bytes(MESSAGE), (UDP_IP, UDP_PORT)) #send byte
recvmsg=sock.recv(1024) #read response messagge (are you sure aout this?)
print recvmsg

现在我们可以关闭套接字了。或循环下一个 8 字节,如果我们正在寻找另一个读数,只需循环上面的代码。

sock.close()

关于python - 通过 serial1 将 Arduno Yun pin 信息发送到 linino python 脚本,通过 UDP 发送到 Max,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22663276/

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