Using netcat to Provide a Network Endpoint During Development

Written by James McDonald

April 2, 2012

I’m in the process of creating a Perl script that sends configuration commands to a SATO printer via a Moxa networked serial port.

My problem with the development process is the Moxa and SATO Printer is in a Tin Shed that climbs to 35° C plus. It’s not much fun sitting with a laptop in sweltering heat.

So noodling away at home I’ve found a great workaround which is to use netcat to emulate the Moxa networked serial port. Using netcat I can send all the data to a host and port of my choosing and get it all functioning as it should before finally retargetting it to point to the Moxa networked serial port.

Following is the ncat command to run on Linux/Cygwin host and a sample Perl script that might when I’m finished become part of a web accessible CGI script to control the printer.

ncat -l 10.11.12.13 2000 -k

#!/usr/bin/perl -w

use Net::Telnet;
use strict;

my $obj = sato->new();

$obj->{barcode} = '12345678901234';
$obj->{description} = "A TEST DESCRIPTION";


$obj->send_print($obj);

package sato;

sub send_print {
	
	my $self = shift;
	my $t = Net::Telnet->new( Timeout => 10,
						Port => 2000 
						);

	$t->open('10.11.12.13');

	my $esc = "\e"; # escape command

	$t->telnetmode;
	$t->put("Before " . $esc . "H0800 $self->{barcode} $self->{description} put from script\n"); # no field sep \n6
		
}

sub new {
	my $class = shift;
	my $self  = bless { @_ }, $class;
	$self->__set_properties();
	return $self;
}

sub __set_properties {
	
		my $self = shift;
		$self->{label_width} = "100";
		$self->{label_height} = "50";
		$self->{font} = "XL1";
		$self->{barcode} = '01234567891234' unless $self->{barcode};
		$self->{description} = 'A TWENTY FOUR CHAR DESC4' unless $self->{description};
	
}


1 Comment

Submit a Comment

Your email address will not be published. Required fields are marked *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...