in reply to Re^6: Win32::SerialPort ; close / open problem
in thread Win32::SerialPort ; close / open problem

Ok, so I was thinking about this business with the byte number being wrong.
I wondered what would happen if I did not set the Baud, and lo, the same behavior occurred. If I set the baud at some high number, it will just give zeros, as expected.
It should be setting the baud though. I have it set to let me know if there were problems.
FYI, here is the code.
use warnings; use strict; use Win32::FTDI::FTD2XX qw(:DEFAULT FT_BAUD_2400 FT_BITS_8 FT_STOP_BITS_1 FT_PARITY_NONE FT_FLOW_RTS_CTS PFT_MODEM_STATUS_CTS ); my $FTD = Win32::FTDI::FTD2XX->new(); unless( $FTD->PFT_STATUS() == FT_OK ) { printf( STDERR "FTD2XX::new() failed: %s (%s)\n", $FTD->PFT_STATUS_MSG(), $FTD->PFT_ERROR() ); exit( 1 ); } printf( "FTD2XX::new() allocated PFT_HANDLE: %d\n", $FTD->PFT_HANDLE +() ); my $numDevices = $FTD->GetNumDevices(); unless( $FTD->PFT_STATUS() == FT_OK ) { printf( STDERR "FTD2XX::GetNumDevices() failed: %s (%s)\n", $FTD->PFT_STATUS_MSG(), $FTD->PFT_ERROR() ); exit( 1 ); } printf( "Found $numDevices FTDI devices connected!\n" ); $FTD->PFT_DEBUG(1); unless ($FTD->OpenByIndex(0)) { print "Problem opening Port 0\n"; return 0; } unless ($FTD->Purge(FT_PURGE_RX)) { print 'cannot purge'; } unless ($FTD->SetBaudRate(FT_BAUD_2400)) { print "Baud is set\n"; return 0; } peek_buf(); sub split_len { my ($len, $string) = @_; $string =~ m/.{1,$len}/g; } sub peek_buf { my ($amountInRxQueue, $amountInTxQueue, $eventStatus) = $FTD->GetS +tatus(); print "trying to read $amountInRxQueue\n"; while ( $amountInRxQueue< 45) { ($amountInRxQueue, $amountInTxQueue, $eventStatus) = $FTD->Get +Status(); print "$amountInRxQueue in q\n"; sleep(1); } my ($bytesReturned, $readBuffer) = $FTD->Read($amountInRxQueue); my $numHex = $bytesReturned*2; my $upack = unpack("H$numHex", $readBuffer); my @a = split_len(18, $upack); print join "\n", @a,"\n"; }

Replies are listed 'Best First'.
Re^8: Win32::SerialPort ; close / open problem
by philipMac (Novice) on Aug 10, 2009 at 19:06 UTC
    Solved it. When you init a new FTD, I needed to
    sub init_FTD { my $FT = Win32::FTDI::FTD2XX->new(); unless( $FT->PFT_STATUS() == FT_OK ) { printf( STDERR "FTD2XX::new() failed: %s (%s)\n", $FT->PFT_STATUS_ +MSG(), $FT->PFT_ERROR() ); return 0; } printf( "FTD2XX::new() allocated PFT_HANDLE: %d\n", $FT->PFT_HANDLE( +) ); # $FT->PFT_DEBUG(1); unless ($FT->OpenByIndex(0)) { print "Problem opening Port 0\n"; return 0; } unless ($FT->SetBreakOn) { print "failed to set brake\n"; } unless ($FT->Purge(FT_PURGE_RX)) { print 'cannot purge'; } unless ($FT->SetBaudRate(FT_BAUD_2400)) { print "Baud is set\n"; return 0; } return $FT; }

    otherwise all the data that comes in is seemingly junk.

    That was incredibly painful to figure out. I knew I would get there though, once I asked for other people's assistance. Its the way of things.