exception type IOException
in
Programming Questions
•
1 year ago
Hello,
I'm trying to make a program which logs the data received at my com-port. You probably also know the arduino-boards, I attached a sensor to this board and made a connection with my pc. The program I load on my arduino works, but the one on processing gives an error and I can't find the problem.
The error is: unhandled exception type IOException. I searched the internet and I saw a couple of posts on this topic. But they all say I have to add "throw IOEception" in the line. This doesn't work, so I was wondering if someone on this forum might know the solution.
I'm trying to make a program which logs the data received at my com-port. You probably also know the arduino-boards, I attached a sensor to this board and made a connection with my pc. The program I load on my arduino works, but the one on processing gives an error and I can't find the problem.
The error is: unhandled exception type IOException. I searched the internet and I saw a couple of posts on this topic. But they all say I have to add "throw IOEception" in the line. This doesn't work, so I was wondering if someone on this forum might know the solution.
- import processing.serial.*;
- import java.util.Date;
- Serial myPort;
- FileWriter file;
- String filename;
- void setup()
- {
- myPort = new Serial(this, "COM4", 115200);
- long thisDate = new Date().getTime();
- filename = "output-" + thisDate + ".txt";
- try {
- String emptyStart = "";
- file = new FileWriter(filename, false); // Wtrite empty string to file to clear it on start
- file.write(emptyStart, 0, emptyStart.length()); //(string, start char, end char)
- //file.close();
- }
- catch(Exception e)
- {
- println("Error: Can't open file!");
- }
- }
- void draw()
- {
- readSerial();
- }
- void readSerial()
- {
- println(myPort.available());
- if (myPort.available() > 5) {
- int serial_in = myPort.read(); // read a byte from the Serial Port
- while (serial_in != 255) {
- serial_in = myPort.read(); // read until value 255 is read
- }
- int lowByte = myPort.read();
- int highByte = myPort.read();
- int sample = highByte * 256 + lowByte;
- lowByte = myPort.read();
- highByte = myPort.read();
- int timestamp = highByte * 256 + lowByte;
- myPort.clear();
- }
- }
- void keyPressed(){
- if ( key == 's' ){
- file.close();
- exit();
- }
- }

1