in reply to Re: How to get right data from COM1 using Win32::SerialPort
in thread How to get right data from COM1 using Win32::SerialPort

Thanks for your attention. My code is as follow:
#initial SerialPort to $configurationFileName; $portCom1 = tie (*FH, 'Win32::SerialPort', $configurationFileName) || +die "Can't tie: $^E\n";
I have tried binmode as "binmode (FH);" But error: Can't locate object method "BINMODE" via package "Win32::SerialPort"...
br> BTW, "Ports are opened for binary transfers. A separate binmode is not needed." (From http://members.aol.com/Bbirthisel/SerialPort.html or CPAN)

Then what should I do?

Replies are listed 'Best First'.
Re^3: How to get right data from COM1 using Win32::SerialPort
by Chenny (Initiate) on Mar 22, 2006 at 01:30 UTC
    Any other suggestion? Thanks!

      Sorry, no. The symptoms you describe look, feel, smell and taste like the classic win32 textmode conversion. If the module is ensuring that none of Perl's IO layers, nor the C runtime layers are exercising an influence upon the datastream, then I cannot see where the problem would lie.

      My instinct says that the OS, CRT or PerlIO layers *must* be getting involved somewhere in the chain and treating the datastream as text instead of binary. Not that it isn't possible for a different explanation, but the obvious one is usually right.

      A few thoughts.

      • Have you tried contacting the author/maintainer?
      • Have you tried using the object interface rather than the tied interface?
      • Is the "serial port" in question a real rs232/422 port, or a pcmcia or usb or firewire simulation of a serialport?
      • What ends up in the file if you do
        copy COM1 /b data.in /b
        and then arrange for the device to send it's binary data?

      Basically, you need to track down where the datastream is being modified. If this is a real serial port, then I'd try attaching a Serial Port analyser to the connection. I used to have a blue cable with a built-in patch box and LEDs that could latch on byte at a time. It's probably in the loft somewhere, but I cannot remember the brand name.

      Alternatively, you could try monitoring the port with on of the many free software analysers that are available on the net. Once you are convinced that the same data that is leaving the device is being received, then you move up to the module itself.

      Try enabling debug on the module. If that shows the data being received from the device driver correctly, then make a copy of the module for backup and then go in and add some trace statements and see if you can find where the modifications are being made. If the problem is in the DD, you're faced with trying contact the supplier and find out if there is a later version, or some IOctl that you can set to disable it. And so on.

      Sorry that's not much help. I last used the module on my old laptop under NT4. It seems to have moved on quite a bit since.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Thanks for your answer!

        Yes, I have sent email to the author: wcbirthisel@alum.mit.edu . But I still have no answer. I have tried ($countIn, $binData) = $portCom1->read (1);
        But get the same result!

        In fact, I can get right txt data from COM using Win32::SerialPort. And I also can sent right txt data to COM. Such as $ComLine = <FH>;But when I get a bin data block(such as 15K or more), I get 0D0A (not initial 0A)!

        I have used "SUDT SerialTrace" to trace the serial Port. It also shows 0D0A (not initial 0A)!

        Then, I try to get 2 characters at one time and delete "0D" if I get "0D0A". It can get right data. But I don't know how to do if the initial data block contains "0D0A".!

        The code:

        #********************************************************************* +********# # Function: # GetFileFromCom # Description: + # Get data from serial port. Chenny--2006-2-27. # Argument: + # 1. Saved file name # 2. Max size of data block + # Return: # #********************************************************************* +********# sub GetFileFromCom($$) { print "Run into GetFileFromCom!\n"; my ($fileName, $maxDataSize) = @_; my( $binData, $binData1, $binData2 ); my $tempX = 0; my $tempY = 0; my $tempI = 0; my $tempCSCal = 0; my $tempCS = 0; open (BINFILE, ">>$fileName") ||print "$!\nCannot write to bin file $fileName\n"; binmode (BINFILE); $portCom1->lookclear; # Clear buffers usleep (10000); syswrite (FH, 'Y', length('Y'), 0); #Send 'Y' to com to start send + data to PC usleep (10000); #delete extr char. for ( $tempI = 0; $tempI < 2; $tempI++ ) { # read(FH, $binData, 1); $binData = getc FH; $binData = ord($binData); printf "0 $tempI: %02X\n", $binData; printf "0 $tempI: %d\n", $binData; } #Get Data header for data begin check. my $decimalData; for ( $tempI = 0; $tempI < 15; $tempI++ ) { $binData = getc FH; $binData = ord($binData); # printf "$tempI: %02X\n", $binData; $decimalData = sprintf ("%d\n", $binData); last if (0 == $decimalData); } #Get Data length. $tempX = $binData; # if ($decimalData > 0 ) { return -1; } # 16M bytes is the +Max file size. printf "1: %02X\n", $tempX; printf "1: %d\n", $tempX; $tempY = getc FH; $tempY = ord($tempY); printf "2: %02X\n", $tempY; printf "2: %d\n", $tempY; $tempX = ($tempX << 24)+ ($tempY << 16); $tempY = getc FH; $tempY = ord($tempY); printf "3: %02X\n", $tempY; printf "3: %d\n", $tempY; $tempX = $tempX+ ($tempY << 8); # read(FH, $tempY, 1); $tempY = getc FH; $tempY = ord($tempY); printf "4: %02X\n", $tempY; printf "4: %d\n", $tempY; $tempX = $tempX + $tempY; printf("\nBlock size is %d bytes", $tempX); if ($tempX > $maxDataSize) # if ($tempX > 8*1024) { printf("\nFile size is too big. size = %d", $tempX); return;# -2; } #Get data byte by byte. $binData1 = getc FH; #Read 2 ch and compare to delete "0D0A" $tempX = $tempX-1; for ( $tempI = 0; $tempI < $tempX; $tempI++) { $binData2 = getc FH; # printf BINFILE ("%02X", ord($binData)); if ( (ord($binData1) == 13) && (ord($binData2) == 10) ) { $binData1 = $binData2; #Delete "0D" Chenny--2006-3-10 $tempI = $tempI-1; } else { print BINFILE $binData1; # printf "%X:%02X ", $tempI, ord($binData1); $binData = ord($binData1); $tempCSCal += $binData; $binData1 = $binData2; } } print BINFILE $binData1; # printf "%X:%02X ", $tempI, ord($binData1); $binData = ord($binData1); $tempCSCal += $binData; $tempX++; print ("\nGet $tempX bytes\n\n"); #Get CS check data--The last two bytes. $tempCS = getc FH; $tempCS = ord($tempCS); # printf("CS condition: ORG1 = %02X\n", $tempCS); $tempY = getc FH; $tempY = ord($tempY); $tempCS = ($tempCS << 8) + $tempY; # printf("CS condition: ORG2 = %X\n", $tempY); # printf("CS CAL condition: CAL = %d, CAL = %X\n", $tempCSCal, +$tempCSCal); $tempCSCal &= 0xffff; #printf("CS CAL condition: CAL = %d, CAL = %X\n", $tempCSCal, $tempCSC +al); printf("\nCS condition: ORG = %04X, CAL = %04X\n\n", $tempCS, $tem +pCSCal); if ($tempCS != $tempCSCal) { printf("\n\nCS Error. ORG = %04X, CAL = %04X!!!\n\n\n", $tempCS, $ +tempCSCal); #return;# -1; } close (BINFILE); return; }
        I will go on! Thank you very much.

        Thanks for your answer!

        Yes, I have sent email to the author: wcbirthisel@alum.mit.edu . But I still have no answer. I have tried ($countIn, $binData) = $portCom1->read (1); But get the same result!

        In fact, I can get right txt data from COM using Win32::SerialPort. And I also can sent right txt data to COM. But when I get a bin data block(such as 15K or more), I get 0D0A (not initial 0A).!

        I have used "SUDT SerialTrace" to trace the serial Port. It also shows 0D0A (not initial 0A). !

        Then, I try to get 2 characters at one time and delete "0D" if I get "0D0A". It can get right data. But I don't know how to do if the initial data block contains "0D0A".!

        The code:!

        <code> #*****************************************************************************# # Function: # GetFileFromCom # Description: # Get data from serial port. Chenny--2006-2-27. # Argument: # 1. Saved file name # 2. Max size of data block # Return: # #*****************************************************************************# sub GetFileFromCom($$) { print "Run into GetFileFromCom!\n"; my ($fileName, $maxDataSize) = @_; my( $binData, $binData1, $binData2 ); my $tempX = 0; my $tempY = 0; my $tempI = 0; my $tempCSCal = 0; my $tempCS = 0; open (BINFILE, ">>$fileName") ||print "$!\nCannot write to bin file $fileName\n"; binmode (BINFILE); $portCom1->lookclear; # Clear buffers usleep (10000); syswrite (FH, 'Y', length('Y'), 0); #Send 'Y' to com to start send data to PC usleep (10000); #delete extr char. for ( $tempI = 0; $tempI < 2; $tempI++ ) { # read(FH, $binData, 1); $binData = getc FH; $binData = ord($binData); printf "0 $tempI: %02X\n", $binData; printf "0 $tempI: %d\n", $binData; } #Get Data header for data begin check. my $decimalData; for ( $tempI = 0; $tempI < 15; $tempI++ ) { $binData = getc FH; $binData = ord($binData); # printf "$tempI: %02X\n", $binData; $decimalData = sprintf ("%d\n", $binData); last if (0 == $decimalData); } #Get Data length. $tempX = $binData; # if ($decimalData > 0 ) { return -1; } # 16M bytes is the Max file size. printf "1: %02X\n", $tempX; printf "1: %d\n", $tempX; $tempY = getc FH; $tempY = ord($tempY); printf "2: %02X\n", $tempY; printf "2: %d\n", $tempY; $tempX = ($tempX << 24)+ ($tempY << 16); $tempY = getc FH; $tempY = ord($tempY); printf "3: %02X\n", $tempY; printf "3: %d\n", $tempY; $tempX = $tempX+ ($tempY << 8); # read(FH, $tempY, 1); $tempY = getc FH; $tempY = ord($tempY); printf "4: %02X\n", $tempY; printf "4: %d\n", $tempY; $tempX = $tempX + $tempY; printf("\nBlock size is %d bytes", $tempX); if ($tempX > $maxDataSize) # if ($tempX > 8*1024) { printf("\nFile size is too big. size = %d", $tempX); return;# -2; } #Get data byte by byte. $binData1 = getc FH; #Read 2 ch and compare to delete "0D0A" $tempX = $tempX-1; for ( $tempI = 0; $tempI < $tempX; $tempI++) { $binData2 = getc FH; # printf BINFILE ("%02X", ord($binData)); if ( (ord($binData1) == 13) && (ord($binData2) == 10) ) { $binData1 = $binData2; #Delete "0D" Chenny--2006-3-10 $tempI = $tempI-1; } else { print BINFILE $binData1; # printf "%X:%02X ", $tempI, ord($binData1); $binData = ord($binData1); $tempCSCal += $binData; $binData1 = $binData2; } } print BINFILE $binData1; # printf "%X:%02X ", $tempI, ord($binData1); $binData = ord($binData1); $tempCSCal += $binData; $tempX++; print ("\nGet $tempX bytes\n\n"); #Get CS check data--The last two bytes. $tempCS = getc FH; $tempCS = ord($tempCS); # printf("CS condition: ORG1 = %02X\n", $tempCS); $tempY = getc FH; $tempY = ord($tempY); $tempCS = ($tempCS << 8) + $tempY; # printf("CS condition: ORG2 = %X\n", $tempY); # printf("CS CAL condition: CAL = %d, CAL = %X\n", $tempCSCal, $tempCSCal); $tempCSCal &= 0xffff; #printf("CS CAL condition: CAL = %d, CAL = %X\n", $tempCSCal, $tempCSCal); printf("\nCS condition: ORG = %04X, CAL = %04X\n\n", $tempCS, $tempCSCal); if ($tempCS != $tempCSCal) { printf("\n\nCS Error. ORG = %04X, CAL = %04X!!!\n\n\n", $tempCS, $tempCSCal); #return;# -1; } close (BINFILE); return; } <code> I will go on! Thank you very much.