使用arduino

时间:2016-02-05 17:31:01

标签: java arduino serial-communication

  

我正在尝试将一些数据发送到arduino的串行监视器,然后使用它开启led但是我无法得到它我的代码中有什么问题请检查它是arduino代码

int led = 12; // LED connected to digital pin 13
int pts = 2;  // Powertail Switch 2 connected to digital pin 2
int recv = 0; // byte received on the serial port

void setup() {
// initialize onboard LED (led), Powertail (pts) and serial port
  pinMode(led, OUTPUT);
 pinMode(pts, OUTPUT);
 digitalWrite(led,LOW);
Serial.begin(9600);
}

void loop()
{

  // if serial port is available, read incoming bytes
  if (Serial.available() > 0) {
  recv = Serial.read();

// if 'y' (decimal 121) is received, turn LED/Powertail on
// anything other than 121 is received, turn LED/Powertail off
if (recv == 121){
  digitalWrite(led, HIGH);
  digitalWrite(pts,LOW);
} else {
  digitalWrite(led, LOW);
  digitalWrite(pts,HIGH);
}

// confirm values received in serial monitor window
Serial.print("--Arduino received: ");
Serial.println(recv);
 }
 }

这是java代码

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.fazecast.jSerialComm.SerialPort;
 import javax.swing.JLabel;
 import javax.swing.ImageIcon;
 import javax.swing.SwingConstants;

