Question

Here is the code snippet

Here i am initiating an Action Listener

    try {

        port_seleted.addEventListener(this);

    } catch (TooManyListenersException e) {
        System.out.println("too many Listeners!");
    }

    port_seleted.notifyOnDataAvailable(true);

Here the below method is called when i receive the data

public void serialEvent(SerialPortEvent Ack_Rec) {

    boolean first_flash = false;

    if (Ack_Rec.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

        try {

            while (input_data.available() > 0) {
                input_data.read(rec_ack);
            }
        } catch (IOException e) {
            System.out.println("IO Exception in SerialEvent");
        }

I am not receiving the data properly, i.e

if I send some data as " Hello How are you doing today" it is received as "Hello h" "ow" " are you" " doin" "g today"

i.e the Serial event method is being called multiple times, i.e it is exiting the while loop before complete data is read.

If i insert a delay

    try {

            while (input_data.available() > 0) {
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                input_data.read(rec_ack);
            }
        } catch (IOException e) {
            System.out.println("IO Exception in SerialEvent");
        }

as shown above, it is working fine. Please help me how to remove the delay, as it is decreasing the efficiency!

Was it helpful?

Solution

Here goes the solution.

As my project is related to an embedded micro-controller, A little info for the programmers.

Any serial communication protocol cannot transmit all the data at a single go. It depends on the periodicity on which your transmit function is being called.

i.e. it depends in which ms task your function is called is you are using an RTOS.

So if the data is large say 1024 bytes it may not be possible to transmit all data at a single go i.e. at the single call of a function and it may need multiple cycles to complete the job.

As the PC side is much faster than the controller side, the PC has to wait till all data is received before it starts processing the data.

Ok, Now coming to the solution for above problem ,

I know the number of bytes i am expecting in each transaction, lets say 1000 bytes.


// Code snippet

public void serialEvent(SerialPortEvent Ack_Rec) {


        if (Ack_Rec.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

            try {

                while (in.available() > 0) {
                  int totalbytesreceivedinSession = in.read(sessionRead);

                    for (int bytesreceieved = 0; bytesreceieved < totalbytesreceivedinSession; bytesreceieved++) {

                        temporaryPacket[recDataCount++] = sessionRead[bytesreceieved];
                    } 
                    if(recDataCount == 1000){
                      //Process the data 
                      }
            }

Hope this is helpfull,

Cheers!

OTHER TIPS

Read the number of bytes available only based on the count of input_data.available()

int bytesAvailable = 0;
while ((bytesAvailable=input_data.available()) > 0) {


                input_data.read(rec_ack,0,bytesAvailable);
            }
        } catch (IOException e) {
            System.out.println("IO Exception in SerialEvent");
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top