Perl serial communication ACK response

Posted on 2009-04-16
Last Modified: 2012-05-06
I am trying to use Perl to provide serial communication between two applications on PCs. I have never attempted serial comms programming before in perl, my experience is mostly TCP stream reading and database access.
What has me beat at the moment is how to handle the ACK/NAK signalling responses. Basically what I need to do is to have the perl script send an ASCII ACK to the other PC via serial, wait for a response which should alos be ASCII ACK for ok or NAK for no.
If the response is ACK then it will send another stream of data and await another ACK/NAK response.
Any help hopefully with an example code as to what Ineed to do to handle these responses is greatly appreciated.

Question by:GerryArmstrong
    LVL 20

    Expert Comment

    As far as I know, ACK and NAK are simple ASCII Cahracters, so try just sending these:

    ASCII Table and their encoding.

    Author Comment

    Ok, guess that makes sense. This is the code I have so far, I know it is probably full of errors and not very resilient but do you think it will work for the purpose of testing? I assume that I should receive the NAK response and have the ASCII code 025 printed to screen.
    use Device::SerialPort;
    $Port = "/dev/ttyS0";
    $ob = Device::SerialPort->new($PORT) || die "Can't Open $PORT: $!";
    $ob->baudrate(9600) || die "failed setting baudrate";
    $ob->parity("none") || die "failed setting parity";
    $ob->databits(Cool || die "failed setting databits";
    $ob->stty_icrnl(1) || die "failed setting convert cr to new line";
    $ob->handshake("none") || die "failed setting handshake";
    $ob->write_settings || die "no settings"; 
    print "\n\n $msg sent.\n\n";
    sleep 1; #delay for reply to be sent
    print "$result";
    undef $ob;

    Open in new window

    LVL 20

    Expert Comment

    I have to admit that I am not very familiar with perl itself, but I have done quite some implementing serial port communication with serial hardware. So in general I think your code should be correct. I don't know If you have to manually flush data to the socket if "send" simply writes to a buffer.

    Is the data you are sending being recieved at the other end? Serial communication is sort of tricky, as you have to set both to the same speed, handshake, parity etc.
    LVL 20

    Accepted Solution

    Hey common! I did answer and I asked you some questions ... questions you didn't respond to.
    LVL 39

    Expert Comment

    is not ascii code 006, the ACK character.  It is 3 characters, zero, zero, six.  If you want the ACK character, you need:
        $msg = "\x06";

    Other than that, what trouble are you having?

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now