This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connection to relay IA-3088

 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package relay_3088;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.FileDescriptor;
import java.io.InputStream;
import java.io.OutputStream;
public class Communicator {

public Communicator()
{
super();
}

void connect(String portName) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if(portIdentifier.isCurrentlyOwned())
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

if(commPort instanceof SerialPort)
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();

(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}

public static void main(String[] args)
{
try
{
(new Communicator()).connect("COM12");
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
____________________________________________________________________________________________________________________________


package relay_3088;

import java.io.IOException;
import java.io.InputStream;

public class SerialReader implements Runnable{
InputStream in;

public SerialReader(InputStream in)
{
this.in = in;
}

public void run()
{
byte[] buffer = new byte[1024];
int length = -1;

try
{
while((length = this.in.read(buffer)) > -1 )
{
System.out.print(new String(buffer, 0, length));
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
____________________________________________________________________________________________________________


package relay_3088;

import java.io.IOException;
import java.io.OutputStream;

public class SerialWriter implements Runnable {

public OutputStream out;

public SerialWriter(OutputStream out)
{
this.out = out;
}

public void run()
{
try
{
String str = "00000011";
String hexString =Integer.toHexString(Integer.parseInt(str,2));
String command = "!002"+hexString;
out.write(command+ "\n");
int c = 0;
while((c = System.in.read()) > -1)
{
this.out.write(c);
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}

}

i am getting the error in write method in class SerialWriter.... please give some suggestion for it and then for all the program. is it going to run fine without any error and exception. And if not then please provide the appropriate way to solve the problem..
thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic