public boolean digitalRead(int pinNumber) { String s = "dr" + pinNumber; _serialPort.writeLn(s); String result = _serialPort.readLine(); char cRes = result.charAt(0); return cRes == '0' ? false : true; }
public void connect() { _serialPort.setBaud(115200); _serialPort.setParity(gnu.io.SerialPort.PARITY_NONE); _serialPort.setDataBits(gnu.io.SerialPort.DATABITS_8); _serialPort.setStopBits(gnu.io.SerialPort.STOPBITS_1); _serialPort.setFlowControl(gnu.io.SerialPort.FLOWCONTROL_NONE); _serialPort.open(); try { for (int n = 0; n < 20; n++) { _serialPort.writeLn("99"); // writeLn flushes automatically Thread.sleep(250); if (_serialPort.available() > 0) { String response = _serialPort.readLine(); if (response.equals("11")) { Thread.sleep(250); while (_serialPort.available() > 0) _serialPort.readChar(); _isConnected = true; return; } _serialPort.close(); throw new RuntimeException( "Arduino.connect: Arduino board present but running incorrect firmware on port " + _serialPort.getName()); } Thread.sleep(250); } } catch (InterruptedException e) { throw new RuntimeException( "Arduino.connect: interrupted while waiting for response from Arduino on port" + _serialPort.getName()); } _serialPort.close(); throw new RuntimeException( "Arduino.connect: Arduino board present but running incorrect firmware on port " + _serialPort.getName()); }
public void digitalWrite(int pinNumber, boolean value) { int iValue = value ? 1 : 0; String s = "dw" + pinNumber + "," + iValue; _serialPort.writeLn(s); }
public void analogWrite(int pinNumber, int value) { String s = "aw" + pinNumber + "," + value; _serialPort.writeLn(s); }
public int analogRead(int pinNumber) { String s = "ar" + pinNumber; _serialPort.writeLn(s); String result = _serialPort.readLine(); return Integer.parseInt(result); }
public void pinMode(int pinNumber, char mode) { if (mode == 'o') mode = 'O'; else if (mode == 'i') mode = 'I'; String s = "pm" + pinNumber + "," + mode; _serialPort.writeLn(s); }