Picproje Elektronik Sitesi

PROGRAMLAMA DİLLERİ => Python => Konuyu başlatan: toor - 27 Temmuz 2016, 00:09:47

Başlık: Python ve Arduino seri iletişim
Gönderen: toor - 27 Temmuz 2016, 00:09:47
Python 3 dilinde pySerial modülünü kullanarak geliştirdiğim basit bir seri iletişim projesi :

! No longer available (http://www.youtube.com/watch?v=5BtnkDbD18w#)

Arduinonumu port üzerinden gelecek 0 ve 1 değerlerine göre iş yapması için ayarladım. Led yakıyor ve Buzzer'dan beep sesi çıkartıyordu.

Bu da Python betiğinin kodları :

import serial
import serial.tools.list_ports
from termcolor import colored


b = 0
x = 0
cmdlist = ["help", "exit", "list_ports", "sel_port", "cls_port", "send_sgnl", "crnt_port"]


def show_help(len, spc):

h= " "

if len == 0:
h = "print this output"
if len == 1:
h = "exit the program"
if len == 2:
h= "listing the com ports"
if len == 3:
h ="opening a port to use"
if len == 4:
h="closing the selected port"
if len == 5:
h = "sending a signal to microcontroller "
if len == 6:
h = "show the current port"
return cmdlist[len] + " " *(16-spc)  + "-- > " + h


print(colored("Use \'help\' command for help", "yellow"))
ser = serial.Serial()
while x == 0:

a = input(">>> ")

if a == cmdlist[1]:
x = 1

elif a == cmdlist[3]:
adrs = input("Address : ")
speed = input("Speed : ")
ser = serial.Serial(adrs, speed)
print("Completed")
elif a == cmdlist[6]:
print(ser.name)
elif a == cmdlist[5]:
value = input("change value(1 or 0) : ")
if int(value) == 1:
ser.write(b'1')
print("Completed")
elif int(value)==0:
ser.write(b'0')
print("Completed")
else:
print("bad value")

elif a == cmdlist[4]:
ser.close()
print("Completed")
elif a == cmdlist[2]:
print("-" * 5 + colored(" Ports ", "cyan") + "-" * 5)
ports = list(serial.tools.list_ports.comports())
for p in ports:
print(p)
print("-" * 17)

elif a == cmdlist[0]:
print("-" *3 + colored(" Commands ", "cyan") + "-" * 4)

while b < len(cmdlist):
print(str(int(b) + 1) + ". " + show_help(b,len(cmdlist[b])))
b = int(b)+1
#operator.add(b, 1)
b = 0
print("-" * 17)