Sending and receiving data from serial port
Hi,
I'm working with serial ports.
Which are the methods to send and receive data to/from a serial port?
I tried using:
>byte[] data_in = {23, 54, 68};//ex: data to send
>thePort.getOutputStream().write(data_in);
to sending , and:
>System.out.println(thePort.getInputStream().read());
>System.out.println(thePort.getInputStream().read());
>System.out.println(thePort.getInputStream().read());
to receive and visualize the three data in the array.
How can I do to know how long is the input stream and, for example, make a for cycle to visualize it?
> How can I do to know how long is the input stream and, for example,
> make a for cycle to visualize it?
You can't automagically 'know' how many bytes can be read from that
stream. There either has to be some protocol, e.g. the first byte(s)
are the length of the entire message, or you can set a read timeout:
if a timeout exception is thrown there certainly aren't any more bytes
available from the input stream.
kind regards,
Jos
> You will have a very rough time if you use package javax.comm or RXTX.
Care to elaborate? I've used javax.comm until Sun dropped support for
it; then I used rxtx; both didn't present any problems nor rough times
for me.
> Try googling for parport. It works like a miracle.
Great; parport handles the parallel port. Lots of simple devices just
have a serial RS/232 interface.
kind regards,
Jos