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.


In reply to Re^5: How to get right data from COM1 using Win32::SerialPort by Chenny
in thread How to get right data from COM1 using Win32::SerialPort by Chenny

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.