我将数据从python脚本发送到我的Arduino板。我可以成功地与董事会沟通。根据python发送的数据,连接的步进电机应向前或向后移动。如果python正在发送' 0'电机应该向前旋转,如果python正在发送' 1'那么电机应该向后旋转。当我键入' 0'在串行监视器中,步进器向前移动并开启“1”。步进器向后移动。但是当python最初发送数据时,步进器不会向前或向后移动。只有当状态发生变化时(如果状态从' 0'或' 0' 0'到' 1'),步进器向前或向后移动。我试图查看是否从python发送数据,arduino正在从python接收数据。建议将受到高度赞赏。
Ardunio代码
static const int SELENOID_PIN = 2;
static const int OPEN = '0';
static const int CLOSE = '1';
static const int STEPS = 30;
static const int delaylegnth = 10;
void setup() {
//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B
Serial.begin(9600);
while(!Serial){}//wait until the serial port is connected
Serial.write('1');
pinMode(SELENOID_PIN ,OUTPUT);
}
void polarisingA()
{
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
delay(delaylegnth);
}
void polarisingB()
{
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, HIGH); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B
delay(delaylegnth);
}
void polarisingC()
{
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
delay(delaylegnth);
}
void polarisingD()
{
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, LOW); //Sets direction of CH B
analogWrite(11, 255); //Moves CH B
delay(delaylegnth);
}
void stepRight()
{
polarisingA();
polarisingB();
polarisingC();
polarisingD();
}
void stepLeft();
{
polarisingD();
polarisingC();
polarisingB();
polarisingA();
}
void spin(bool dir,int tickes)
{
//Serial.print(dir);
for(int i =0 ; i < tickes; i++)
{
if(dir == true)//right
{
stepRight();
}
else
{
stepLeft();
}
}
}
void loop() {
if(Serial.available() > 0)
{//there is incoming data form the serial and serial is setted up
handleData();
}
delay(5);
}
void handleData()
{
unsigned char a = Serial.read();
Serial.write("new data");
int action = -1;
if(a == OPEN)
{
action = 1;
}
else if(a == CLOSE)
{
//Serial.print("close");
action = 0;
}
if (action != -1)
{
bool dir = action == 1 ? HIGH : LOW ;
spin(dir,STEPS);
digitalWrite(SELENOID_PIN ,action ? HIGH : LOW );
Serial.write(a);//ack
}
}
Python代码(仅部分向arduino发送数据):
def openthedoor(set_accepted_list,set_list_ant_id,set_forbidden_list,set_accepted_list_frozen):
if(((len(set_accepted_list)) >0) & (set_forbidden_list == set()) & ((set_accepted_list_frozen == None) or ((set_accepted_list_frozen & set_accepted_list)== set_accepted_list))):
print"yes,open the gate"
use_step(1)
else:
print"no,close the gate"
use_step(0)
def establishing_connection():
#serial.Serial("COM1",9600)
print ser.read();
last_action = -1
def use_door(activate):
global last_action
#global serial
if(last_action != activate):
send_data(activate)
last_action = activate
def send_data(data):
global ser
try:
if(ser == None):
ser = serial.Serial("COM1",9600,timeout = 0)
print "reconnect"
if(data==0):
ser.write('0')
print "python is telling the arduino to move stepper forward"
else:
ser.write('1')
print "python is telling the ardunio to move stepper backward"
time.sleep(0)
except IOError:
time.sleep(0)
ser.close()
#continue
ser = None
print "after read"