xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

HI monks,

This time, the problem I meet is about to deal with a old snippet. This snippet receive the reqeuest from com port and answer some data. this snippet written in C works very fine until the modem is broken. Considered transmit speed and expense of buying new modem, I intend to write a perl snippet as below figure:

_____ socket _____ com1 | |________________| |----| | | | |----| ----- ------com2

That is , I circutly connect com1 and com2 in this machine. The new snippet listen some tcp port and forward the data between tcp and com port. The benifit is that I don't modify the old program.

Let's see the code took about 5 mins to me(a big advantage of perl ;))

use IO::Socket; $server = IO::Socket::INET->new(Listen => 5, LocalAddr => 'localhost', LocalPort => 10001, Proto => 'tcp'); open( PORT, "+>COM1" ) or die "Can't open COM1: $!"; binmode(PORT); while ($client = $server->accept()) { while($response = <$client>){ print PORT $response; sleep 1; while(read(PORT, $tem_response,2048)){ print $client $tem_response; } } } close($server);
Fortunatly, I can see the data from com2. Unfortuately, the program can't tell the command from com2 at all! I suspect two points: But I think seeking the monks wisdom before I tried like a chicken with it's head cut off.;)
TIA


I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: communication between socket and serial port
by rpnoble419 (Pilgrim) on Dec 11, 2009 at 06:06 UTC
    I would use the Win32::SerialPort package. I use it for all of my communication with the scanners I support in my application. It makes debugging comm errors very easy and allows me to support multiple systems. Its only limitation is that it can't see comm ports past COM8. As for packing the data, can your old program handle it, if not don't pack....
      Thanks for your reply, I'll have a try.


      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction