2009-03-24 8 views

答えて

44
import serial 
ser = serial.Serial(0) # open first serial port 
print ser.portstr  # check which port was really used 
ser.write("hello")  # write a string 
ser.close()    # close port 

、私はそれはとても素敵なインタフェースのように思えるpyserialを使用しますがhttp://pyserial.wiki.sourceforge.net/pySerialでAPIドキュメントをもとにしていない、より多くの例

+0

。新しいもの:http://pythonhosted.org/pyserial/ – MarredCheese

1

ために使用http://pyserial.wiki.sourceforge.net/pySerial。あなたが扱っているデバイス/ラジオ/のATコマンドの仕様を二重チェックする価値があります。

具体的には、コマンドモードに入るためにATコマンドの前および/または後にある程度の無音期間が必要なものがあります。私はいくつか遅れていない応答を最初に読んで好きではないものに遭遇しました。今壊れているリンク

76

ブログポストSerial RS232 connections in Python

import time 
import serial 

# configure the serial connections (the parameters differs on the device you are connecting to) 
ser = serial.Serial(
    port='/dev/ttyUSB1', 
    baudrate=9600, 
    parity=serial.PARITY_ODD, 
    stopbits=serial.STOPBITS_TWO, 
    bytesize=serial.SEVENBITS 
) 

ser.isOpen() 

print 'Enter your commands below.\r\nInsert "exit" to leave the application.' 

input=1 
while 1 : 
    # get keyboard input 
    input = raw_input(">> ") 
     # Python 3 users 
     # input = input(">> ") 
    if input == 'exit': 
     ser.close() 
     exit() 
    else: 
     # send the character to the device 
     # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device) 
     ser.write(input + '\r\n') 
     out = '' 
     # let's wait one second before reading output (let's give device time to answer) 
     time.sleep(1) 
     while ser.inWaiting() > 0: 
      out += ser.read(1) 

     if out != '': 
      print ">>" + out 
+8

このコードを実行すると、 'serial.serialutil.SerialException:Port is already open'というエラーが表示されました。私はこれについては確信していませんが、あなたが 'ser'で行ったように明示的に定義されると、シリアルポートが自動的に開かれると思います。 'ser.open()'行をコメントアウトした後、それは機能しました。 – user3817250

+0

このコメントは救世主です。 –

+1

@ user3817250:単にser.isopen()を持つ 'ser.open()' –

20

http://www.roman10.net/serial-port-communication-in-python/comment-page-1/#comment-1877

#!/usr/bin/python 

import serial, time 
#initialization and open the port 

#possible timeout values: 
# 1. None: wait forever, block call 
# 2. 0: non-blocking mode, return immediately 
# 3. x, x is bigger than 0, float allowed, timeout block call 

ser = serial.Serial() 
#ser.port = "/dev/ttyUSB0" 
ser.port = "/dev/ttyUSB7" 
#ser.port = "/dev/ttyS2" 
ser.baudrate = 9600 
ser.bytesize = serial.EIGHTBITS #number of bits per bytes 
ser.parity = serial.PARITY_NONE #set parity check: no parity 
ser.stopbits = serial.STOPBITS_ONE #number of stop bits 
#ser.timeout = None   #block read 
ser.timeout = 1   #non-block read 
#ser.timeout = 2    #timeout block read 
ser.xonxoff = False  #disable software flow control 
ser.rtscts = False  #disable hardware (RTS/CTS) flow control 
ser.dsrdtr = False  #disable hardware (DSR/DTR) flow control 
ser.writeTimeout = 2  #timeout for write 

try: 
    ser.open() 
except Exception, e: 
    print "error open serial port: " + str(e) 
    exit() 

if ser.isOpen(): 

    try: 
     ser.flushInput() #flush input buffer, discarding all its contents 
     ser.flushOutput()#flush output buffer, aborting current output 
       #and discard all that is in buffer 

     #write data 
     ser.write("AT+CSQ") 
     print("write data: AT+CSQ") 

     time.sleep(0.5) #give the serial port sometime to receive the data 

     numOfLines = 0 

     while True: 
      response = ser.readline() 
      print("read data: " + response) 

     numOfLines = numOfLines + 1 

     if (numOfLines >= 5): 
      break 

     ser.close() 
    except Exception, e1: 
     print "error communicating...: " + str(e1) 

else: 
    print "cannot open serial port " 
関連する問題