// This code reads data from comm port and writes it to the data base(MS-Access)
//To work with this code you may need javax.comm library
//this code works with 8051 micro-controller
// this code first writes 0x80(128) to comm port and my code in assembly sends it back 0x32(50). and then //my code sends 7 bits more with 0x70 to terminate the communication
//This code is written with NetBeans IDE you can change accordingly your IDE.
-Watch video
https://youtu.be/sX0XAROhuiQ
import java.io.*;
import java.util.*;
import javax.comm.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Fms implements Runnable{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static Thread readThread,dataBase;
static int crf;
static int cwf;
static byte cxc[] = new byte[4];
static int cxm = 128;//0x80
static byte[] creadBuffer;//1byte read
static int creadBuff[];
write rt=new write();
static read rd;
public void run() {
};
public static void main(String[] args) {
int a=10;
write wr = new write();
read rd = new read(1);
int speed=0,tcount=0,jcount=0,status=0;
try{
wr.cwritemain(a);
rd.cmainx(a);
readThread.sleep(1000);
}
catch(Exception e){
}
if(j==0){
status =creadBuff[1];
jcount =creadBuff[5];
System.out.println("results : "+status +"\t"+jcount);
}
if(j==1){
speed =creadBuff[1]*256+creadBuff[2];
tcount =creadBuff[6];
System.out.println("results : "+speed +"\t"+tcount);
}
try{
ResultSet rs;
Statement stmt;
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Documents and Settings\\accounts\\My Documents\\FMSDB.mdb");
stmt=con.createStatement();
stmt.executeUpdate("INSERT INTO Job_Status (Speed,Job_Count, Total_Count)" + "VALUES('"+speed+"','"+jcount+"','"+tcount+"')" );
// rs=stmt.executeQuery("Select * from Job_Status where Date = '27-11-2010'" );
stmt.close();
con.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
}
static class write{
public void cwritemain(int m)
{
// System.out.println("in cwritemain");
cwf = m;
cxc[0] = 0x26;
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println(portList.hasMoreElements());
while (portList.hasMoreElements()) {
// System.out.println("in while");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// System.out.println("in port");
if (portId.getName().equals("COM1"))
{
// System.out.println("in com");
try {
serialPort = (SerialPort) portId.open("SimpleWriteApp32", 2000);
// System.out.println("in open");
} catch (PortInUseException e) {
}
try {
outputStream = serialPort.getOutputStream();
} catch (Exception e) {
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (Exception e) {
}
if(cwf==10){
try {
// System.out.println("in 400");
outputStream.write(cxm);// 0x80 written
//System.out.println("0x80 is written");
serialPort.close();
outputStream.close();
} catch (Exception e) {
}
}
}
}
}
}
}
static class read implements SerialPortEventListener{
public read(int s) {
}
public void cmainx(int cp) {
crf = 10;
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort) portId.open("readapp32",
2000);
} catch (PortInUseException e) {
System.out.println(e.getMessage());
}
try {
inputStream = serialPort.getInputStream();
// System.out.println("ready reading");
} catch (Exception e) {
System.out.println(e);
}
try {
serialPort.addEventListener(this);
} catch (Exception e) {
System.out.println(e);
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// System.out.println("Kam rady hai!!!");
} catch (Exception e) {
System.out.println(e);
}
}
}
}
}
public void serialEvent(SerialPortEvent event) {
//System.out.println("Kam rady hai bhai!!!");
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
creadBuffer = new byte[8];
creadBuff= new int[8];
try {
int i = 0;
while (inputStream.available() > 0) {
creadBuff[i]=inputStream.read();
System.out.println("Data available is: "+creadBuff[i]);
// if(i==1)
// System.out.println("Data available is: "+creadBuff[1]*256);
i++;
}
inputStream.close();
} catch (IOException e) {
}
System.out.println("event");
System.out.println("recieved \t"+creadBuffer[0] );
if (creadBuffer[0] == 0x70) {
System.out.println("Mil gaya 0x70"+creadBuffer[0] );
serialPort.close();
rd = new read(5);
rd.cmainx(1);// ag
}
if (creadBuffer[0] == 0x32) {
System.out.println("Mil gaya"+creadBuffer[0] );
serialPort.close();
}
serialPort.close();
}
}
}
}
Watch video
https://youtu.be/sX0XAROhuiQ
//To work with this code you may need javax.comm library
//this code works with 8051 micro-controller
// this code first writes 0x80(128) to comm port and my code in assembly sends it back 0x32(50). and then //my code sends 7 bits more with 0x70 to terminate the communication
//This code is written with NetBeans IDE you can change accordingly your IDE.
-Watch video
https://youtu.be/sX0XAROhuiQ
import java.io.*;
import java.util.*;
import javax.comm.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Fms implements Runnable{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static Thread readThread,dataBase;
static int crf;
static int cwf;
static byte cxc[] = new byte[4];
static int cxm = 128;//0x80
static byte[] creadBuffer;//1byte read
static int creadBuff[];
write rt=new write();
static read rd;
public void run() {
};
public static void main(String[] args) {
int a=10;
write wr = new write();
read rd = new read(1);
int speed=0,tcount=0,jcount=0,status=0;
try{
wr.cwritemain(a);
rd.cmainx(a);
readThread.sleep(1000);
}
catch(Exception e){
}
if(j==0){
status =creadBuff[1];
jcount =creadBuff[5];
System.out.println("results : "+status +"\t"+jcount);
}
if(j==1){
speed =creadBuff[1]*256+creadBuff[2];
tcount =creadBuff[6];
System.out.println("results : "+speed +"\t"+tcount);
}
try{
ResultSet rs;
Statement stmt;
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Documents and Settings\\accounts\\My Documents\\FMSDB.mdb");
stmt=con.createStatement();
stmt.executeUpdate("INSERT INTO Job_Status (Speed,Job_Count, Total_Count)" + "VALUES('"+speed+"','"+jcount+"','"+tcount+"')" );
// rs=stmt.executeQuery("Select * from Job_Status where Date = '27-11-2010'" );
stmt.close();
con.close();
}
catch(Exception e1)
{
System.out.println(e1);
}
}
static class write{
public void cwritemain(int m)
{
// System.out.println("in cwritemain");
cwf = m;
cxc[0] = 0x26;
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println(portList.hasMoreElements());
while (portList.hasMoreElements()) {
// System.out.println("in while");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// System.out.println("in port");
if (portId.getName().equals("COM1"))
{
// System.out.println("in com");
try {
serialPort = (SerialPort) portId.open("SimpleWriteApp32", 2000);
// System.out.println("in open");
} catch (PortInUseException e) {
}
try {
outputStream = serialPort.getOutputStream();
} catch (Exception e) {
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (Exception e) {
}
if(cwf==10){
try {
// System.out.println("in 400");
outputStream.write(cxm);// 0x80 written
//System.out.println("0x80 is written");
serialPort.close();
outputStream.close();
} catch (Exception e) {
}
}
}
}
}
}
}
static class read implements SerialPortEventListener{
public read(int s) {
}
public void cmainx(int cp) {
crf = 10;
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
try {
serialPort = (SerialPort) portId.open("readapp32",
2000);
} catch (PortInUseException e) {
System.out.println(e.getMessage());
}
try {
inputStream = serialPort.getInputStream();
// System.out.println("ready reading");
} catch (Exception e) {
System.out.println(e);
}
try {
serialPort.addEventListener(this);
} catch (Exception e) {
System.out.println(e);
}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// System.out.println("Kam rady hai!!!");
} catch (Exception e) {
System.out.println(e);
}
}
}
}
}
public void serialEvent(SerialPortEvent event) {
//System.out.println("Kam rady hai bhai!!!");
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
creadBuffer = new byte[8];
creadBuff= new int[8];
try {
int i = 0;
while (inputStream.available() > 0) {
creadBuff[i]=inputStream.read();
System.out.println("Data available is: "+creadBuff[i]);
// if(i==1)
// System.out.println("Data available is: "+creadBuff[1]*256);
i++;
}
inputStream.close();
} catch (IOException e) {
}
System.out.println("event");
System.out.println("recieved \t"+creadBuffer[0] );
if (creadBuffer[0] == 0x70) {
System.out.println("Mil gaya 0x70"+creadBuffer[0] );
serialPort.close();
rd = new read(5);
rd.cmainx(1);// ag
}
if (creadBuffer[0] == 0x32) {
System.out.println("Mil gaya"+creadBuffer[0] );
serialPort.close();
}
serialPort.close();
}
}
}
}
Watch video
https://youtu.be/sX0XAROhuiQ
No comments:
Post a Comment