Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7794525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:48:08+00:00 2026-06-01T22:48:08+00:00

i’m working on software for warehouse management (for storing clothes). All items in warehouse

  • 0

i’m working on software for warehouse management (for storing clothes). All items in warehouse have rfid inside, with unique id stored as a String.
I’m trying to make module that will allow users to read rfid from item by antenna and automatically show received ID in one of GUI’s jLabels. Antenna is connected with PC by rs232.
I’ve managed to make class to read data from antenna and it works fine. It opens port to communication, sets properties and reads data when available (through SerialPortEvent.DATA_AVAILABLE event).
But then I’ve encountered problem:
I want instance of that class running in different thread so antenna will be waiting for scan and every time after scan jLabel will be changed according to item’s ID (later on i will be doing more complex operations on this ID connected with my database but now i just want it to be shown somewhere).
I’m beginning my Java adventure and I don’t know how to handle multithreading.
In this case I would like to start scanning from my Netbeans GUI jFrame and while the scanning will be in progress the jFrame should dynamically refresh value of jLabel according to last scanned item.
Scenario:
User presses the button to start scanning, scans some ammount of items, ID of each scanned item goes to jLabel (jReadLabel) for the time between scans, and when scanning is complete user presses button to stop, so app knows when to stop the thread.
I’ve made getter method in my ReadCOM class (getChipID()) but i don’t know how to pass data to jFrame every time when event occured.
Here is what I’ve done so far:

import java.io.*;
import java.util.*;
import javax.comm.*;

public class ReadCOM implements Runnable, SerialPortEventListener {

static CommPortIdentifier portId;
static CommPortIdentifier saveportId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
public static Thread readThread;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
public String defaultPort;
boolean isRunning = true;
private String chip_id="";

public CzytajCOM(CommPortIdentifier portId, String defaultPort) {

    this.defaultPort = defaultPort;
    try {
        serialPort = (SerialPort) portId.open("Magazyn", 2000);
    } catch (PortInUseException e) {
        System.out.println("Connection Error. Port in use.");
    }

    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {
    }

    try {
        serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {
    }
    serialPort.notifyOnDataAvailable(true);
    try {
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {
    }
    readThread = new Thread(this);
    readThread.start();       
}

public void initwritetoport() {

    try {
        outputStream = serialPort.getOutputStream();
    } catch (IOException e) {
    }

    try {
        serialPort.notifyOnOutputEmpty(true);
    } catch (Exception e) {
        System.exit(-1);
    }

}

public void writetoport() {
}

public void run() {

    while (isRunning) {
        try {
            while (isRunning) {
                Thread.sleep(100);
            }
        } catch (Exception e) {
            isRunning = false;
        }
    }
}

public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
            break;

        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;

        case SerialPortEvent.DATA_AVAILABLE:

            Object obj = event.getSource();
            if (obj instanceof javax.comm.SerialPort) {
                serialPort = (SerialPort) obj;
                try {
                    BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(serialPort.getInputStream()));
                    chip_id = bufferedReader.readLine();
                    //System.out.println("Data: "+chip_id);
                    bufferedReader.close();
                } catch (Exception ex) {
                    System.out.println("Reading from device failed!");
                }
            }
            break;
    }
}

public boolean isRunning() {
    return isRunning;
}

public String getChipId() {
    return chip_id;
}

public void setIsRunning(boolean isRunning) {
    this.isRunning = isRunning;
}

}
And in my jFrame file here’s the code of ButtonActionPerformed (the button which starts and stops reading, the part where im lost…):

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
     if(isRead==false) {
          jStatus.setText("Reading in progress...");
          jLabel1.setText("Press the button to stop reading.");
          isRead=true;
          try {
        portId1=CommPortIdentifier.getPortIdentifier("COM4");
        ReadCOM reader=new ReadCOM(portId1, portId1.getName());
        reader.setIsRunning(true); //
        jReadLabel.setText(reader.getChipId());
              }
    catch (Exception ex){
                    System.out.println("Error in button pressed method.");
                   }
     }
               else {

        jStatus.setText("Reading stopped.");
        jLabel1.setText("Press the button to start reading.");
        isRead=false;
    }
}//GEN-LAST:event_jButton1ActionPerformed    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:48:10+00:00Added an answer on June 1, 2026 at 10:48 pm

    1) method setText() is declared as thread safe but works untill current thread isn’t freezed by Thread.sleep(int),

    2) method setText() works wrapped into invokeLater() from Runnable#run() or util.Timer#run(), but your Thread.sleep(int) is outside of those APIs, then potentially can locking events to the EventDispatchThread

    3) you opening CommPort from ActionListener, then Swing GUI is freeze or isn’t unresponsible untill all events from ActionListener ended, nothing could be dispayed in JLabel

    4) you have to move (reading value from CommPort) to the Background task

    • invoke CommPort from Runnable.Thread or from util.Timer (then not required pausing loop by using Thread.sleep())

    but most better and I’d suggest to use

    • invoke from SwingWorker, then you can use Thread.sleep() or util.Timer inside method doInBackground() and output from methods publish() or process() could be invoke events on EDT
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.