serial port reading

W

Willie

Hi, my friends,

I am working on a device connecting to the serial port. I need to read
data from the device. I used the SimpleRead.java from SUN's website.
http://java.sun.com/developer/releases/javacomm/SimpleRead.java

The port I used is COM1. But it didn't give me what I want. The data
was stored in the log area of the device. The previous version for the
application was in C++ and worked well. They use
ReadFile(hComm,s,maxtoread,&read,0) to retrieve the data from the
device.

Does the inputstream not fit the application or I did other things
wrong? Any help is good. Thank you in advance.

Willie
 
R

Roedy Green

But it didn't give me what I want.

what happened?

Did you get garbled data, nothing at all, valid data but not what you
expected?

There are so many things to configure before a serial port will work
correctly:
baud rate, character width, stop bits, parity, flow control.
 
J

jan V

Willie,

It seems you need to learn how to tackle debugging in a systematic way.

You've connected your device to your Java app via a cable and a host for
your Java app, OK?

Is any data flowing over that cable? If not, forget the program, your
problem is probably cabling.

What's the log area of the device (we really can't read minds in this
newsgroup, you need to be in alt.mystic for that...) ?

Have you checked that your very first transmission to the device is
bit-for-bit what it needs to be? Your device isn't going to answer if you
send it junk...

Break the problem down. Tackle each sub-problem one at a time.
 
W

Willie

I used serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE) to set the parameters
and
....
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
int r=0;
try {
while
((r=inputStream.read(readBuffer))!=-1){//(inputStream.available() > 0)
{
int numBytes = inputStream.read(readBuffer);
System.out.println(new String(readBuffer, 0, r));
}
....

to print the output. The output repeated like this :
(Q )
(Q )
(Q )
....
I don't know what's that.

My expected output is a string of digital data. Thanks,

Willie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top