The files contained in this repository can be downloaded to your computer using a svn client.
On Linux you simply type the command displayed below.

This URL has Read-Only access.

Statistics
| Revision:

root / trunk / DomotiGaServer / CVISCA.class @ 707

History | View | Annotate | Download (9.2 kB)

1
' Gambas class file
2
3
' Description:
4
' CVISCA.class
5
' Support for Sony VISCA protocol to control PTZ camera's
6
7
' Development Status:
8
' Working, a few bugs to fix.
9
10
' Links:
11
' http://www.vision.auc.dk/~tbm/Sony/EVID30.pdf
12
' http://animatlab.lip6.fr/~gouricho/SonyPTZCamEVID31/evidapplication.html
13
' http://www.j3soft.net/webcam/evi-d30.htm
14
' http://www.j3soft.net/webcam/evi-d31.htm
15
' http://f1chf.free.fr/SONY/
16
17
' DomotiGa - an open source home automation program.
18
' Copyright(C) 2008-2009 Ron Klinkien
19
20
' Read file called COPYING for license details.
21
22
PROPERTY Port AS String
23
PROPERTY VISCADebug AS Boolean
24
25
PRIVATE sPort AS String
26
PRIVATE bVISCADebug AS Boolean
27
28
PUBLIC hVISCA AS NEW SerialPort
29
PUBLIC bCamNum AS Byte = &H80 + Hex(Main.iVISCACameraAddress)
30
31
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32
' open serial port
33
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34
PUBLIC FUNCTION Connect() AS Boolean
35
36
  ' try to close the port
37
  TRY hVISCA.Close
38
39
  ' get a new one
40
  hVISCA = NEW Serialport AS "VISCA"
41
  WITH hVISCA
42
    .PortName = sPort
43
    .Speed = 9600
44
    .Parity = 0
45
    .DataBits = 8
46
    .StopBits = 1
47
    .FlowControl = 0
48
    .Open()
49
  END WITH
50
51
  ' all ok
52
  RETURN TRUE
53
54
CATCH ' some errors
55
  Main.WriteLog(("VISCA Error: ") & ERROR.Text)
56
  RETURN FALSE
57
58
END
59
60
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61
' close port
62
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63
PUBLIC FUNCTION Disconnect() AS Boolean
64
65
  ' try to close the connection
66
  TRY hVISCA.Close
67
  Main.WriteLog(("VISCA serial port close."))
68
69
  ' all ok
70
  RETURN TRUE
71
72
CATCH ' some errors
73
  Main.WriteLog(("VISCA Error: ") & ERROR.Text)
74
  RETURN FALSE
75
76
END
77
78
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79
' send a command to the camera
80
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81
PUBLIC FUNCTION SendCommand(aCommand AS Array)
82
83
  DIM iCmd AS Byte
84
85
  IF Main.bVISCADebug THEN Main.WriteDebugLog("[VISCA] > ", TRUE)
86
  FOR EACH iCmd IN aCommand
87
    IF Main.bVISCADebug THEN Main.WriteRFXDebugLog(Hex$(iCmd, 2) & " ", TRUE)
88
    TRY WRITE #hVISCA, iCmd
89
  NEXT
90
  IF Main.bVISCADebug THEN Main.WriteRFXDebugLog("\n", TRUE)
91
92
END
93
94
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95
' got data back from camera
96
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97
PUBLIC SUB VISCA_Read()
98
99
  DIM sData AS Byte
100
101
  TRY READ #hVISCA, sData
102
  IF ERROR THEN Main.WriteDebugLog(("[VISCA] Error reading data from the serial port! ->") & ERROR.Text)
103
  IF Main.bVISCADebug THEN Main.WriteDebugLog("[VISCA] < " & Hex$(sData, 2))
104
105
END
106
107
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108
' send address set command to camera
109
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
PUBLIC SUB AddressSet()
111
112
  DIM b AS Byte[] = [CByte(&H88), CByte(&H30), &H1, &HFF]
113
114
  SendCommand(b)
115
116
END
117
118
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119
' send clear command to camera
120
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121
PUBLIC SUB IfClear()
122
123
  DIM b AS Byte[] = [CByte(&H88), CByte(&H1), &H0, &H1, &HFF]
124
125
  SendCommand(b)
126
127
END
128
129
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130
' switch camera on and off (power save mode)
131
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132
PUBLIC SUB Power(sMode AS String)
133
134
  DIM b AS Byte[] = [CByte(bCamNum), CByte(&h1), &H4, &H0, IIf(sMode = "on", &H2, &H3), &HFF]
