我做了7段,导致了74ls48
IC connected to
Gizduino(迷你)w / ATmega328 . I wanted to count from
0-10 using
java-to-arduino。
这是Java的代码:
public class Main extends javax.swing.JFrame {
public static final int[] arrayNum= new int[20];
private BufferedReader input;
private OutputStream output=null;
SerialPort serialPort;
private final String PORT="COM18";
private static final int TIMEOUT=2000; //2000
private static final int DATA_RATE=9600; //9600
public Main() {
initComponents();
init();
setTitle("JavaDuino");
//jRadioButton1.setSelected(true);
}
public void init(){
CommPortIdentifier portId = null;
Enumeration portEnum=CommPortIdentifier.getPortIdentifiers();
while(portEnum.hasMoreElements()){
CommPortIdentifier actualPortID=(CommPortIdentifier) portEnum.nextElement();
if(PORT.equals(actualPortID.getName())){
portId=actualPortID;
break;
}
}
if(portId==null){
//mostError("Could not find COM port.");
System.exit(ERROR);
}
try{
serialPort = (SerialPort) portId.open(this.getClass().getName(), TIMEOUT);
serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
output = serialPort.getOutputStream();
}catch(Exception e){
// mostError(e.getMessage() );
System.exit(ERROR);
}
}
private void eventData(String data ){
try{
output.write(data.getBytes());
System.out.print("\nData= "+data);
} catch(Exception e){
// mostError("ERROR");
System.exit(ERROR);
}
// public void mostError(String Message){
//JOptionPane.showMessageDialog(this, message, "ERROR", JOptionPane.ERROR_MESSAGE);
}
private int i=0;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
arrayNum[i]=i;
String a=Arrays.toString(arrayNum);
System.out.println("Array="+Arrays.toString(arrayNum));
System.out.println("a="+a);
eventData(a);
i++;
if(i==10){
while(i>0){
arrayNum[i]=0;
i--;
}
i=0;
}
}
Arduino的代码:
const int a=10;
const int b=11;
const int c=12;
const int d=13;
int Byte=0;
void setup(){
Serial.begin(9600); //open connection serial port
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
}
void loop(){
if(Serial.available()>0)
{
Byte=Serial.read();
if(Byte=='0'){ //Number 1
digitalWrite(a ,LOW);
digitalWrite(b ,LOW);
digitalWrite(c ,LOW);
digitalWrite(d ,LOW);
}else if(Byte=='1'){ //Number 1
digitalWrite(a ,HIGH);
digitalWrite(b ,LOW);
digitalWrite(c ,LOW);
digitalWrite(d ,LOW);
} else if(Byte=='2'){ //Number 2
digitalWrite(a ,LOW);
digitalWrite(b ,HIGH);
digitalWrite(c ,LOW);
digitalWrite(d ,LOW);
}
else if(Byte=='3'){ //Number 3
digitalWrite(a ,HIGH);
digitalWrite(b ,HIGH);
digitalWrite(c ,LOW);
digitalWrite(d ,LOW);
} else if(Byte=='4'){ //Number 4
digitalWrite(a ,LOW);
digitalWrite(b ,LOW);
digitalWrite(c ,HIGH);
digitalWrite(d ,LOW);
} else if(Byte=='5'){ //Number 5
digitalWrite(a ,HIGH);
digitalWrite(b ,LOW);
digitalWrite(c ,HIGH);
digitalWrite(d ,LOW);
} else if(Byte=='6'){ //Number 6
digitalWrite(a ,LOW);
digitalWrite(b ,HIGH);
digitalWrite(c ,HIGH);
digitalWrite(d ,LOW);
} else if(Byte=='7'){ //Number 7
digitalWrite(a ,HIGH);
digitalWrite(b ,HIGH);
digitalWrite(c ,HIGH);
digitalWrite(d ,LOW);
} else if(Byte=='8'){ //Number 8
digitalWrite(a ,LOW);
digitalWrite(b ,LOW);
digitalWrite(c ,LOW);
digitalWrite(d ,HIGH);
} else if(Byte=='9'){ //Number 9
digitalWrite(a ,HIGH);
digitalWrite(b ,LOW);
digitalWrite(c ,LOW);
digitalWrite(d ,HIGH);
} else if(Byte=='10'){ //Number 0
digitalWrite(a ,LOW);
digitalWrite(b ,LOW);
digitalWrite(c ,LOW);
digitalWrite(d ,LOW);
}
}
}
代码似乎堆叠在0,我真的不知道为什么。请帮忙。
答案 0 :(得分:0)
Byte=Serial.read();
正在读取一个字节,可以是单个字符'0'
,.. '9'
java字符串"10"
是两个字节,'1'
,然后是'0'
。所以在loop
中,最终的案例从未使用过。而是执行'1'
案例并将'0'
留在缓冲区中。
它有两种方式。始终一次写两个字节。使用Byte1=Serial.read(); Byte2=Serial.read();
。
您可以在java中使用Integer.toString(int i, 16)
使用十六进制数字。并else if( Byte=='a') ...
。在arduino。
答案 1 :(得分:0)
您可以使用Serial.parseInt()
功能。
我遇到过同样的问题。我用过这个,它开始接受两位数没有任何麻烦。
int value = Serial.parseInt()