2 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use strict;
require 5.003;
use Win32API::CommPort;
use Win32::SerialPort qw( :STAT 0.19);
use Win32;
my $quiet = 1; # Einschränkung bei Fehlermeldung
my $PortName = 'COM3';
my $configFile = 'ER400TRS.cfg';
my $PortObj = new Win32::SerialPort( $PortName, $quiet )
or die "Can't open $PortName: $^E\n";
## initialize
$PortObj->user_msg( 'ON' );
$PortObj->baudrate( 19200 );
$PortObj->databits( 8 );
$PortObj->parity( "none" );
$PortObj->stopbits( 1 );
$PortObj->handshake( "none" );
my @ar=$PortObj->buffers( 128, 128 ); ## read buffer, write buffer
## write settings to com port
$PortObj->write_settings or undef $PortObj;
unless( $PortObj ){ print "can't change device control block: $^E\n"; }
## save settings in configuration file
$PortObj->save( $configFile ) or warn "can't save $configFile: $^E\n";
## close all stuff
$PortObj->close or die "failed to close";
undef $PortObj; ## frees memory back to perl
## open again with tie (needed for some functions)
$PortObj = tie( *COMFH, 'Win32::SerialPort', $configFile )
or die "can't tie using $configFile: ^E\n";
### ready to communicate
##get current status
my $BlockingFlags;
my ( $InBytes, $OutBytes, $LatchErrorFlags );
( $BlockingFlags, $InBytes, $OutBytes, $LatchErrorFlags ) = $PortObj->status
|| warn "could not get port status\n";
if ( $BlockingFlags ) { warn "Port is blocked"; }
if ( $BlockingFlags & BM_fCtsHold ) { warn "Waiting for CTS (clear to send)"; }
if ( $LatchErrorFlags & CE_FRAME ) { warn "Framing Error"; }
#######write to serial port###################
my ( $count_out, $output_string );
$output_string = "Hallo ";
my $i = 10;
while( $i > 0 ) {
$count_out = $PortObj->write( $output_string );
warn "write failed\n" unless ( $count_out );
warn "write incomplete\n" if ( $count_out != length( $output_string ) );
$i--;
}
## close all stuff
$PortObj->close or die "failed to close";
undef $PortObj; ## frees memory back to perl
1
2
3
4
5
6
## read
$InBytes = 1;
my ( $count_in, $string_in );
( $count_in, $string_in ) = $PortObj->read( $InBytes );
print "count: $count_in\nin: $string_in\n";
warn "read unsuccessful\n" unless ( $count_in == $InBytes );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(1){
$ca++;
($BlockingFlags,$InBytes,$OutBytes,$ErrorFlags) = $PortObj->status;
die "lost port\n" unless defined $ErrorFlags;
unless($InBytes){
last if($ca > 25);
select(undef,undef,undef,0.01);
next;
}
if($string_in = $PortObj->read($InBytes)){
# hat geklappt mach was
last;
}
}
2 Einträge, 1 Seite |