java


displaying the output from a serial port in JTextField


This is a part of a sample code from a RXTX serial communication program.
I would like to know on how to extract the string variable that I have created to store the output so that I could use it in another class. The output is showing on the console but I want to display it in a JTextfield and I'm unable to extract it. I have done the GUI part.
First I will show the main part:
public class MainSerialGui {
public static void main(String[] args) {
SerialGui vimal = new SerialGui ();
vimal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
vimal.setSize(250, 200);
vimal.setVisible(true);
}
}
This is the GUI part
public class SerialGui extends JFrame { //inherit all the stuff from JFrame and let us create a window
private JTextField item1;
private JButton readButton;
public SerialGui (){
setLayout(new FlowLayout());
item1 = new JTextField("Display Output");
add(item1);
readButton = new JButton("Read Data");
add(readButton);
thehandler handler = new thehandler();
readButton.addActionListener(handler);
}
private class thehandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if(event.getSource() == readButton){
String portName = "COM4";
try{
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(57600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE); // Setting port parameters
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
SerialReader comm = new SerialReader(null);
item1.setText(comm.str);
}
}
}
catch (Exception e){
e.printStackTrace();
System.out.println("Only serial ports please");
}
}
}
}
}
This is a separate class to read the data from the comm port.
public class SerialReader implements Runnable {
InputStream in;
public String str;
public SerialReader ( InputStream in )
{
this.in = in;
}
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 )
{
str = new String(buffer,0,len); // I would like to take this String
System.out.print(str); // and use it to display in a
} // Jtextfield
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
This is how I initially extract it to display in the textfield however the results shows null. I declare str as public string so that it is visible in the GUI class. The data that being read can be shown in the console but no changes in the textfield. Is it because its taking the value even before the output is stored in str or because of the 'null' argument?
I was unable to replace it with any other argument.
SerialReader comm = new SerialReader(null);
item1.setText(comm.str);
http://rxtx.qbang.org/wiki/index.php/Two_way_communcation_with_the_serial_port
You are creating TWO SerialReader instances, and starting one of them while using output of the other.
Try replacing,
(new Thread(new SerialReader(in))).start();
SerialReader comm = new SerialReader(null);
item1.setText(comm.str);
with
SerialReader comm = new SerialReader(in);
Thread commThread = new Thread(comm);
commThread.start();
commThread.join();
item1.setText(comm.str);
Hope this helps.

Related Links

Parse changing XML using XStream
opencv_ffmpeg2413_64.dll Linux build for OpenCV
Integrate Spring Boot app into existing project
Does Freemarker 2.3.23 handle varargs properly?
How to trace a syntax error while parsing a grammar?
When AWS KCL processRecords is failed, how to “mark” that the records should be reprocessed?
Searching Java ArrayList and returning element location - indexOf() not working.
How to handle scheduled task in dropwizard in multiserver environment
How can I go to second activity but send a data to another activity?
LWJGL 3 Javadoc
How to connect to LDAPS using Apache LDAP Client?
Gradle unit tests hang when a SecurityException is encountered
An code example of serialization JSON to JSON-LD in Java?
Accessing variable from other class
Completion Listener for setValue() operation is never triggered?
Replacing values in an ArrayList of custom objects

Categories

HOME
compiler-construction
vbscript
isabelle
c#-4.0
react-virtualized
relayjs
kalman-filter
rubygems
webpack-2
fingerprint
ezpublish
append
synchronization
awesome-wm
communication
midi
nstableview
etl
postgres-xl
visual-studio-2005
percona
serilog
pugjs
connection-string
one-hot-encoding
errorlevel
graphicsmagick
pass-by-reference
tibco-mdm
format-specifiers
spark-jobserver
ejabberd-module
avcapturesession
android-kernel
stacked
web-mining
abstract-class
code-search-engine
particles.js
sql-server-agent
total-commander
gpx
scorm
chain-builder
blogengine.net
tizen-native-app
darcs
nativeapplication
magma
mako
dwscript
redundancy
mathematica-frontend
paypal-express
simplewebrtc
react-native-listview
pagedlist
yaws
independentsoft
freedesktop.org
msys
c++03
kineticjs
rake-task
tablelayout
file-copying
fpml
codeigniter-routing
bitcoinj
castle
ceil
funcunit
valueconverter
comexception
marmalade
jboss-weld
hyprlinkr
sabredav
google-email-migration
pysimplesoap
ticoredatasync
regsvr32
removeclass
pendrive
hgsubversion
spyware
deobfuscation
file-comparison
iphone-web-app
visitor-statistic
dmx512
gamma
routedevent
text-coloring
defensive-programming
rtml

Resources

Encrypt Message