views:

21

answers:

1

Hi All,

I have a serial device connected to my computer at com port. I want to send Hex data to this device, what is the best approach to send data to the device.

For example I have this data-

String hexStr = "02303033434333d3037313131303131323639393130333131303139033f";

What I am doing right now is-

     byte[] unsignedByte = HexUtil.convertToBytes(hexStr);
serialPort.getOutputStream().write(unsignedByte);

The problem I am facing is - Serial device is not responding ? When i m sending the same hex string via a software, terminal is responding. Am I doing something wrong?

This is some part of the code-

                    mOutputToPort.write(unsignedByte);          
        logger.info("Byte Sent");
        mOutputToPort.flush();
        logger.info("Waiting for response");
        Thread.sleep(10000);
        byte mBytesIn [] = new byte[2000];          
        int num = mInputFromPort.read(mBytesIn);
        System.out.println("Number of bytes read -"+ num);

        for(byte b : mBytesIn){
            System.out.print(b);
            System.out.print(" ");
        }
        System.out.println();       

Hi Guys, I finally found out by myself how to do it properly. I was not sending data in correct format. I found out while i monitor the serial data exchange using other software.

the best way to send data to serial device is - ASCII DATA --> HEX STRING --> UNSIGNED BYTE

A: 

You may want to flush() the outputstream after write operations. Try if this helps:

byte[] unsignedByte = HexUtil.convertToBytes(hexStr);
serialPort.getOutputStream().write(unsignedByte);
serialPort.getOutputStream().flush();   // <- new line added here
Andreas_D
Hi Guys, I finally found out by myself how to do it properly.I was not sending data in correct format. I found out while i monitor the serial data exchange using other software.the best way to send data to serial device is -ASCII DATA --> HEX STRING --> UNSIGNED BYTE