Leser: 2
5 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use LabVIEW_Comm;
use DBI;
#------- Parameterübergabe -----------------#
foreach $eintrag (@ARGV)
{
push(@eintraege,$eintrag);
}
$dbname = $eintraege[0];
$hostname = $eintraege[1];
$user = $eintraege[2];
$password = $eintraege[3];
$driver = $eintraege[4];
$mdb_file = $hostname."\\".$dbname;
#--- ACCESS Verbindung -----------#
my $connection_string = q{;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=};
my $dbh = DBI->connect("dbi:ADO:Data Source=".$mdb_file.$connection_string, "");
#---- Datenbankverbindung ---#
#my $dbname='ess';
#my $hostname='localhost';
#my $driver='mysql';
#my $user='';
#my $password='';
#my $dsn="DBI:$driver:dbname=$dbname;host=$hostname";
#my $dbh = DBI->connect($dsn, $user, $password,{RaiseError=>0});
my $sth_select;
$PORT = 9000;
$_='';
until (/exit/)
{
$labview = LabVIEW_Comm::Open_LV_Connection;
$str = LabVIEW_Comm::Read_LV();
$_ = $str;
if ($str eq "stop\n") # Ist der String auf dem Netz "stop" so wird die Verbindung geschlossen und das Script beendet
{
LabVIEW_Comm::Close_LV_Connection;
exit;
}
#----------- SQL-String auf shell ausgeben ------------------#
print $str."\n";
#--------------------------------------------------------------#
@sql=split(/ +/,$str);
$befehl=@sql[0];
$befehl=lc($befehl); # $befehl immer in Kleinbuchstaben wandeln
if($befehl eq "select")
{
$antwort=&select("$str");
$sql_antwort="";
$str="";
}
if(($befehl eq "delete") or ($befehl eq "insert") or ($befehl eq "update") or ($befehl eq "load"))
{
$antwort=&sqloa("$str","$befehl");
$str="";
#print $str.$befehl;
}
LabVIEW_Comm::Write_LV(" ".$antwort);
}
# sub für select Statement
sub select()
{
$sth_select=$dbh->prepare("$_[0]") or ($sql_fail=1);
$sth_select->execute or ($sql_fail=1);
#--- Anzahl der betroffenen Zeilen auslesen und in $cnt speichern ----#
$zaehler = $dbh->prepare("SELECT count(*) FROM (($_[0]) As tblTemp)");
$zaehler->execute;
while (my $zahl = $zaehler->fetchrow)
{
$cnt=$zahl;
}
#----------------------------------------------------------------------#
if ($cnt eq "0E0")
{
return "nix da";
}
if ($sql_fail == 1)
{
return "SQL- Fehler";
$sql_fail=0;
}
else
{
while (my @ausgabe = $sth_select->fetchrow_array)
{
for ($x=0;$x<=@ausgabe;$x++)
{
$test=$test.$ausgabe[$x]."|";
}
$sql_antwort=$sql_antwort.$test."\n";
$test=""; # inhalt von $test wieder löschen!!
}
$sql_antwort="$cnt*".$sql_antwort."END";
$cnt=0;
return "$sql_antwort";
}
$sth_select->finish;
$dbh->disconnect;
} # Ende sub "select"
# sub für delete Statement
sub sqloa()
{
$sth_oa=$dbh->prepare("$_[0]") or ($sql_fail=1);
$sth_oa->execute or ($sql_fail=1);
$sth_oa->finish;
if ($sql_fail == 1)
{
return " SQL- Fehler";
$sql_fail=0;
}
else
{
return " $_[1] erfolgreich";
}
} # Ende sub "sqloa"
$dbh->disconnect;
$dummy=<STDIN>;
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package LabVIEW_Comm;
{
###############################################################################
#
# DESCRIPTION:
# This package encloses the subroutines necessary for communication with LabVIEW:
#
# Open_LV_Connection([$port,$timeout]);
# returns a filehandle that can be used with "print" statements to send data
# from Perl to LabVIEW and vice-versa. Should be used with "Open Perl Connection.vi"
# Global filehandle is $labview that is used by the other routines.
#
# Write_LV($command_name);
# Sends LabVIEW the string $str.
# Returns a value of 1 if successful, 0 if there was an error.
#
# Read_LV();
# Reads a string sent from LabVIEW. Interpreting this string will depend on the context.
# Returns the value read from Labview.
#
# Close_LV_Connection();
# Closes the TCP connection.
#
###############################################################################
# Stuff required for package exporting its subs and variables
#require Exporter;
#@ISA = qw(Exporter);
#@EXPORT = qw( Open_LV_Connection
# Write_LV
# Read_LV
# Close_LV_Connection
# $labview,$text);
#@EXPORT_OK = qw();
sub Open_LV_Connection
{
use IO::Socket;
use Net::hostent; # for OO version of gethostbyaddr
# Set up the server object
$PORT = 9000;
$timeout = 50000;
$server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
Reuse => 1,
Timeout => $timeout);
die "can't setup TCP server" unless $server;
# To reduce the size of the STDOUT file, only report every tenth connection.
if ( (($Connect_Count) % 10) == 0 )
{
printf "Server port: $PORT Timeout value: $timeout Connect count: $Connect_Count\n";
}
# Now set up a client filehandle, $labview. This will be a global used in other places
$labview = $server->accept();
$labview->autoflush(1);
# To reduce the size of the STDOUT file, only report every tenth connection.
if ( (($Connect_Count++) % 10) == 0 )
{
printf "LabVIEW connected \n";
}
return $labview;
}
sub Close_LV_Connection()
{
close($labview);
}
sub Write_LV
{
if ($labview)
{
print $labview ($_[0]); # Send command
print $labview "\r\n"; # print terminator
while (<$labview>) # Check for response
{
next unless /\S/; # ignore blank lines
print "stuff was $_\n";
if (/OK/i)
{
return 1;
print "OK\n";
my $back=ok;
}
else
{
return 0;
}
}
}
else
{
return 0;
}
}
sub Read_LV()
{
while (<$labview>)
{
next unless /\S/; # ignore blank lines
return $_;
}
}
return 1;
}
5 Einträge, 1 Seite |