这是我的 Java 代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jsscusbconnection;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
/**
*
* @author Heshan
*/
public class JsscUsbConnection {
/**
* @param args the command line arguments
*/
static SerialPort serialPort;
public static void main(String[] args) {
serialPort = new SerialPort("COM15");
try {
System.out.println("port open :" + serialPort.openPort());//Open port
serialPort.setParams(SerialPort.BAUDRATE_9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
serialPort.setEventsMask(mask);//Set mask
serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
static class SerialPortReader implements SerialPortEventListener {
public void serialEvent(SerialPortEvent event) {
if (event.isRXCHAR()) {//If data is available
//System.out.println(event.getEventValue());
if (event.getEventValue() > 4) {//Check bytes count in the input buffer
//Read data, if 10 bytes available
try {
byte buffer[] = serialPort.readBytes(4);
System.out.println(buffer[0] + " " + buffer[1] + " " + buffer[2] + " " + buffer[3]);
} catch (SerialPortException ex) {
System.out.println(ex);
}
}
} else if (event.isCTS()) {//If CTS line has changed state
if (event.getEventValue() == 1) {//If line is ON
System.out.println("CTS - ON");
} else {
System.out.println("CTS - OFF");
}
} else if (event.isDSR()) {///If DSR line has changed state
if (event.getEventValue() == 1) {//If line is ON
System.out.println("DSR - ON");
} else {
System.out.println("DSR - OFF");
}
}
}
}
}
这是我的 Arduino 代码:
int k=0;
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println(67,BYTE);
Serial.println(98,BYTE);
Serial.println(34,BYTE);
Serial.println(108,BYTE);
Serial.flush();
}
这是我的 Java 代码输出:
67 13 10 98
13 10 34 13
10 108 13 10
67 13 10 98
那些大胆的价值观是什么?我不知道这些未知值。