wood burning stoves*
The moose likes Sockets and Internet Protocols and the fly likes connection to relay IA-3088 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "connection to relay IA-3088" Watch "connection to relay IA-3088" New topic
Author

connection to relay IA-3088

duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connection to relay IA-3088
 
Similar Threads
Problem with jframe, serial communication, dynamic changing value and displaying it
Tried for hours and hours to work this out, data buffer from one class back to my main class?
Two way communcation with the serial port (modem to modem)
socket prog not working
Java Telephone API (JTAPI)