Serial COM Port Value in to database
How save the serial COMport value in mysql databse.
Following code is here.
package simpleread;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputstream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM18")) {
// if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
inputstream = serialPort.getInputStream();
} catch (IOException e) {System.out.println(e);}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(100,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
readThread = new Thread(this);
readThread.start();
}
@Override
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {System.out.println(e);}
}
@Override
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[1024];
int[] readBuffer1 = new int[1024];
int m=0;
try {
while (inputstream.available() > 0) {
int numBytes=0;
readBuffer1[m] =inputstream.read((readBuffer));
System.out.println(new String(readBuffer));
//System.out.println(inputstream);
try {
Thread.sleep(6000);
} catch (InterruptedException ex) {
Logger.getLogger(SimpleRead.class.getName()).log(Level.SEVERE, null, ex);
}
try
{
Connection con=(Connection) ConnectionFactory.getConnection();
System.out.println(con);
String sql= "insert into Read2(DTST) Values(?)";
PreparedStatement ps=con.prepareStatement(sql);
ps.setInt(1,readBuffer1[m]);
ps.executeUpdate();
// System.out.println(""+numBytes);
// System.out.print(new String(readBuffer));
System.out.println((readBuffer1[m]));
// System.out.println(new String(readBuffer, "US-ASCII"));
m++;
}
catch(Exception e)
{
}
}
} catch (IOException e) {System.out.println(e);}
break;
}
}
}
plz help.
View Answers
Ads
Related Tutorials/Questions & Answers:
Serial COM Port Value in to database
Serial COM Port Value in to database How save the
serial COMport
value in mysql databse.
Following code is here.
package simpleread;
import...();
if (portId.getPortType() == CommPortIdentifier.PORT_
SERIAL
Advertisements
jar file not reading input from serial port
jar file not reading input from
serial port i used a coding for getting data from
serial port. JOptionbox with "
port not found" message is shown... from comm
port. why it is happening? i attached the coding i used.
public class
jar file not reading input from serial port
jar file not reading input from
serial port i used a coding for getting data from
serial port. JOptionbox with "
port not found" message is shown... from comm
port. why it is happening? i attached the coding i used.
public class
Subtract a value in the database from java
Subtract a
value in the
database from java Hi, i want to know how to subtract a
value on a
database. im using JDBC connection from access and i want to decrement the
value of table "quantity" of 1 each time it passes a loop my
Update value of database using hibernate.
Update
value of
database using hibernate. How to update
value of
database using hibernate?
Hibernate provides facility to update... this by setting the
value and calling the update method.
session.update(employee
how to delete the access database value in jsp
how to delete the access
database value in jsp i loaded my
database data into the dropdown list box...now i want to delete the
value..plz send the source code to delete the
value in dropdown list box and also from
database
how to delete the access database value in jsp
how to delete the access
database value in jsp i loaded my
database data into the dropdown list box...now i want to delete the
value..plz send the source code to delete the
value in dropdown list box and also from
database
com in ms word
com in ms word how to use
com object to fetch data from ms word and storing it into sql
database
com in ms word
com in ms word how to use
com object to fetch data from ms word and storing it into sql
database
check radio button on retrieving the value from database.
check radio button on retrieving the
value from
database. HI
i am...
database and according to that i want to check the
cash cheque radio button... information from
database the i want to show
cash radio button checked.How can i do
how to insert checkbox value into database using jsp
how to insert checkbox
value into
database using jsp How to insert check box
value to the oracle
database using jsp?
I want to create hotel's...;
<td><input name="pack_id" type="type"
value="<%= pack_id %>" size
Retrieving value from multiple table in database
Retrieving
value from multiple table in database Hi fnds, I want to maintain the financial
database of 20 users for 1 year and update the details in jsp page.. so i have decided to maintain the details based on month (because