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 2000 -k
#!/usr/bin/perl -w

use Net::Telnet;
use strict;

my $obj = sato->new();

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


package sato;

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


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

	$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;
	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.

You May Also Like…

PHP Iterators

Just came across a Youtube talk "Iterators in PHP" by Jake Smith published in 2014 that steps through the many...

PHP array_map Multiple Arrays

array_map can take multiple arrays. I like how it starts mapping through them starting at the first element of each...