Im trying to make connection between my computer and arduino uno using Java Simple Serial Connector. Im trying to do it using code listed below. Somehow its not working ( the led diode connected to pin 7 of arduino is not turning on while running my programm, but when im using serial monitor of artuino software it does. ). Does anyone know why?
Java project code :
import jssc.SerialPort;
import jssc.SerialPortException;
public class Main {
public static void main(String[] args) {
//In the constructor pass the name of the port with which we work
SerialPort serialPort = new SerialPort("COM3");
try {
//Open port
serialPort.openPort();
//We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
//Writes data to port
serialPort.writeBytes("Test".getBytes());
//Closing the port
serialPort.closePort();
}
catch (SerialPortException ex) {
System.out.println(ex);
}
}
}`
Arduino code:
void setup() {
Serial.begin(9600); //Ustawienie prędkości transmisji
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
}
void loop() {
if( Serial.available() > 0){
digitalWrite(7, HIGH);
}
}
I think, your Arduinocode is wrong.
I do it like this.
https://www.arduino.cc/en/Serial/Write
Serial.write(val)
Serial.write(str) Serial.write(buf, len)
val: a value to send as a single byte str: a string to send as a series of bytes buf: an array to send as a series of bytes len: the length of the buffer