135
136
  SendCommand(b)
137
138
END
139
140
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
141
' change white balance setting
142
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
143
PUBLIC SUB WhiteBalance(sMode AS String)
144
145
  DIM b AS Byte[6]
146
147
  b[0] = bCamNum
148
  b[1] = &H1
149
  b[2] = &H4
150
  b[3] = &H35
151
152
  SELECT CASE LCase(sMode)
153
    CASE "auto"
154
      b[4] = &H0
155
    CASE "indoor"
156
      b[4] = &H1
157
    CASE "outdoor"
158
      b[4] = &H2
159
    CASE "onepush"
160
      b[4] = &H3
161
    CASE "trigger"
162
      b[4] = &H4
163
  END SELECT
164
165
  b[5] = &HFF
166
  SendCommand(b)
167
168
END
169
170
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171
' control autoexposure mode
172
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
173
PUBLIC SUB AutoExposure(sMode AS String)
174
175
  DIM b AS Byte[6]
176
177
  b[0] = bCamNum
178
  b[1] = &H1
179
  b[2] = &H4
180
  b[3] = &H39
181
182
  SELECT CASE LCase(sMode)
183
    CASE "auto"
184
      b[4] = &H0
185
    CASE "manual"
186
      b[4] = &H3
187
    CASE "shutter"
188
      b[4] = &HA
189
    CASE "iris"
190
      b[4] = &HB
191
    CASE "bright"
192
      b[4] = &HD
193
  END SELECT
194
195
  b[5] = &HFF
196
  SendCommand(b)
197
198
END
199
200
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
' control exposure modes (bright, shutter, iris and gain)
202
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203
PUBLIC SUB ManualExposure(sMode AS String, sDo AS String)
204
205
  DIM b AS Byte[7]
206
207
  b[0] = bCamNum
208
  b[1] = &H1
209
  b[2] = &H4
210
  b[3] = &H0
211
212
  SELECT CASE LCase(sMode)
213
    CASE "bright"
214
      b[4] = &HD
215
    CASE "shutter"
216
      b[4] = &HA
217
    CASE "iris"
218
      b[4] = &HB
219
    CASE "gain"
220
      b[4] = &HC
221
  END SELECT
222
223
  IF sDo = "-" THEN
224
    b[5] = &H2
225
  ELSE IF sDo = "+" THEN
226
    b[5] = &H3
227
  ELSE
228
    b[5] = &H0 ' reset
229
  END IF
230
231
  b[6] = &HFF
232
  SendCommand(b)
233
234
END
235
236
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237
' switch backlight compensation on/off
238
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239
PUBLIC SUB BackLight(sMode AS String)
240
241
  DIM b AS Byte[] = [CByte(bCamNum), CByte(&H1), &H4, &H33, IIf(sMode = "on", &H2, &H3), &HFF]
242
243
  SendCommand(b)
244
245
END
246
247
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
248
' tiltdrive
249
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
250
PUBLIC SUB Direction(sMode AS String, iPanSpeed AS Integer, iTiltSpeed AS Integer)
251
252
  DIM b AS Byte[9]
253
254
  b[0] = bCamNum
255
  b[1] = &H1
256
  b[2] = &H6
257
  b[3] = &H1
258
  b[4] = Format(Val("&H" & Hex(iPanSpeed)))
259
  b[5] = Format(Val("&H" & Hex(iTiltSpeed)))
260
261
  SELECT CASE LCase(sMode)
262
    CASE "up"
263
      b[6] = &H3
264
      b[7] = &H1
265
    CASE "down"
266
      b[6] = &H3
267
      b[7] = &H2
268
    CASE "left"
269
      b[6] = &H1
270
      b[7] = &H3
271
    CASE "right"
272
      b[6] = &H2
273
      b[7] = &H3
274
    CASE "upleft"
275
      b[6] = &H1
276
      b[7] = &H1
277
    CASE "downleft"
278
      b[6] = &H1
279
      b[7] = &H2
280
    CASE "upright"
281
      b[6] = &H2
282
      b[7] = &H1
283
    CASE "downright"
284
      b[6] = &H2
285
      b[7] = &H2
286
    CASE "stop"
287
      b[6] = &H3
288
      b[7] = &H3
289
  END SELECT
290
291
  b[8] = &HFF
292
  SendCommand(b)
