当我使用Arduino串行工具时,它可以工作,当我尝试通过PHP发送消息('w','a','s'等)时,它只适用于WI尝试用S代替W但是它不会工作,所以它只适用于W我也将incomingByte变量类型更改为int并尝试使用数字而不是字符,但它不会工作
PHP代码
if (isset($_GET['action'])) {
require('php_serial.class.php');
$serial = new phpSerial();
$serial->deviceSet('COM4');
$serial->confBaudRate(9600);
$serial->deviceOpen();
if ($_GET['action'] == 'f1') {
$serial->sendMessage("w\r");
} else if ($_GET['action'] == 'f2') {
$serial->sendMessage("p\r");
} else if ($_GET['action'] == 'f3') {
$serial->sendMessage("s\r");
} else if ($_GET['action'] == 'f4') {
$serial->sendMessage("a\r");
} else if($_GET['action'] == 'f5') {
$serial->sendMessage("d\r");
}
$serial->deviceClose();
}
Arduino代码
int MOTOR_1A = 4; // Motor 1 direction
int MOTOR_1B = 5; // Motor 1 Power
int MOTOR_2A = 7; // Motor 2 direction
int MOTOR_2B = 6; // Motor 2 Power
int MOTOR_CURRENT = 0; // Current
byte incomingByte;
void setup() {
pinMode (MOTOR_1A, OUTPUT);
pinMode (MOTOR_1B, OUTPUT);
pinMode (MOTOR_2A, OUTPUT);
pinMode (MOTOR_2B, OUTPUT);
pinMode (MOTOR_CURRENT, INPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer
incomingByte = Serial.read();
Serial.print(incomingByte);
if (incomingByte == 'w') { //===> W
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_1B, HIGH);
digitalWrite(MOTOR_2B, HIGH);
} else if (incomingByte == 's') { //===> S
digitalWrite(MOTOR_1A, LOW);
digitalWrite(MOTOR_2A, LOW);
digitalWrite(MOTOR_1B, HIGH);
digitalWrite(MOTOR_2B, HIGH);
} else if (incomingByte == 'a') { //===> A
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_1B, HIGH);
digitalWrite(MOTOR_2B, LOW);
} else if (incomingByte == 'd') { //===> D
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2B, HIGH);
} else if (incomingByte == 'p') { //===> p
digitalWrite(MOTOR_1A, HIGH);
digitalWrite(MOTOR_2A, HIGH);
digitalWrite(MOTOR_1B, LOW);
digitalWrite(MOTOR_2B, LOW);
}
}
}