Author Topic: java with plc  (Read 9141 times)

sun919

  • Newbie
  • Posts: 3
  • I love YaBB 1G - SP1!
    • View Profile
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();
      }
}
« Last Edit: December 31, 1969, 04:00:00 PM by 1076562000 »

support

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3174
    • View Profile
    • Internet Programmable PLCs
Re: java with plc
« Reply #1 on: March 13, 2002, 06:17:41 AM »
Since this has nothing to do with application support relating to our PLCs, you should re-post this message at the General Discussion Forum. A better place to post info regarding pure Javax.comm related topic is at www.javasoft.com.
« Last Edit: December 31, 1969, 04:00:00 PM by 1076562000 »
Email: support@triplc.com
Tel: 1-877-TRI-PLCS