Menu

#4 error: I/O failed to write

open
nobody
None
5
2014-08-28
2009-08-03
Anonymous
No

I have a program that is generating the below error. I have used a serial port monitor program and the Modbus message is written correctly to the serial port and the slave sends a valid reply. Any input will be appreciated as to the cause.

execute try 1 error: I/O failed to write
execute try 2 error: I/O failed to write
net.wimpi.modbus.ModbusIOException: I/O failed to write
at net.wimpi.modbus.io.ModbusRTUTransport.writeMessage(ModbusRTUTransport.java:99)
at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:181)
at queenslandhealth.PowerToDBNewMeters.responseDouble(PowerToDBNewMeters.java:191)
at queenslandhealth.PowerToDBNewMeters.readMeters(PowerToDBNewMeters.java:242)
at queenslandhealth.PowerToDBNewMeters.run(PowerToDBNewMeters.java:406)

import java.io.*;
import net.wimpi.modbus.msg.*;
import net.wimpi.modbus.io.*;
import net.wimpi.modbus.net.*;
import net.wimpi.modbus.util.*;

import javax.swing.JOptionPane;
import javax.swing.JFrame;

import java.util.Calendar;

/**
*
* @author Onditz
*/
/*
* METERS INFORMATION:
* 1. Aircon: 8 meters 1-8
* 2. Lights: 8 meters 9-16
* 3. All power: 8 meters 17-24
* 4. Lifts: 1 meter 25
* 5. Hot Water: 1 meter 26
* 6. Ventilation: 1 meter 27
* 7. Heating: --
* 8. Common Lights: 1 meter 28
* 9. External Lights: 1 meter 29
*
* Total: 29 meters
*/
public class PowerToDBNewMeters extends Thread {

//Variables:
// private int totalMeters = 10;
private int totalMeters = 28;
boolean oclock = false;
//mysql variables
public Connection con;
public Statement stmt;
public ResultSet rs;
public Connection con2;
public Statement stmt2;
public ResultSet rs2;

//Afterhours Variables
public int actualDayOfTheWeek = 0;
public boolean isAfterhours = false;
public int startHour[] = new int[7];
public int startMinute[] = new int[7];
public int endHour[] = new int[7];
public int endMinute[] = new int[7];
public AfterhoursThread afterhoursThread;
public LoadAfterhoursThread loadAfterhoursThread;

/* The important instances of the classes mentioned before */
static SerialConnection connect = null; //the connection
static ModbusSerialTransaction trans = null; //the transaction
static ReadMultipleRegistersRequest req = null; //the request
static ReadMultipleRegistersResponse res = null; //the response
SerialParameters params = new SerialParameters();

public PowerToDBNewMeters() {
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
// con = DriverManager.getConnection("jdbc:mysql://localhost:3306/LangParade?user=root&password=onditz01");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/qhealth?user=root");
stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
con2 = DriverManager.getConnection("jdbc:mysql://localhost:3306/qhealth?user=root");
stmt2 = con2.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

params.setPortName("COM1");
params.setBaudRate(9600);
params.setDatabits(8);
params.setParity("None");
params.setStopbits(1);
params.setEncoding("RTU");
params.setEcho(true);

//4. Open the connection
connect = new SerialConnection(params);
try {
connect.open();
} catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(new JFrame(), "PowerToDB Connection Failed: " + ex.getMessage(), "MetersToDatabase Exception Dialog", JOptionPane.ERROR_MESSAGE);
}

trans = new ModbusSerialTransaction(connect);

this.loadAfterhoursThread = new LoadAfterhoursThread();
this.afterhoursThread = new AfterhoursThread();

} catch (SQLException sqlEx) {
// e.printStackTrace();
// JOptionPane.showMessageDialog(new JFrame(), "PowerToMySQL Line 219: "+e.getMessage(), "MetersToDatabase Exception Dialog", JOptionPane.ERROR_MESSAGE);
relaunch(sqlEx);
}
this.setDaemon(true);
this.start();
}

