Jump to content
Sign in to follow this  
jerzykiller

RS-232 Perl

Recommended Posts

Hey,

I wonder if anyone of you have tried to generate voltage on one pin in rs-232 port in Perl.

 

This is what I am trying to do, and I don't know which module should I use. There are nothing about voltage in modules at CPAN. Is this even possible?

I would appreciate any help.

 

Regards.

Share this post


Link to post
Share on other sites

Hehe, I actually did it some time ago, for Windows :P

 

Here is the code if someone would want it :P

 

#!C:\Perl\bin\perl.exe
use Win32API::CommPort;
use strict;
use Tk;
use Tk::Checkbutton;
use Tk::Optionmenu;

my $ontopvar = 0;
my $ivar = 0;
my $serial;
my $opened;
my $onoff = 0;
my $hide = 0;


my $mw = MainWindow->new;
$mw->resizable(0,0);
$mw->title('RS-232 Controller');
my $ptt = $mw->Button(
-text    => 'PTT',
-command => sub {switch($serial)},)->pack(-side => 'left');

$ptt->configure(-bg => 'green');
$ptt->configure(-activebackground => 'green');
my $ontopbutton = $mw->Button(
-text	 => 'Always on top',
-command => sub {ontop()},)->pack(-side => 'left');
my $ibut = $mw->Checkbutton(
   -text=>'Invert',
   -offvalue=> 0,
   -onvalue=> 1,
   -variable=>\$ivar,
)->pack(-side => 'left');
my $opt = $mw->Optionmenu(
-textvariable => \$serial,
-options => [ ["COM1", "COM1" ],
			  ["COM2", "COM2" ],
			]
)->pack(-side => 'left');

my $closebutton = $mw->Button(
-text	 => 'Exit',
-command => sub {exit},)->pack(-side => 'right');
my $hidebut = $mw->Button(
-text	 => 'Hide Decoration',
-command => sub {hidedec()})->pack(-side => 'left');
MainLoop;

sub switch {
my ($tmp) = shift;
if ($onoff == 0) {
	if ($ivar == 0) {
		$serial = new Win32API::CommPort($tmp) || die "Can't open $tmp\n";
		$serial->rts_active(1);
		$serial->dtr_active(0);
	}
	elsif ($ivar == 1) {
		$serial = new Win32API::CommPort($tmp) || die "Can't open $tmp\n";
		$serial->rts_active(0);
		$serial->dtr_active(1);
	}
	$ptt->configure(-bg => 'red');
	$ptt->configure(-activebackground => 'red');
	$ibut->configure(-state => 'disabled');
	$opened = $tmp;
	$onoff = 1;

}
elsif ($onoff == 1) {
	$serial->rts_active(0);
	$serial->dtr_active(0);
	$serial->close;
	$ptt->configure(-bg => 'green');
	$ptt->configure(-activebackground => 'green');
	$ibut->configure(-state => 'active');
	$serial = $opened;
	$onoff = 0;

}
}

sub ontop {
if ($ontopvar == 0) {
	$mw->attributes(-topmost => 1);
	$ontopbutton->configure(-bg => 'green');
	$ontopbutton->configure(-activebackground => 'green');
	$ontopvar = 1;
}
elsif ($ontopvar == 1) {
	$mw->attributes(-topmost => 0);
	$ontopbutton->configure(-bg => 'SystemButtonFace');
	$ontopbutton->configure(-activebackground => 'SystemButtonFace');
	$ontopvar = 0;
}
}
sub hidedec {
if ($hide == 0) {
	$mw->overrideredirect(1);
	$hidebut->configure(-bg => 'green');
	$hidebut->configure(-activebackground => 'green');
	$closebutton->packForget();
	$ontopbutton->packForget();
	$opt->packForget();
	$ibut->packForget();
	$hide = 1;
}
elsif ($hide == 1) {
	$mw->overrideredirect(0);
	$hidebut->configure(-bg => 'SystemButtonFace');
	$hidebut->configure(-activebackground => 'SystemButtonFace');
	$hide = 0;
	$hidebut->packForget();
	$ontopbutton->pack(-side => 'left');
	$ibut->pack(-side => 'left');
	$opt->pack(-side => 'left');
	$hidebut->pack(-side => 'left');
	$closebutton->pack(-side => 'right');
}
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...