python控制arduino模拟端口,Python自动选择串行端口(用于Arduino)

"""

Written on a Windows 10 Computer, Python 2.7.9 Version.

This program automatically detects and lists ports. If no ports are found, it simply shells out. In the printout below "list(serial.tools.list_ports.comports())" finds two ports and the program lists them out - a listout shown below:

COM5 - USB-SERIAL CH340 (COM5)

Found Arduino Uno on COM5

COM4 - Microsoft USB GPS Port (COM4)

As each port is found, "CH340," (the name of the Adruino Uno) is searched for in the listed port with the "while int1 < 9:" loop. The first "If" statement looks for "CH340" and when found the integer value "int1" will be the same as the com port #. With a concatination, the operation "str1 = "COM" + str2" gives the com port name of the Adruino, eg. "COM5." The next "IF" statement looks for both "CH340" AND str1, ("COM5") in the above case. The statement "Found Arduino Uno on COM5" prints out, and "str1" is used in setting up the com port:

ser = serial.Serial(str1, 9600, timeout=10)

This program goes on to open the com port and prints data from the Adruino.

The modules "serial, sys, time, serial.tools.list_ports" must all be imported.

Written by Joseph F. Mack 01/29/2016. "A BIG Thank you" to all the individuals whose programs I "borrowed" from that are available in the many forums for Python and PYGame users!

"""

import serial

import sys

import time

import serial.tools.list_ports

serPort = ""

int1 = 0

str1 = ""

str2 = ""

# Find Live Ports

ports = list(serial.tools.list_ports.comports())

for p in ports:

print p # This causes each port's information to be printed out.

# To search this p data, use p[1].

while int1 < 9: # Loop checks "COM0" to "COM8" for Adruino Port Info.

if "CH340" in p[1]: # Looks for "CH340" in P[1].

str2 = str(int1) # Converts an Integer to a String, allowing:

str1 = "COM" + str2 # add the strings together.

if "CH340" in p[1] and str1 in p[1]: # Looks for "CH340" and "COM#"

print "Found Arduino Uno on " + str1

int1 = 9 # Causes loop to end.

if int1 == 8:

print "UNO not found!"

sys.exit() # Terminates Script.

int1 = int1 + 1

time.sleep(5) # Gives user 5 seconds to view Port information -- can be changed/removed.

# Set Port

ser = serial.Serial(str1, 9600, timeout=10) # Put in your speed and timeout value.

# This begins the opening and printout of data from the Adruino.

ser.close() # In case the port is already open this closes it.

ser.open() # Reopen the port.

ser.flushInput()

ser.flushOutput()

int1 = 0

str1 = ""

str2 = ""

while int1==0:

if "\n" not in str1: # concatinates string on one line till a line feed "\n"

str2 = ser.readline() # is found, then prints the line.

str1 += str2

print(str1)

str1=""

time.sleep(.1)

print 'serial closed'

ser.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值