Java串行连接和文件问题(Arduino和Java)

时间:2019-06-12 22:04:29

标签: java arduino arduino-uno

我有一个连接到PC的arduino uno,并且我正在尝试使用一个创建gui的java程序,因此我连接到了串行端口,并将所有数据存储在一个文件中,该文件将以今天的日期命名

但是当我使用它尝试连接到串行端口时,会出现很多错误。


import java.io.*;
import java.util.Date;
import java.util.Scanner;
import javax.swing.*;
import com.fazecast.jSerialComm.SerialPort;
import java.awt.*;
import java.awt.event.*;

public class FluidDynamicSerial implements ActionListener {

    //main frame
    JFrame window;

    //create class members for buttons/check-box and drop-down
    JComboBox<String> portList;
    JButton connectButton;
    JCheckBox multFilesDay;

    //variables for files
    boolean nameWithTime = false;
    String name;

    //warning message
    String msg = "This file already exists. By default all files are named with today's date, if you wish to fix this problem either rename the file in the current diretory or click the multiple files in one day chekbox";

    //serial port var
    SerialPort port;


    //file vars
    File file;
    FileWriter writeFile;
    BufferedWriter buffWriteFile;

    //Date var
    Date date;

    public static void main(String[] args) {
        new FluidDynamicSerial();
    }

    @SuppressWarnings("deprecation")
    public FluidDynamicSerial() {
        //create window
        window = new JFrame();
        window.setTitle("Save Sensor Readings");
        window.setSize(300, 150);
        window.setLayout(new BorderLayout());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //create drop-down and button to connect to serial port
        portList = new JComboBox<String>();
        connectButton = new JButton("Connect");

        //create Top Panel to place drop-down & button
        JPanel topPanel = new JPanel();
        topPanel.add(portList);
        topPanel.add(connectButton);
        window.add(topPanel, BorderLayout.NORTH);

        //create middle panel with hekBox to have multiple files in one day
        multFilesDay = new JCheckBox("Multiple Files in one day");
        JPanel centerPanel = new JPanel();
        centerPanel.add(multFilesDay);
        window.add(centerPanel, BorderLayout.CENTER);

        //place serial port options in drop-down
        SerialPort[] portOptions = SerialPort.getCommPorts();
        for(int i = 0; i < portOptions.length; i++) {
            portList.addItem(portOptions[i].getSystemPortName());
        }

        //file code
        date = new Date();
        String month = new Integer(date.getMonth()+1).toString();
        String yr = new Integer(date.getYear()+1900).toString();

        if(nameWithTime == true) {
            name = month + "-" + date.getDate() + "-" + yr + "_" + date.getHours() + "_" + date.getMinutes() + ".txt";
        } else {
            name = month + "-" + date.getDate() + "-" + yr + ".txt";
        }

        file = new File(name);



        window.setVisible(true);

        connectButton.addActionListener(this);
        multFilesDay.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event){
        Object control = event.getSource();

        if(control == connectButton) {
            if(connectButton.getText().equals("Connect")) {
                port = SerialPort.getCommPort(portList.getSelectedItem().toString());
                port.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                if(port.openPort()) {
                    connectButton.setText("Disconnect");
                    portList.setEnabled(false);
                }
                Scanner scan = new Scanner(port.getInputStream());
                while(scan.hasNextLine()) {
                    if(file.exists()) {
                        JOptionPane.showMessageDialog(window,
                                msg,
                                "File Dupliation Error",
                                JOptionPane.ERROR_MESSAGE);
                        scan.close();
                    } else {
                        try {
                            writeFile = new FileWriter(file);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        buffWriteFile = new BufferedWriter(writeFile);
                        String line = scan.nextLine();
                        try {
                            buffWriteFile.append(date.toString() + " " + line + " " + "\n");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                scan.close();
                try {
                    buffWriteFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                port.closePort();
                portList.setEnabled(true);
                connectButton.setText("Connect");
            }
        } else if(control == multFilesDay) {
            if(multFilesDay.isEnabled()) {
                nameWithTime = true;
            } else if(multFilesDay.isEnabled() == false) {
                nameWithTime = false;
            }
        }
    }
}

我希望程序创建一个文本文件,其名称为今天的日期,并且其中将包含来自串行端口的所有数据(电压读数)。

1 个答案:

答案 0 :(得分:0)

由于有人尝试提供帮助,我重新整理了一些代码并对其进行了修改,现在它可以正常工作了