Я написал эскиз, который отлично работает с Python, но я не могу заставить его работать с JSerialComm в Java. Я вижу, что порт подключается и через 5 сек. RX-свет вскоре мигает, но ничего не происходит, и никаких исключений не брошено. Он работает на Raspberry Pi 5 с книжным червем.#define motorDir1 2
#define motorPwm 3
#define motorDir2 4
#define shooter 5
void setup()
{
pinMode(motorPwm, OUTPUT);
pinMode(motorDir1, OUTPUT);
pinMode(motorDir2, OUTPUT);
pinMode(shooter, OUTPUT);
analogWrite(motorPwm, 0);
digitalWrite(motorDir1, LOW);
digitalWrite(motorDir2, LOW);
digitalWrite(shooter, HIGH);
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) {
String message = Serial.readStringUntil('\n');
if(message == "shooter_shoot"){
shooter_shoot();
Serial.println("shooter_shot");
}
else if(message == "shooter_release"){
shooter_release();
Serial.println("shooter_released");
}
else{
Serial.println("unknown_message " + message);
}
}
}
void shooter_shoot()
{
digitalWrite(shooter, LOW);
}
void shooter_release()
{
digitalWrite(shooter, HIGH);
}
< /code>
Рабочий код питона: < /p>
import serial
import time
s = serial.Serial('/dev/ttyACM0', 9600)
s.close()
s.open()
time.sleep(5)
s.write(str.encode('shooter_shoot\n'))
response = s.readline().decode().strip()
print(response)
time.sleep(2)
s.write(str.encode('shooter_release\n'))
response = s.readline().decode().strip()
print(response)
< /code>
Не работающий код Java: < /p>
import com.fazecast.jSerialComm.SerialPort;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
SerialPort comPort = SerialPort.getCommPort("/dev/ttyACM0");
comPort.closePort();
comPort.openPort();
comPort.setBaudRate(9600);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
String data = "shooter_shoot\n";
byte[] writeBuffer = data.getBytes();
try {
comPort.getOutputStream().write(writeBuffer);
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
comPort.getOutputStream().flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
< /code>
Очень трудно отлаживать, потому что я ничего не вижу, когда Arduino подключен к малине. У кого -нибудь есть идея? < /P>
Заранее спасибо. Даниэль
Подробнее здесь: https://stackoverflow.com/questions/797 ... s-expected