4

I have been trying to use the module Win32::SerialPort in perl 5.10 (perhaps it has some issues with 5.14). I would have to issue commands to the serial port and get their output. I have figured out that write will do the earlier part for me. For the output part I have seen implementations like:

$port_obj->write("ATE");  #assuming we give the AT command Echo
$res = $port_obj->input;

If this gave the output of the command, why can't we have the more intuitive like the following(even though $res will then return the no. of bits written) with $res capturing the output of write.

$res = $port_obj->write("ATE");

Additionally, I have also seen some implementations of $port_obj->read() but I am not sure what is the difference between read and the input methods.

Please help.

Shades
  • 37
  • 5
CuriousSid
  • 534
  • 3
  • 11
  • 25

1 Answers1

0

1.) Directly returning received data from a write will not work in all (most?) cases, because of the device connected. What if it does not reply, or does not reply immediately? There are uncounted serial devices out there and you will never know what to expect back.

2.) $PortObj->read is a blocking call, i.e. it waits until the requested number of bytes was received. $PortObj->input is non-blocking, i.e. it returns immediately, telling you how many bytes (if any) were received. The documentation is a little bit obscure on that point.

Cheers.

Ekki
  • 16
  • 1
  • I found out that input is nothing but read in the background and works well if you give sufficient sleep before invoking it. – CuriousSid May 28 '12 at 11:45