• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1134
  • Last Modified:

Perl serial communication ACK response

Guys,
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.

0
GerryArmstrong
Asked:
GerryArmstrong
  • 3
1 Solution
 
ChristoferDutzCommented:
As far as I know, ACK and NAK are simple ASCII Cahracters, so try just sending these:

ASCII Table and their encoding.
http://www.december.com/html/spec/ascii.html
0
 
GerryArmstrongAuthor Commented:
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.
#!/usr/bin/perl
 
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"; 
 
$msg="006";
$ob->send($msg);
print "\n\n $msg sent.\n\n";
 
sleep 1; #delay for reply to be sent
 
$result=$ob->input;
print "$result";
 
$ob->close;
undef $ob;

Open in new window

0
 
ChristoferDutzCommented:
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.
0
 
ChristoferDutzCommented:
Hey common! I did answer and I asked you some questions ... questions you didn't respond to.
0
 
Adam314Commented:
This:
    $msg="006";
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?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now