python - 使用python将arduino中的字符串存储到文本文件

标签 python serial-port arduino

我正在使用此代码将字符串从 arduino 发送到 PC

int i=0;
void setup(){  
  Serial.begin(9600);    // Open serial connection at a baud rate of 9600
  pinMode(13, OUTPUT);   //set pin13 in o/p mode
}

void loop(){ 
while(1)
{
Serial.write("10.028371,76.328873"); 
Serial.write('\0'); 
delay(1000);
  }
}

我需要一个Python代码来接收这个字符串并将其存储在一个文本文件中。arduino正在连续传输这个字符串,但我在文本文件中只需要它一次。 我编写了下面的代码,但在文本文件中只得到垃圾值

## import the serial library
import serial

## Boolean variable that will represent 
## whether or not the arduino is connected
connected = False

## establish connection to the serial port that your arduino 
## is connected to.

locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3']

for device in locations:
    try:
        print "Trying...",device
        ser = serial.Serial(device, 9600)
        break
    except:
        print "Failed to connect on",device

## loop until the arduino tells us it is ready
while not connected:
    serin = ser.read()
    connected = True

## open text file to store the current 
##gps co-ordinates received from the rover    
text_file = open("position4.txt", 'w')
## read serial data from arduino and 
## write it to the text file 'position.txt'
while ser.read():
    x=ser.read()
    print(x) 
    if x=="\0":
      text_file.seek(0)
      text_file.truncate()   
    text_file.write(x)
    text_file.flush()
## close the serial connection and text file
text_file.close()
ser.close()

最佳答案

通过对 arduino 和 python 代码进行一些更改来解决

arduin代码:

int i=0;
void setup(){  
  Serial.begin(9600);    // Open serial connection at a baud rate of 9600
  pinMode(13, OUTPUT);   //set pin13 in o/p mode
}

void loop(){ 
while(1)
{
Serial.write('\n'); 
Serial.write("10.028371,76.328873"); 
delay(1000);
  }
}

Python代码:

## import the serial library
import serial

## Boolean variable that will represent 
## whether or not the arduino is connected
connected = False

## establish connection to the serial port that your arduino 
## is connected to.

locations=['/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3']

for device in locations:
    try:
        print "Trying...",device
        ser = serial.Serial(device, 9600)
        break
    except:
        print "Failed to connect on",device

## loop until the arduino tells us it is ready
while not connected:
    serin = ser.read()
    connected = True

## open text file to store the current 
##gps co-ordinates received from the rover    
text_file = open("position4.txt", 'w')
## read serial data from arduino and 
## write it to the text file 'position.txt'
while 1:
    if ser.inWaiting():
        x=ser.read()
        print(x) 
        text_file.write(x)
        if x=="\n":
             text_file.seek(0)
             text_file.truncate()
        text_file.flush()

## close the serial connection and text file
text_file.close()
ser.close()

关于python - 使用python将arduino中的字符串存储到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892133/

相关文章:

C++ 将类方法作为参数传递

python - 即使数据不存在,也收集数据、计数并返回字典列表

python - 无法为 "high"x 计算 e^(-x)

python - PySerial 不与 Arduino 交谈

node.js - 在各种事件下自动打开串口并记录数据

c++ - ((Thermostat*)this)->Thermostat::_dht' 没有类类型

python - <urlopen error (-1, 'SSL 异常 : Differences between the SSL socket behaviour of cpython vs. jython 在 wiki 上有解释

python - Pandas - 为所有行(特别是)重复行提供唯一标识符

boost - 如何确定是否有字节可从 boost :asio:serial_port 读取

c++ - 通过 Xbee 网络传输浮点值