1
Technical support / java with plc
« on: March 12, 2002, 07:38:51 PM »
hi there,
is there a way in which allow the communication between java (from pc) and plc . I needed plc to be able to communicate (sending/recieving) with java comm . at the moment it is not possible for me to communicate.. below are my source code for java
public class ReadPorts implements SerialPortEventListener
{
private SerialPort serialp = null;
private static CommPortIdentifier comportiden ;
private InputStream inp ;
private CommPort comp ;
private Enumeration enum ;
private InputStream input = null;
private OutputStream output;
private int invalue, portnum, inputport ;
private DisplayPict2 display ;
private GetScan scan ;
// SerialPort serialPort = null;
public ReadPorts()
{
try
{
String portName = "COM2" ;
comportiden =CommPortIdentifier.getPortIdentifier(portName);
// System.out.println( comportiden.getCurrentOwner()) ;
serialp = (SerialPort)comportiden.open("Download", 1000);
serialp.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,SerialPort.PARITY_NONE );
serialp.addEventListener(this) ;
//scan = new GetScan();
//before need to have getscan class ready
// display = new DisplayPict2("save.jpg") ;
portnum = display.getPortNumber() ;
System.out.println("Port number" + portnum);
System.out.println(serialp.getBaudRate());
System.out.println(serialp.isDSR());
}
catch (Exception e)
{
System.out.println("ERROR");
}
// testRead();
}
public void testRead()
{
try
{
input = serialp.getInputStream();
invalue = input.read() ;
checkDecision() ;
}
catch(Exception e)
{
System.out.println("ERROR WITH INPUT");
}
}
public void checkDecision()
{
if (invalue ==0 )
{
//break ;
}
else if (invalue ==1)
{
display = new DisplayPict2("picture name");
portnum = display.getPortNumber();
sendData(portnum) ;
}
}
public void sendData(int i)
{
try
{
output.write(i) ;
}
catch( IOException e)
{
System.out.println("Error writing");
}
}
public void closePorts()
{
try
{
input.close();
output.close();
}
catch( IOException e)
{
System.out.println(" Error closing port");
}
}
public void serialEvent(SerialPortEvent event)
{
if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE);//DSR)//DATA_AVAILABLE)
{
try
{System.out.println("In the serial");
inputport = input.read();
System.out.println("In the serial");
}
catch(IOException e)
{
System.out.println("error with input data");
}
}
}
public static void main(String args[])
{
ReadPorts e = new ReadPorts();
}
}
is there a way in which allow the communication between java (from pc) and plc . I needed plc to be able to communicate (sending/recieving) with java comm . at the moment it is not possible for me to communicate.. below are my source code for java
public class ReadPorts implements SerialPortEventListener
{
private SerialPort serialp = null;
private static CommPortIdentifier comportiden ;
private InputStream inp ;
private CommPort comp ;
private Enumeration enum ;
private InputStream input = null;
private OutputStream output;
private int invalue, portnum, inputport ;
private DisplayPict2 display ;
private GetScan scan ;
// SerialPort serialPort = null;
public ReadPorts()
{
try
{
String portName = "COM2" ;
comportiden =CommPortIdentifier.getPortIdentifier(portName);
// System.out.println( comportiden.getCurrentOwner()) ;
serialp = (SerialPort)comportiden.open("Download", 1000);
serialp.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,SerialPort.PARITY_NONE );
serialp.addEventListener(this) ;
//scan = new GetScan();
//before need to have getscan class ready
// display = new DisplayPict2("save.jpg") ;
portnum = display.getPortNumber() ;
System.out.println("Port number" + portnum);
System.out.println(serialp.getBaudRate());
System.out.println(serialp.isDSR());
}
catch (Exception e)
{
System.out.println("ERROR");
}
// testRead();
}
public void testRead()
{
try
{
input = serialp.getInputStream();
invalue = input.read() ;
checkDecision() ;
}
catch(Exception e)
{
System.out.println("ERROR WITH INPUT");
}
}
public void checkDecision()
{
if (invalue ==0 )
{
//break ;
}
else if (invalue ==1)
{
display = new DisplayPict2("picture name");
portnum = display.getPortNumber();
sendData(portnum) ;
}
}
public void sendData(int i)
{
try
{
output.write(i) ;
}
catch( IOException e)
{
System.out.println("Error writing");
}
}
public void closePorts()
{
try
{
input.close();
output.close();
}
catch( IOException e)
{
System.out.println(" Error closing port");
}
}
public void serialEvent(SerialPortEvent event)
{
if(event.getEventType()==SerialPortEvent.DATA_AVAILABLE);//DSR)//DATA_AVAILABLE)
{
try
{System.out.println("In the serial");
inputport = input.read();
System.out.println("In the serial");
}
catch(IOException e)
{
System.out.println("error with input data");
}
}
}
public static void main(String args[])
{
ReadPorts e = new ReadPorts();
}
}