public class Connection {

static SerialPort chosenPort;

public static void main(String[] args) throws FileNotFoundException {

    // create and configure the window
    JFrame window = new JFrame();
    window.setTitle("Automatic Car Wash");
    window.setSize(1000, 600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().setLayout(null);
    Date DateAndTime = Calendar.getInstance().getTime();
    PrintWriter out = new PrintWriter("Incom Record Save.txt");


    // create a drop-down box and connect button, then place them at the top of the window
    JComboBox<String> portList = new JComboBox<String>();
    JButton connectButton = new JButton("Connect");
    JPanel topPanel = new JPanel();
    topPanel.setBounds(0, 0, 584, 33);
    topPanel.add(portList);
    topPanel.add(connectButton);
    window.getContentPane().add(topPanel);

    JLabel stageone = new JLabel("Stage One gif");
    stageone.setIcon(new ImageIcon("E:\\thesis\\first stage gif.gif"));
    stageone.setBounds(76, 95, 160, 120);
    stageone.setVisible(false);
    window.getContentPane().add(stageone);

    JLabel stageoneimage = new JLabel("Stage One image ");
    stageoneimage.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\stagoneimage.jpg"));
    stageoneimage.setBounds(76, 95, 160, 120);
    window.getContentPane().add(stageoneimage);

    JLabel TotalCars = new JLabel("Number of Cars");
    TotalCars.setHorizontalAlignment(SwingConstants.CENTER);
    TotalCars.setBounds(156, 270, 138, 14);

    window.getContentPane().add(TotalCars);

    JLabel lblTotalCars = new JLabel("Total Cars=");
    lblTotalCars.setHorizontalAlignment(SwingConstants.CENTER);
    lblTotalCars.setBounds(104, 270, 81, 14);
    window.getContentPane().add(lblTotalCars);

    JLabel TotalIncome = new JLabel("0");
    TotalIncome.setHorizontalAlignment(SwingConstants.CENTER);
    TotalIncome.setBounds(365, 270, 92, 14);

    window.getContentPane().add(TotalIncome);

    JLabel RS = new JLabel("RS");
    RS.setHorizontalAlignment(SwingConstants.CENTER);
    RS.setBounds(416, 270, 46, 14);
    window.getContentPane().add(RS);

    JLabel DateTime = new JLabel("Todays Date And Time");
    DateTime.setHorizontalAlignment(SwingConstants.CENTER);
    DateTime.setBounds(184, 294, 173, 25);
    DateTime.setText(DateAndTime.toString());
    window.getContentPane().add(DateTime);

    JButton btnSave = new JButton("  Save");
    btnSave.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            out.close();

        }
    });
    btnSave.setBounds(26, 295, 89, 23);
    window.getContentPane().add(btnSave);

    JLabel lblTotalIncome = new JLabel("Total Income=");
    lblTotalIncome.setHorizontalAlignment(SwingConstants.CENTER);
    lblTotalIncome.setBounds(304, 270, 92, 14);
    window.getContentPane().add(lblTotalIncome);

    JLabel thanks = new JLabel("thanks");
    thanks.setHorizontalAlignment(SwingConstants.CENTER);
    thanks.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\thanks.gif"));
    thanks.setBounds(36, 333, 312, 218);
    thanks.setVisible(false);
    window.getContentPane().add(thanks);

    JLabel lblStageTwoGif = new JLabel("stage two gif");
    lblStageTwoGif.setBounds(236, 95, 160, 120);
    window.getContentPane().add(lblStageTwoGif);

    JLabel stagetwoimage = new JLabel("stage2image");
    stagetwoimage.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\stagetwoimage.jpg"));
    stagetwoimage.setBounds(236, 95, 160, 120);
    window.getContentPane().add(stagetwoimage);

    JLabel stage3gif = new JLabel("stage3gif");
    stage3gif.setBounds(395, 95, 160, 120);
    window.getContentPane().add(stage3gif);

    JLabel stage3image = new JLabel("stageimage3");
    stage3image.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\stagethree.jpg"));
    stage3image.setBounds(395, 95, 160, 120);
    window.getContentPane().add(stage3image);

    JLabel Stagefourimage = new JLabel("stagefourimage");
    Stagefourimage.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\stagefour.jpg"));
    Stagefourimage.setBounds(555, 95, 160, 120);
    window.getContentPane().add(Stagefourimage);

    JLabel Stagefourgif = new JLabel("stagefourgif");
    Stagefourgif.setBounds(555, 95, 160, 120);
    window.getContentPane().add(Stagefourgif);

    JLabel Stagefiveimage = new JLabel("stagefiveimage");
    Stagefiveimage.setIcon(new ImageIcon("C:\\Users\\DELL\\Desktop\\stage 5image.jpg"));
    Stagefiveimage.setBounds(715, 95, 160, 120);
    window.getContentPane().add(Stagefiveimage);

    JLabel Stagefivegif = new JLabel("stagefivegif");
    Stagefivegif.setBounds(715, 95, 160, 120);
    window.getContentPane().add(Stagefivegif);

    // populate the drop-down box
    SerialPort[] portNames = SerialPort.getCommPorts();
    for(int i = 0; i < portNames.length; i++)
        portList.addItem(portNames[i].getSystemPortName());




    // configure the connect button and use another thread to listen for data
    connectButton.addActionListener(new ActionListener(){
        @Override public void actionPerformed(ActionEvent arg0) {
            if(connectButton.getText().equals("Connect")) {
                // attempt to connect to the serial port
                chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
                chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                if(chosenPort.openPort()) {
                    connectButton.setText("Disconnect");
                    portList.setEnabled(false);
                }


                Thread thread = new Thread(){
                    @Override public void run() {
                        // wait after connecting, so the bootloader can finish
                        try {Thread.sleep(100); } catch(Exception e) {}

                        // enter an infinite loop that sends text to the arduino
                        PrintWriter output = new PrintWriter(chosenPort.getOutputStream());
                        while(true) {
                            output.println("y");
                            output.flush();
                            try {Thread.sleep(100); } catch(Exception e) {}
                        }
                    }
                };
                thread.start();

                // create a new thread that listens for incoming text and populates the graph
                Thread thread2 = new Thread(){
                    @Override public void run() {
                        int count = 0;
                        int total_income = 0;
                        int car_price=300;



                        Scanner scanner = new Scanner(chosenPort.getInputStream());
                        while(scanner.hasNextLine()) {

                            try {
                                String line = scanner.nextLine();
                                int number = Integer.parseInt(line);
                                System.out.println(number);

                                if(number==1)
                                {
                                    count++;
                                    stageone.setVisible(true);
                                    stageoneimage.setVisible(false);
                                    total_income=count*car_price;
                                    out.println("Date= "+DateAndTime+" Number of Cars="+count+" Total Income="+total_income);
                                    TotalCars.setText(String.valueOf(count));
                                    TotalIncome.setText(String.valueOf( total_income));
                                    thanks.setVisible(true);



                                }





                            } catch(Exception e) {}
                        }
                        scanner.close();

                    }
                };
                thread2.start();
            } else {
                // disconnect from the serial port
                chosenPort.closePort();
                portList.setEnabled(true);
                connectButton.setText("Connect");

            }
        }
    });



    // show the window
    window.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:0)

我通过改变output.println(“y”)来解决这个问题; to output.write(“y”); 它的工作就像一个chorm 谢谢你的回复