from time import sleep
import serial
from msvcrt import getch
##==============================================================================
ser =serial.Serial("COM4", 115200, timeout=2) # Establish the connection on a specific port
##==============================================================================
##=======getchar========================
def getchar():
#Returns a single character from standard input
key = getch() ##Get byte ex: b'a'
key_num=ord(key) ##convert byte to integer 97
key_chr=chr(key_num) ##convert integer to char 'a'
return key_num
##====================================
##======Write Serial Command to arduino============
def SerialWrite(command):
ser.write(cmd)
rv=ser.readline()
#print (rv) # Read the newest output from the Arduino
print (rv.decode("utf-8"))
sleep(1) # Delay for one tenth of a second
ser.flushInput()
##====================================
##=======Get Ready================
print("Connecting to Arduino.....")
for i in range (1,10):
rv=ser.readline()
print("Loading...")
#Debug print (rv) # Read the newest output from the Arduino
print (rv.decode("utf-8"))
ser.flushInput()
sleep(1) # Delay for one tenth of a secon
Str=rv.decode("utf-8")
#Debug print(Str[0:5])
if Str[0:5]=="Ready":
print("Get Arduino Ready !")
break
##------------------------------------------------------
print("==================================")
##counter = 65 # "A"
##ser.write(chr(counter).encode('utf-8')) # Convert the decimal number to ASCII then send it to the Arduino
cmd="Key in the Command".encode("utf-8")
SerialWrite(cmd)
##===Get char from keyboard then send to arduino and get it back to print in screen==
while True:
chr_num = getchar()
cmd=(chr(chr_num).encode('utf-8'))
SerialWrite(cmd)
if chr_num==27: ##ESC
break
ser.close()
<Source code in Arduino>
//*****************************************************************************
//ArbuluckyChat V1.0
//阿布拉機的3D列印與機器人
//http://arbu00.blogspot.tw/
//
//2016/07/20 Writen By Ashing Tsai
//
//******************************************************************************
const int LedPin12=12;
const int LedPin11=11;
const int LedPin10=10;
String Str01="";
void setup() {
Serial.begin(115200); // set the baud rate
Serial.println("Ready"); // print "Ready" once
pinMode(LedPin12,OUTPUT);
pinMode(LedPin11,OUTPUT);
pinMode(LedPin10,OUTPUT);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
}
void loop() {
if (Serial.available())
{
Str01="";
delay(1);
while(Serial.available())
{
Str01+=(char)Serial.read();
}
Serial.println(Str01); // send the data back in a new line so that it is not all one long line
}
if (Str01[0]=='a')
{
digitalWrite(LedPin12,HIGH);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
delay(500);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,HIGH);
digitalWrite(LedPin10,LOW);
delay(500);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,HIGH);
delay(500);
}
if (Str01[0]=='b')
{
digitalWrite(LedPin12,HIGH);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
delay(100);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,HIGH);
digitalWrite(LedPin10,LOW);
delay(100);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,HIGH);
delay(100);
}
if (Str01[0]=='c')
{
digitalWrite(LedPin12,HIGH);
digitalWrite(LedPin11,HIGH);
digitalWrite(LedPin10,HIGH);
delay(300);
digitalWrite(LedPin12,LOW);
digitalWrite(LedPin11,LOW);
digitalWrite(LedPin10,LOW);
delay(300);
}
}