Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java串行连接和文件问题(Arduino和Java)_Java_Arduino_Arduino Uno - Fatal编程技术网

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

Java串行连接和文件问题(Arduino和Java),java,arduino,arduino-uno,Java,Arduino,Arduino Uno,我有一个arduino uno连接到我的电脑,我正试图使用一个java程序创建一个gui,这样我就可以连接到串行端口,并将所有数据存储在一个文件中,该文件将以今天的日期命名 但是当我使用它尝试连接到串行端口时,我得到了很多错误 import java.io.*; import java.util.Date; import java.util.Scanner; import javax.swing.*; import com.fazecast.jSerialComm.SerialPort; im

我有一个arduino uno连接到我的电脑,我正试图使用一个java程序创建一个gui,这样我就可以连接到串行端口,并将所有数据存储在一个文件中,该文件将以今天的日期命名

但是当我使用它尝试连接到串行端口时,我得到了很多错误


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;
            }
        }
    }
}

导入java.io.*;
导入java.util.Date;
导入java.util.Scanner;
导入javax.swing.*;
导入com.fazecast.jSerialComm.SerialPort;
导入java.awt.*;
导入java.awt.event.*;
公共类FluidDynamicSerial实现ActionListener{
//主机架
JFrame窗口;
//为按钮/复选框和下拉列表创建类成员
jComboxPortList;
JButton连接按钮;
JCheckBox multFilesDay;
//文件的变量
布尔名称WithTime=false;
字符串名;
//警告信息
String msg=“此文件已存在。默认情况下,所有文件都以今天的日期命名,如果要解决此问题,请在当前目录中重命名该文件,或单击“一天内多个文件”复选框”;
//串行端口变量
串行端口;
//文件变量
文件;
文件编写器写入文件;
缓冲写入程序buffWriteFile;
//日期变量
日期;
公共静态void main(字符串[]args){
新的FluidDynamicSerial();
}
@抑制警告(“弃用”)
公共流动态序列(){
//创建窗口
window=newjframe();
设置标题(“保存传感器读数”);
设置窗口大小(300150);
setLayout(新的BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建下拉列表和按钮以连接到串行端口
portList=newjcombobox();
connectButton=新的JButton(“连接”);
//创建顶部面板以放置下拉按钮(&P)
JPanel-topPanel=新的JPanel();
topPanel.add(端口列表);
topPanel.add(连接按钮);
添加(topPanel,BorderLayout.NORTH);
//使用hekBox创建中间面板,以便在一天内拥有多个文件
multFilesDay=newjcheckbox(“一天内有多个文件”);
JPanel centerPanel=新的JPanel();
centerPanel.add(multFilesDay);
添加(中心面板、边框布局、中心);
//将串行端口选项置于下拉列表中
SerialPort[]端口选项=SerialPort.getCommPorts();
对于(int i=0;i

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

我重新排列了一些代码,并对其进行了修改,现在它可以工作了,多亏有人试图提供帮助

我重新排列了一些代码,并对其进行了修改,现在它可以工作了,感谢所有试图提供帮助的人

您能用您遇到的错误更新问题吗?您能用您遇到的错误更新问题吗?