使用python将字符串从arduino存储到文本文件 [英] Storing string from arduino to text file using python

查看:162
本文介绍了使用python将字符串从arduino存储到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using this code to send a string from arduino to 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正在连续传输此字符串,但我在文本文件中只需要一次. 我已经写了下面的代码,但是在文本文件中只得到了垃圾值

I need a python code that receives this string and store it in a text file as such.The arduino is transmitting this string continuously but i need it only once in the text file. I have written the below code but am getting only junk values in the text file

## 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代码中进行一些更改来解决

solved by making some changes in both arduino and python codes

验证码:

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将字符串从arduino存储到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