• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

connection to relay IA-3088

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic