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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
| sub write_map{
my ($name,$value) = @_;
$map_settings = 1;
my $hex = sprintf "%x", $name;
warn "WRITTING to $name ($hex) value $value";
&warn_write;
&writemap($name, $value);
#initiate values!
&writemap(0,7);
}
sub prepare_eeprom {
@mapcommand =('P', 'C', 'W', 'R', "\r", "\n"); # 0 1 2 3 4 5 6 7 8 9 10 11 12
my ($addr, $command) = @_;
$map_settings = 1;
$addr = '0';
$command = 3;
my ($error, $answer, $checksum, $data);
my ($a0, $a1, $a2);
my ($s0, $s1);
my ($d0, $d1);
my $temp;
return if ($port_not_opening);
$temp = $addr>>8;
$a0=&digit2ascii($temp);
$temp = ($addr>>4)&0x0F;
$a1=&digit2ascii($temp);
$temp = $addr&0x0F;
$a2=&digit2ascii($temp);
$mapcommand[4]=chr($a0);
$mapcommand[5]=chr($a1);
$mapcommand[6]=chr($a2);
$temp = $command>>4;
$d0 = &digit2ascii($temp);
$temp = $command&0x0F;
$d1 = &digit2ascii($temp);
my $sum = 0; $sum = $a0+$a1+$a2+$command; $sum =0xff-$sum; $sum++;
$temp = $sum>>4;
$s0=&digit2ascii($temp);
$temp = $sum&0x0F;
$s1=&digit2ascii($temp);
$mapcommand[7]=chr($d0);
$mapcommand[8]=chr($d1);
$mapcommand[9]=chr($s0);
$mapcommand[10]=chr($s1);
$mapcommand[11]="\r";
$mapcommand[12]="\n";
$answer="";
my $result = send_mapcommand_to_device(@mapcommand);
if ($result != 0) {
$error=2;
$port_not_opening = $port_in_use;
return 0xff;
}
($result, $answer) = &wait_device_response;
if ($result != 0){
$error=3;
$port_not_opening = $port_in_use;
}
chop ($answer);
chop ($answer);
if ($answer=~/MOk/ ) {
$temp = ord(substr($answer,3,1));
$a0=&ascii2digit($temp);
$temp = ord(substr($answer,3,1));
$a1=&ascii2digit($temp);
$data=($a0<<4)+$a1;
$error=0;
return $data;
} else {
$error=1;
$port_not_opening = $port_in_use;
}
}
sub writemap {
$map_settings = 1;
&prepare_eeprom;
@mapcommand =('P', 'C', 'W', 'R', '0', '0', '0', "\r", "\n"); # 0 1 2 3 4 5 6 7 8 9 10 11 12
my ($addr, $command) = @_;
my ($error, $answer, $checksum, $data);
my ($a0, $a1, $a2);
my ($s0, $s1);
my ($d0, $d1, $d2);
my $temp;
return if ($port_not_opening);
$temp = $addr>>8;
$a0=&digit2ascii($temp);
$temp = ($addr>>4)&0x0F;
$a1=&digit2ascii($temp);
$temp = $addr&0x0F;
$a2=&digit2ascii($temp);
$mapcommand[4]=chr($a0);
$mapcommand[5]=chr($a1);
$mapcommand[6]=chr($a2);
$temp = $command>>4;
$d0 = &digit2ascii($temp);
$temp = $command&0x0F;
$d1 = &digit2ascii($temp);
my $sum = 0; $sum = $a0+$a1+$a2+$command;
$sum = 255 - $sum;
$sum = $sum + 1;
my $shifted = ($sum>>4)&0x0f;
$s0=&digit2ascii($shifted);
$shifted = $sum & 0x0F;
$s1=&digit2ascii($shifted);
$mapcommand[7]=chr($d0);
$mapcommand[8]=chr($d1);
$mapcommand[9]=chr($s0);
$mapcommand[10]=chr($s1);
$mapcommand[11]="\r";
$mapcommand[12]="\n";
$answer="";
my $result = send_mapcommand_to_device(@mapcommand);
if ($result != 0) {
$error=2;
$port_not_opening = $port_in_use;
return 0xff;
}
($result, $answer) = &wait_device_response;
if ($result != 0){
$error=3;
#$port_not_opening = $port_in_use;
}
chop ($answer);
chop ($answer);
if ($answer=~/MOk/ ) {
$temp = ord(substr($answer,3,1));
$a0=&ascii2digit($temp);
$temp = ord(substr($answer,3,1));
$a1=&ascii2digit($temp);
$data=($a0<<4)+$a1;
$error=0;
return $data;
} else {
$error=1;
$port_not_opening = $port_in_use;
}
}
sub send_mapcommand_to_device {
my @mapcommand = @_;
my $length = @mapcommand;
#print "sending mapcommand to device: ", @mapcommand, "\n" if ($debug);
my ($count_in, $string_in);
my $ccnt = 0;
for (my $i=0; $i< $length; $i++) {
my $tosend = $mapcommand[$i];
if (!defined($PortObj)){
#print "sudden disconnect at send_mapcommand_to_device first! port_in_use=$port_in_use\n";
#$port_not_opening = 'ALL';
$port_not_opening = $port_in_use;
&connect;
last;
}
my $sent = $PortObj->write($tosend);
while(1){ #receive echo
$ccnt++;
($count_in, $string_in) = $PortObj->read(1); #read 1 byte
if (!defined($string_in)){
#print "sudden disconnect second ! port_in_use=$port_in_use\n";
$port_not_opening = $port_in_use;
&connect;
last;
}
last if ($ccnt > 250);
next if ($string_in eq 'M');
if ($count_in){
return (-1) if ($count_in !=1); #error - can;t receive the byte
}
last if ($string_in eq $tosend); #check if same char received, else continue while
}
}
return 0; #no error
}
sub wait_device_response {
my $answer = "";
my $ccnt = 0;
while(1) {
$ccnt++;
my ($count_in, $string_in) = $PortObj->read(1); #read 1 byte
unless (defined($string_in)){
#print "sudden disconnect second ! port_in_use=$port_in_use\n";
$port_not_opening = $port_in_use;
&connect;
last;
}
if ($count_in != 1) { #err , return anyway the string too
return (-1, $answer);
}
last if ($ccnt > 250);
$PortObj->write($string_in); #echo it
$answer .= $string_in;
return (0, $answer) if ($string_in eq "\n"); #exit ok, received string in answer
}
} |