2016-08-03 5 views
0

回転する磁場の速度を調整するプログラムを作成しています。基本的に、私はちょうどユーザーの意図された速度を表すためにシリアルポートを介してフロートを送信しようとしています。しかし、私は本当に意味をなさないエラーが発生しています。私はずっと小さな部分でエラーを分離しました。PySerial:TypeError: 'float'オブジェクトが反復不可能です

コード:私は、スクリプトを実行する場合、これは誤りである

import serial #imports PySerial Library 

#Function allows for user input and conversion to float. 
#If not float, "Invalid" is printed to the console, and input is requested again 
def get_float(prompt): 
     while True: #Main Loop 
       try: 
         response = raw_input(prompt) #User inputs 
         return float(response) #Conversion is attempted 
       except: 
         print("Invalid") #If conversion fails 

def magnet_speed(): 
     input = get_float('Enter a speed in rpm to immediately change rotation speed\n>> ') 
     print input 
     arduino.write(input) #send to arduino 



arduino = serial.Serial('/dev/ttyACM0', 9600) #declares which serial port the arduino is con$ 

magnet_speed() 
exit() 

Enter a speed in rpm to immediately change rotation speed 
>> a 
Invalid 
Enter a speed in rpm to immediately change rotation speed 
>> 4 
4.0 
Traceback (most recent call last): 
    File "magnet_speed.py", line 22, in <module> 
    magnet_speed() 
    File "magnet_speed.py", line 16, in magnet_speed 
    arduino.write(input) #send to arduino 
    File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 498, in write 
    d = to_bytes(data) 
    File "/usr/local/lib/python2.7/dist-packages/serial/serialutil.py", line 66, in to_bytes 
    for item in seq: 
TypeError: 'float' object is not iterable 

私の唯一の考えは、私がget_float()右からfloatを返すいないよ場合、または私はありません変数inputが正しく定義されています。 get_float()関数は、pythonシェルで実行すると、入力された数値を単独で出力することは間違いありません。

+0

arduino.write(出力)には繰り返し可能なアイテムが必要です。試してみてくださいarduino.write([出力]) – galaxyan

+0

@ galaxyan:シリアルポート*バイト文字列を扱う*。 Pythonではiterableです。 –

+0

@MartijnPietersは理にかなっています。ありがとうございます。 – galaxyan

答えて

1

serial.Serial.write()浮動小数点値ではなく、strオブジェクトに渡されることが予期されます。 strオブジェクトは反復可能です。 pyserial documenationから

Write the bytes data to the port. This should be of type bytes (or compatible such as bytearray or memoryview). Unicode strings must be encoded (e.g. 'hello'.encode('utf-8').

Changed in version 2.5: Accepts instances of bytes and bytearray when available (Python 2.6 and newer) and str otherwise.

パイソン2、bytesにおいては、(それが簡単に前方のPython 3と互換性のあるコードを記述できるようにする)strの別名です。

arduino.write(str(output)) 

またはより正確にフロートはバイトに変換される方法を制御する別の方法を使用します。

最初文字列にあなたのフロートを変換します。あなたは、小数点の後に入れているどのように多くの桁数を制御するためにformat() functionを使用することができ、および/または科学表記法は、これまでで、使用するか、値をCと互換性のバイナリ表現を生成するためにstruct.pack() functionを使用することができた場合:

arduino.write(format(output, '.4f')) # fixed point with 4 decimals 
arduino.write(pack('>d', output))  # big-endian C double 

あなたが選ぶものは、Arduinoが読もうとしているものによって決まります。

+0

ありがとうございます。 arduino側の文字列を扱うのはもう少し難しいですが、それはうまくいくはずです! –

+0

@ T.ZackCrawford:シリアルポートはバイトのみを送信します。 arduino stdlibには、バイトストリームから 'double '値をデコードする方法があると確信しています。 –

+0

@ T.ZackCrawford:文字列表現が期待されるように見える['parseFloat()'関数(https://www.arduino.cc/en/Serial/ParseFloat)があります。 –

関連する問題