293
294
END
295
296
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
297
' stop whatever you are doing
298
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299
PUBLIC SUB StopCommand()
300
301
  DIM b AS Byte[9]
302
303
  b[0] = bCamNum
304
  b[1] = &H1
305
  b[2] = &H6
306
  b[3] = &H1
307
  b[4] = &H10
308
  b[5] = &H10
309
  b[6] = &H3
310
  b[7] = &H3
311
  b[8] = &HFF
312
313
  SendCommand(b)
314
315
END
316
317
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
318
' go home
319
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
320
PUBLIC SUB Home()
321
322
  DIM b AS Byte[] = [CByte(bCamNum), CByte(&H1), &H6, &H4, &HFF]
323
324
  SendCommand(b)
325
326
END
327
328
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
329
' memory reset, set and recall
330
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
331
PUBLIC SUB Memory(sPreset AS String, bRecall AS Boolean, bRecallSet AS Boolean)
332
333
  DIM b AS Byte[7]
334
335
  b[0] = bCamNum
336
  b[1] = &H1
337
  b[2] = &H4
338
  b[3] = &H3F
339
340
  IF bRecall = TRUE THEN
341
    b[4] = &H2 ' recall
342
  ELSE IF BRecallSet = TRUE THEN
343
    b[4] = &H1 ' set
344
  ELSE
345
    b[4] = &H0 ' reset
346
  END IF
347
348
  b[5] = sPreset
349
  b[6] = &HFF
350
  SendCommand(b)
351
352
END
353
354
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
355
' power inquiry
356
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
357
PUBLIC SUB PowerInq()
358
359
  DIM b AS Byte[] = [CByte(bCamNum), CByte(&H9), &H4, &H0, &HFF]
360
361
  SendCommand(b)
362
363
END
364
365
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
366
' control focus modes (stop, near, far, auto and manual)
367
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
368
PUBLIC SUB Focus(sMode AS String)
369
370
  DIM b AS Byte[6]
371
372
  b[0] = bCamNum
373
  b[1] = &H1
374
  b[2] = &H4
375
376
  SELECT CASE LCase(sMode)
377
    CASE "stop"
378
      b[3] = &H8
379
      b[4] = &H0
380
    CASE "far"
381
      b[3] = &H8
382
      b[4] = &H2
383
    CASE "near"
384
      b[3] = &H8
385
      b[4] = &H3
386
    CASE "auto"
387
      b[4] = &H38
388
      b[4] = &H2
389
    CASE "manual"
390
      b[4] = &H38
391
      b[5] = &H3
392
  END SELECT
393
394
  b[5] = &HFF
395
  SendCommand(b)
396
397
END
398
399
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
400
' control zoom (stop, direct, tele, wide, tele(var) and wide(var)
401
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
402
PUBLIC SUB Zoom(sMode AS String, iValue AS Integer)
403
404
  DIM b AS Byte[6]
405
406
  b[0] = bCamNum
407
  b[1] = &H1
408
  b[2] = &H4
409
410
  SELECT CASE LCase(sMode)
411
    CASE "stop"
412
      b[3] = &H7
413
      b[4] = &H0
414
    CASE "tele"
415
      b[3] = &H7
416
      IF iValue > 1 THEN
417
        b[4] = &H2 + Hex(iValue)
418
      ELSE IF iValue > 7 THEN
419
        b[4] = &H2 + Hex(7)
420
      ELSE
421
        b[4] = &H2
422
      END IF
423
    CASE "wide"
424
      b[3] = &H7
425
      IF iValue > 1 THEN
426
        b[4] = &H3 + Hex(iValue)
427
      ELSE IF iValue > 7 THEN
428
        b[4] = &H2 + Hex(7)
429
      ELSE
430
        b[4] = &H3
431
      END IF
432
      b[4] = &H3
433
  END SELECT
434
435
  b[5] = &HFF
436
  SendCommand(b)
437
438
END
439
440
441
' implement the properties
442
PRIVATE FUNCTION Port_Read() AS String
443
444
  RETURN sPort
445
446
END
447
448
PRIVATE SUB Port_Write(sValue AS String)
449
450
  sPort = sValue
451
452
END
453
454
PRIVATE FUNCTION VISCADebug_Read() AS Boolean
455
456
  RETURN bVISCADebug
457
458
END
459
460
PRIVATE SUB VISCADebug_Write(sValue AS Boolean)
461
462
  bVISCADebug = sValue
463
464
END