public long response(int address, int ref) {

int count = 1;
int repeat = 1;
long value[] = new long[1];

try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
//5. Prepare a request
// System.out.println(5);
req = new ReadMultipleRegistersRequest(ref, count);
req.setUnitID(address);
req.setHeadless();

//6. Prepare a transaction

trans.setRequest(req);

//7. Execute the transaction repeat times
// System.out.println(7);
int k = 0;
do {
try {
trans.execute();
res = (ReadMultipleRegistersResponse) trans.getResponse();
for (int n = 0; n < res.getWordCount(); n++) {
// System.out.println("Meter: "+address+" Register: "+ref+" Word: " + n + " = " + Integer.toHexString(res.getRegisterValue(n)));
// System.out.println("Meter: "+address+" Register: "+ref+" Word: " + n + " = " + res.getRegisterValue(n));
value[n] = res.getRegisterValue(n);
}
k++;
} catch (Exception ex) {
ex.printStackTrace();
// JOptionPane.showMessageDialog(new JFrame(), "PowerToDB response: " + ex.getMessage(), "MetersToDatabase Exception Dialog", JOptionPane.ERROR_MESSAGE);
}
k++;
} while (k < repeat);

try {
//Thread.sleep(300);
} catch (Exception e) {
e.printStackTrace();
}

return value[0];
}

public long responseDouble(int address, int ref) {

int count = 1;
int repeat = 1;
long value = 0;
int readValues[] = new int[2];

try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
//5. Prepare a request
// System.out.println(5);
req = new ReadMultipleRegistersRequest(ref, count);
req.setUnitID(address);
req.setHeadless();

//6. Prepare a transaction

trans.setRequest(req);

//7. Execute the transaction repeat times
// System.out.println(7);
int k = 0;
do {
try {
trans.execute();
res = (ReadMultipleRegistersResponse) trans.getResponse();
for (int n = 0; n < res.getWordCount(); n++) {
System.out.println("Word " + n + "=" + res.getRegisterValue(n));
readValues[n] = res.getRegisterValue(n);
// if(n==0){
// System.out.println("Word " + n + "=" + res.getRegisterValue(n));
// value = res.getRegisterValue(n)* 65536;
// }else{
// System.out.println("Word " + n + "=" + res.getRegisterValue(n));
// value = value + res.getRegisterValue(n);
// }
}
k++;
} catch (Exception ex) {
ex.printStackTrace();
// JOptionPane.showMessageDialog(new JFrame(), "PowerToDB response: " + ex.getMessage(), "MetersToDatabase Exception Dialog", JOptionPane.ERROR_MESSAGE);
}
k++;
} while (k < repeat);

// System.out.println("Long Word 0 dec=" + readValues[0]);
// System.out.println("Long Word 1 dec=" + readValues[1]);
// System.out.println("Long Word 0 dec=" + readValues[0]);
// System.out.println("Long Word 1 dec=" + readValues[1]);
//
// System.out.println("Long Word 0 bin=" + Integer.toBinaryString(readValues[0]));
// System.out.println("Long Word 1 bin=" + Integer.toBinaryString(readValues[1]));
// System.out.println("Long Word 0 bin=" + Integer.toHexString(readValues[0]));
// System.out.println("Long Word 1 bin=" + Integer.toHexString(readValues[1]));

long aux = 0;
aux = readValues[0] * 65536;

aux = aux + readValues[1];

// System.out.println("Long AUX =" + Double.toString(aux));

return aux;

}

public void readMeters() {
// Float kwh;
double kwh_int[] = new double[totalMeters];

for (int meter = 25; meter <= totalMeters; meter++) {

if(meter!=26){
double kwhDouble = responseDouble(meter, 9105);

try {
this.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}

// System.out.print("Meter: "+meter+" kwh_long: "+kwhLong+" scaleFactor: "+scaleFactor);
System.out.println(" *********** Meter:"+meter+" kwh reading DOUBLE: " + kwhDouble);

if (kwhDouble != 0.0) {
dataToDatabase(meter, kwhDouble);
}

try {
//Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Discussion


Log in to post a comment.

MongoDB Logo MongoDB