Wondering how to use the USB GPIO module as a serial port in Perl?
Well, here is a sample script that opens a serial port and sends and receives messages. The script itself is quite simple, but it could be modified\extended for more complicated applications. The script can be used in either Windows or Linux. Just make sure that you have the correct Perl modules installed (and uncomment\comment the correct lines in the script ;-) ). Also... when using the script to test a USB GPIO model, make sure that the TXD and RXD pins are shorted together in a "loopback". Note the TXD and RXD pins are screwterm inputs 1 and 4 respectively.
ENJOY!
#!/usr/bin/perl -w
#use Getopt::Std;
# For Windows uncomment the following 3 lines
#use Win32::SerialPort;
#my $port_n ="COM0";
#my $port_obj = new Win32::SerialPort ($port_n) || die " Can't open $port_n $!";
# For Linux uncomment the following 3 lines
use Device::SerialPort;
my $port_n ="/dev/ttyUSB0";
my $port_obj = new Device::SerialPort ($port_n) || die " Can't open $port_n $!";
#Serial port Parameters
$baud_rate=115200;
$port_obj->databits(8);
$port_obj->handshake("none");
$port_obj->baudrate($baud_rate);
$port_obj->parity("none");
$port_obj->stopbits(1);
$port_obj->buffers(4096,4096);
# This is the essential step
$port_obj->write_settings || die "Cannot write settings";
#Write a Welcome Message
$port_obj->write("\n=========================================\n");
$port_obj->write("| Five Man Conspiracy |");
$port_obj->write("\n-----------------------------------------\n");
$port_obj->write("| USB GPIO |\n");
$port_obj->write("| Serial Port Test Perl Script |");
$port_obj->write("\n=========================================\n");
$port_obj->write("| Type and message and press the ENTER |\n");
$port_obj->write("| key to send. To quit press ^C. |");
$port_obj->write("\n=========================================\n\n");
#sleep for 50 milliseconds
select(undef,undef,undef,0.05);
# Read the Com Port input buffer (4096 chars max)
$data_read=$port_obj->read(4096);
print "$data_read";
while(1){
$key = ;
$port_obj->write($key);
#sleep for 50 milliseconds
select(undef,undef,undef,0.05);
# Read the Com Port input buffer (4096 chars max)
$data_read=$port_obj->read(4096);
#if($data_read){print "\nYou said... $data_read\n";}
print "\nRX message --> $data_read\n";
}