BEst way to write to serial port in this example

E

etantonio

In this class that I use to write to a serial port my problem is that
I want to recall the method sendCommand from the main but I don't know
the best way to arrange this,
I also don't know if it is right to leave empty the run method
Can someone explain how with this code I could write to the serial
port ??

Thanks,

Antonio
www.etantonio.it/en



package it.imt.edusat.vcp;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io_OutputStream;
import java.util.Vector;

public class TestSerialWrite {

public TestSerialWrite()
{
super();
}

public 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;
OutputStream out = serialPort.getOutputStream();

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


public static class SerialWriter implements Runnable
{
OutputStream out;

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

public void run ()
{

}

public void sendCommand(Vector<Integer> v) {
try {
for(int i = 0; i<v.size();i++)
{
int dato = v.get(i);
this.out.write(dato);
}
} catch (Exception e) {
System.exit(0);
}
}
}



public static void main ( String[] args )
{
try
{
TestSerialWrite ps = new TestSerialWrite();
ps.connect("COM9");
Vector<Integer> v = new Vector<Integer>();
v.add(Integer.valueOf(4));
v.add(Integer.valueOf(4));
v.add(Integer.valueOf(4));
ps.????

}
catch ( Exception e )
{
e.printStackTrace();
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top