Skip to content

Commit

Permalink
Vast upgrades to the serial interface.
Browse files Browse the repository at this point in the history
Lots of functionalization.
Again, found laying around in backups.  No clue if it works.
  • Loading branch information
Mark Jerde committed Jul 9, 2011
1 parent 9f6db29 commit 2f0238b
Showing 1 changed file with 77 additions and 22 deletions.
99 changes: 77 additions & 22 deletions rxv2400server.pl
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@
my $warning = "";
my $error = "";

$lastCtrl = 0;

sub setupSerialPort
{
$PortObj = new Win32::SerialPort ($PortName)
Expand All @@ -263,23 +265,56 @@ sub setupSerialPort
}
}

sub sendInit
sub rs232_read
{
print "Sending undocumented init...\n";
$PortObj->write("0000020100");
my $length = $_[0];
my $count_in = 0;
my $string_in = "";
my $timeout = 0;

print "read $length\n";
($count_in, $string_in) = $PortObj->read($length);

if ( ($count_in < $length) && ($timeout < 5) )
{
$timeout++;
sleep(1);

print "read $length - $count_in t$timeout\n";
($ncount_in, $nstring_in) = $PortObj->read($length-$count_in);
$count_in += $ncount_in;
$string_in .= $nstring_in;
}

return ($count_in, $string_in);
}

sub rs232_flush
{
print "Flushing rs232 read buffer.\n";
($count_in, $string_in) = $PortObj->read(275);
print "Got $count_in bytes back.\n";
$string_in = "";
if ( 275 == $count_in ) { rs232_flush(); }
}

sub sendInit
{
print "Sending undocumented init...\n";
$PortObj->write("0000020100");
rs232_flush();
print "\n";
}

$dcnfail = 0;
sub sendReady # getConfiguration
{
rs232_flush();
print "Sending Ready command...\n";
$PortObj->write("000");
($count_in, $response) = $PortObj->read(512);
print "Got $count_in bytes, reading Configuration...\n";

($count_in, $response) = rs232_read(1);
print "Got $count_in bytes, reading DCn...\n";
if ( 0 == $count_in )
{
$response = "N";
Expand Down Expand Up @@ -315,6 +350,9 @@ sub sendReady # getConfiguration
}
}

($count_in, $response) = rs232_read(8);
print "Got $count_in bytes, reading Configuration...\n";
if ( $count_in < 8 ) { print "Failed to read Configuration. ($count_in)\n" and exit; }
print " Received Configuration...\n";
# 5B Model ID
$yamaha{'ModelID'} = substr($response,0,5);
Expand All @@ -338,10 +376,11 @@ sub sendReady # getConfiguration
$dcnfail++;
return sendReady();
} else {
while ( $dataLength > ($count_in - 12) )
($count_in, $response) = rs232_read($dataLength);
while ( $dataLength > $count_in )
{
print "warning: Ready response missing ".($dataLength - ($count_in - 12))." bytes.\n";
($ncount_in, $nresponse) = $PortObj->read(512);
($ncount_in, $nresponse) = rs232_read($dataLength-$count_in);
$count_in += $ncount_in;
$response .= $nresponse;
if ( $ncount_in == 0 )
Expand All @@ -353,6 +392,8 @@ sub sendReady # getConfiguration
}
}
}
print "Got $count_in bytes, reading Configuration Data...\n";
# Data 0 through 6 are "Don't care" per spec
$yamaha{'System'} = substr($response,7,1);
$yamaha{'Power'} = substr($response,8,1);
if ( $yamaha{'System'} eq "0" )
Expand Down Expand Up @@ -401,6 +442,8 @@ sub sendReady # getConfiguration
$dataLength--;
}
print "\n";
($count_in, $response) = rs232_read(3);
print "Got $count_in bytes, reading Checksum + ETX...\n";
# 2B checksum
$checksum = substr($response,0,2);
print " checksum: 0x".$checksum;
Expand All @@ -422,6 +465,22 @@ sub sendReady # getConfiguration
$dcnfail = 0;
}

sub readReport
{
print "Reading report.\n";
($count_in, $string_in) = rs232_read(8);
print "Got $count_in bytes back.\n";
#$string_in = "";
print "\n";
$string_in =~ s/(.)/$1 /g;
$string_in =~ s/$STX/STX/g;
$string_in =~ s/$ETX/ETX/g;
$retMsg = "OK - Received $count_in byte response.";
$retMsg = $retMsg."\n - $string_in";
return $retMsg;
return "OK - Received $count_in byte response.";
}

sub sendControl # getReport
{
local($inStr) = @_;
Expand Down Expand Up @@ -488,18 +547,15 @@ sub sendControl # getReport
print "send $packet";

print "Sending Control $inStr...\n";
$curCtrl = time;
if ( $curCtrl > ($lastCtrl + 10) )
{
sendReady();
}
$PortObj->write($packet);
($count_in, $string_in) = $PortObj->read(275);
print "Got $count_in bytes back.\n";
#$string_in = "";
print "\n";
$string_in =~ s/(.)/$1 /g;
$string_in =~ s/$STX/STX/g;
$string_in =~ s/$ETX/ETX/g;
$retMsg = "OK - Received $count_in byte response.";
$retMsg = $retMsg."\n - $string_in";
return $retMsg;
return "OK - Received $count_in byte response.";
$lastCtrl = time;

return readReport();
}

sub writeMacroFile
Expand Down Expand Up @@ -559,7 +615,6 @@ sub decode
s/^Control\s*//;
$command = $_;
#sendInit();
sendReady();
$status = sendControl($command);
print $status."\n";
print $client $status."\n";
Expand Down Expand Up @@ -597,7 +652,7 @@ sub decode
$string_in = "";
while ( 20 > $rcvd )
{
($count_in, $string_i) = $PortObj->read(512);
($count_in, $string_i) = rs232_read(512);
$string_in .= $string_i;
if ( 0 != $count_in )
{
Expand Down Expand Up @@ -787,7 +842,7 @@ sub decode
Reuse => 1);

die "sorry, couldn't setup server" unless $server;
print "[LINX-Server waiting for commands on port $PORT]\n";
print "[RXV-Server 1.3.1.2 waiting for commands on port $PORT]\n";

#$PortObj->write($STX."07AED".$ETX.$STX."07EBA".$ETX.$STX."07A1B".$ETX.$STX."07A1B".$ETX.$STX."07A1B".$ETX.$STX."07A1B".$ETX);

Expand All @@ -796,7 +851,7 @@ sub decode
$client->autoflush(1);

# Welcome message
print $client "rxv2400server connection open.\n";
print $client "rxv2400server 1.3.1.2 connection open.\n";
print $client "Accepting commands for $yamaha{'ModelID'}";
if ( $yamaha{'ModelID'} eq "R0161" ) { print $client " (RX-V2400)"; }
print $client " device.\n";
Expand Down

0 comments on commit 2f0238b

Please sign in to comment.