Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.35k stars 287 forks source link

serial bit to convert original format #238

Closed jrajauriya closed 4 years ago

jrajauriya commented 5 years ago

we want to convert serial bit data to original format. first we getting the garbage data after that i'm able to convert hex code.

        ```
     public void serialEvent(SerialPortEvent event) {
    try {
        while (true) {
           while (comPort.bytesAvailable() == 0)
            Thread.sleep(20);

             byte[] readBuffer = new byte[comPort.bytesAvailable()];
             int numRead = comPort.readBytes(readBuffer, readBuffer.length);
             String s1 = new String(readBuffer);
             char c1[] = s1.toCharArray();                                      
                 for(char ch : c1)
                  {
                     System.out.print((int)ch + ", ");//convert to hex code
                   }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            comPort.closePort();
            System.out.println("Done.");
        }

output:


3,3,3,66,10,34,6,d,3,3,6,34,6,3e,4,6,6,d,3f,3,3,40,3,3f,3,40,3,3,

we want to expected output:
<SOH>BD<STX>SNO<HT>1<LF>DATE<HT>256<LF>4<HT>17<HT><HT>27<HT><HT>37<HT>
hedgecrw commented 5 years ago

Sorry, I'm not understanding the question. Are you asking how to convert bytes into their ASCII representation?

jrajauriya commented 5 years ago

thanks for reply. we already convert to ASCII. but how to getting following expected output from byte or ASCII or HEX: <SOH>BD<STX>SNO<HT>1<LF>DATE<HT>256<LF>4<HT>17<HT><HT>27<HT><HT>37<HT>

hedgecrw commented 5 years ago

1) Still not understanding the question. Are you saying that the bytes you receive are not the bytes you are expecting to receive (which would be "1,66,68,2,83,78,79,9,49,10,68,65,84,69,9,50,53,54,10,52,9,49,55,9,9,50,55,9,9,51,55,9" based on turning your expected string into a byte array)? 2) If that's the case, can you post an entire minimal working example showing how you have jSerialComm/your serial port set up? 3) Can you also post the output of directly printing the bytes received from jSerialComm instead of performing your conversions...in other words, directly after the line int numRead = comPort.readBytes(readBuffer, readBuffer.length);, add:

System.out.print("Received " + numRead + " bytes\nBytes: ");
for (byte b : readBuffer)
   System.out.print((int)b + ",");
jrajauriya commented 5 years ago

following jSerialComm complete code

public static void main(String[] args) {
        SerialPort comPort = SerialPort.getCommPort("COM1");
        comPort.openPort();     
        comPort.setComPortParameters(9600, 8, 1, SerialPort.NO_PARITY);
        reading_bytes(comPort);
    }

public static void reading_bytes(SerialPort comPort){       
        comPort.addDataListener(new SerialPortDataListener() {
            @Override
            public int getListeningEvents() {
                return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
            }

            @Override
            public void serialEvent(SerialPortEvent event) {
                if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
                    return;
                byte[] readBuffer = new byte[comPort.bytesAvailable()];
                int numRead = comPort.readBytes(readBuffer, readBuffer.length);
                System.out.println("Read " + numRead + " bytes.");
                for (byte b : readBuffer)
                    System.out.print((int) b + ",");
                System.out.println();
            }
        });     
    }

and we got following output: Read 14 bytes. 9,25,35,6,-20,73,-19,-64,33,62,108,-8,116,-2, Read 0 bytes.

Read 14 bytes. 58,124,28,25,14,-58,24,62,-26,116,-96,7,7,-35, Read 0 bytes.

Read 14 bytes. 3,6,15,54,59,-116,25,15,15,29,-125,6,-104,-16, Read 0 bytes.

Read 14 bytes. -48,-74,-60,100,-122,6,6,6,14,14,14,6,6,6, Read 0 bytes.

Read 14 bytes. 13,7,15,7,15,7,7,6,6,13,3,-76,-125,-125, Read 0 bytes.

Read 14 bytes. 6,-60,6,7,-49,6,6,7,13,-121,70,110,24,59, Read 0 bytes.

Read 14 bytes. 6,71,-125,6,-51,3,3,3,7,15,26,14,7,6, Read 0 bytes.

Read 14 bytes. 3,64,3,3,3,6,13,3,3,6,13,3,3,6, Read 0 bytes. .....

now how can we convert to this.

jrajauriya commented 5 years ago

@hedgecrw We shared complete code could you give me best solutions.

hedgecrw commented 5 years ago

You keep saying "convert" but I don't know understand what you're trying to convert. From what I gather so far, you are sending the following bytes (1 66 68 2 83 78 79 9 49 10 68 65 84 69 9 50 53 54 10 52 9 49 55 9 9 50 55 9 9 51 55 9) from some device and receiving the incorrect bytes on another device. My guess is that the serial port parameters that you are specifying in comPort.setComPortParameters() do not match the actual/expected port parameters being used by your transmitting device.

The other issue is that I'm not sure how this program is functioning at all if what you posted is actually the entire code base, since the main(String[] args) function sets up the serial port and then immediately returns which will cause all the variables and ports you set up to go out of scope (and your program should theoretically terminate at that point).

jrajauriya commented 5 years ago

@hedgecrw Let me explain you in details. I want to lab machine interfacing with my existing software. I am using RS232 female to female connector for communication machine to pc. and according to machine manual we set correct parameters in comPort.setComPortParameters(9600, 8, 1, SerialPort.NO_PARITY). When ever machine send the data we got bytes like 9,25,35,6,-20,73,-19,-64,33,62,108,-8,116,-2 up to 300 lines. now these bytes we already tried to convert all format likes: String, Hex, Ascii, Decimal but we are not able to got following expected output according to machine manual:

O|1|LID668||^WBC^^\^RBC^^|R||20030516161149||||A||The
patient is abc.|||Dr. xyz||sus.blasts^?\var.
lymph^++||100^\^|1.00^2.0^3.0^4.0^5.0^6.0^7.0^8.0^^^^
^^8.0^10.0^11.0^12.0^13.0^14.0^15.0^^^^^||||Q<CR>
hedgecrw commented 4 years ago

Were you ever able to resolve this issue? I'm closing the bug report due to it being unlikely to be a library issue since no other similar bug reports have been opened nor am I able to reproduce.