arduino出现java错误_java - Java串行连接和文件问题(Arduino和Java) - 堆栈内存溢出...

本文档描述了在使用Java程序通过串行端口与Arduino Uno交互时遇到的错误,特别是关于文件命名和堆栈内存溢出的问题。作者尝试创建一个GUI,该GUI将从Arduino读取数据并将其保存到以日期命名的文件中。当文件已存在或尝试连接串行端口时,程序会出现错误。文章提供了代码示例以展示问题所在。
摘要由CSDN通过智能技术生成

我将arduino uno连接到我的PC,并且尝试使用创建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 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();

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;

}

}

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值