Waiting for a char received by a serial port (Device::SerialPort)

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default Waiting for a char received by a serial port (Device::SerialPort)

    Hello,

    I just did my first steps in programming perl today and got
    a "nearly" functioning program. What it should do:
    Send a file to /dev/ttyS0 char by char. After each char,
    wait for the echo to come back, then continue.
    Here is what I wrote, but there seems to be a bug in the
    receving/waiting part, it waits endlessly! :-(

    Can anyone help me out here?

    Thanks,

    Olaf

    --------------
    #!/usr/bin/perl

    use Device::SerialPort qw( :PARAM :STAT 0.07);

    $Port = "/dev/ttyS0";
    $file = "rollei.out";

    $device = new Device::SerialPort($Port);
    $device->baudrate(9600) || die ("Couldnt set baudrate");
    $device->parity("none") || die ("Couldnt Set Parity");
    $device->databits(8) || die ("Couldnt Set Databits ");
    $device->stopbits(1) || die ("Couldnt set stopbits");
    $device->write_settings || die ("No Settings");

    open(INFO, $file); # Open for input
    @lines = <INFO>; # Read it into an array
    close(INFO); # Close the file

    foreach $curr_line (@lines) {
    for ($i = 0; $i <= length($curr_line); $i++) {
    $pass=$device->write(substr($curr_line,$i,1));
    $in=1;
    while ($in) {
    $rcv = $device->input;
    if ($rcv ne "") {
    print $rcv;
    $in=0;
    }
    }
    }
    }

    undef $device;

    Olaf Stetzer Guest

  2. Similar Questions and Discussions

    1. flash to interact with a device in the serial port(rs232)
      Somebody to say would know me if is possible the flash to interact with a device in the serial port (rs232)? It is a device of contactless....
    2. Help compiling Device::SerialPort
      I am trying to get Device::SerialPort working on Soalris 10. The build fails during the make stage. What's hapening is that, even though...
    3. Problems with Device-SerialPort
      I'm trying to posr a perl application from FreeBSD to Soalris 10 X86. One of the key requirements of this application is communicating via the...
    4. problem with Device::SerialPort
      Hi. I've been googling all afternoon now but wasn't able to find my problem mentioned someplace, but if for some reason my google-fu was weak,...
    5. Loopback with Device::SerialPort?
      This is cross-posted to .misc and modules, as I'm not certain where it belongs. Feel free to redirect the conversation to whichever group you feel...
  3. #2

    Default Re: Waiting for a char received by a serial port (Device::SerialPort)

    Hi Olaf,
    >What it should do:
    >Send a file to /dev/ttyS0 char by char.
    It looks like it does do that.
    > After each char,
    >wait for the echo to come back, then continue.
    Two issues:

    1. What are you doing to get an echo? Are you connecting via a loopback?
    The module (and the port itself) does not echo characters back to the
    program that writes them.

    2. Unless you set them otherwise, linux ports are commonly configured to
    receive a full line of data at a time. You can arrange to receive
    char-by-char,
    see demo6 for a read-timeout-based way, demo8 for a way that samples
    from a recurring loop. You can also adjust the port using stty - but that
    is
    more difficult to get right than using the time-based alternatives.

    There is a lot of error handling missing from the script - especially from
    critical
    operations like "new" and "open" which really can fail. And no "-w" or "use
    strict"
    either - and BOTH are recommended when working with devices.

    I think you should start by running the examples that come with the
    distribution,
    to see what they do and assure yourself everything is running properly. Then
    try
    modifying demo8 to do what you want (your file write replaces the section with
    the TERM::Readkey call).

    Good luck,

    -bill
    Bbirthisel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139