Excel et communication

Résolu
Anguel Messages postés 31 Date d'inscription mardi 8 novembre 2005 Statut Membre Dernière intervention 15 septembre 2011 - 29 avril 2007 à 21:22
Anguel Messages postés 31 Date d'inscription mardi 8 novembre 2005 Statut Membre Dernière intervention 15 septembre 2011 - 1 mai 2007 à 23:42
Bonjour a tous.



J'ai un problème lequel me prend déjà deux mois et je n'arrive pas trouvé la solution :
 Je travail avec un fichier Excel dans le feuil 1 j'ai un bouton de commande lequel change des valeurs dans trois cellules et JE veux utiliser ce valeurs pour les envoyer en travers la porte séries rs232 pour diriger une cameras Mini Dôme . Comment faire?
Ma caméra est Speed Mini Dôme, elle travaille en code Pelco P , avec une interface RS485 , Sur le clavier de commande il y a RS422 .
Je pence fabriquer ou acheter un convertisseur RS232/RS422 ou RS232/RS485.

Voila le Code pour Pelco ecrit en C# (je le trouve sur le Net). La Question est : Comment faire avec ce code dans Excel et VB6  , Il m'interesse que les commandes :Stop, Go Preset , Left et Pattern :
MERCI




 



using




System;



using




System.Collections;



namespace




Pelco
{







///


<summary>






///
dot.NET Implementation of Pelco P Protocol






///


</summary>






public



class
P
{


private



const



byte
STX = 0xA0;


private



const



byte
ETX = 0xAF;



#region




Pan and Tilt Commands



#region




Data1





private



const



byte
FocusFar = 0x01;


private



const



byte
FocusNear = 0x02;


private



const



byte
IrisOpen = 0x04;


private



const



byte
IrisClose = 0x08;


private



const



byte
CameraOnOff = 0x10;


private



const



byte
AutoscanOn = 0x20;


private



const



byte
CameraOn = 0x40;


#endregion


#region




Data2





private



const



byte
PanRight = 0x02;


private



const



byte
PanLeft = 0x04;


private



const



byte
TiltUp = 0x08;


private



const



byte
TiltDown = 0x10;


private



const



byte
ZoomTele = 0x20;


private



const



byte
ZoomWide = 0x40;


#endregion


#region




Data3





private



const



byte
PanSpeedMin = 0x00;


private



const



byte
PanSpeedMax = 0x40;


#endregion


#region




Data4





private



const



byte
TiltSpeedMin = 0x00;


private



const



byte
TiltSpeedMax = 0x3F;


#endregion


#endregion


#region




Enums





public



enum
PresetAction {Set,Clear,Goto}


public



enum
PatternAction {Start,Stop,Run}


public



enum



Action
{Start,Stop}


public



enum
LensSpeed {Low=0x00,Medium=0x01,High=0x02,Turbo=0x03}


public



enum
Pan {Left PanLeft,Right PanRight}


public



enum
Tilt {Up TiltUp,Down TiltDown}


public



enum
Iris {Open IrisOpen,Close IrisClose}


public



enum
Zoom {Wide ZoomWide,Tele ZoomTele}


public



enum
Switch {On,Off}


public



enum
Focus {Near FocusNear,Far FocusFar}


#endregion


#region




Extended Command Set





public



byte
[] Preset(

uint
deviceAddress,

byte
preset, PresetAction action){


byte
m_action;


switch
(action){


case
PresetAction.Set:m_action = 0x03;


break
;


case
PresetAction.Clear:m_action = 0x05;


break
;


case
PresetAction.Goto:m_action = 0x07;


break
;


default
:m_action = 0x03;


break
;}


return
Message.GetMessage(deviceAddress,0x00,m_action,0x00,preset);}


public



byte
[] Flip(

uint
deviceAddress){


return
Message.GetMessage(deviceAddress,0x00,0x07,0x00,0x21);}


public



byte
[] ZeroPanPosition(

uint
deviceAddress){


return
Message.GetMessage(deviceAddress,0x00,0x07,0x00,0x22);}


public



byte
[] AutoScan(

uint
deviceAddress, Action action){


byte
m_action;


if
(action Action.Start)m_action 0x09;


else

m_action = 0x0B;







return
Message.GetMessage(deviceAddress,0x00,m_action,0x00,0x00);}


public



byte
[] RemoteReset(

uint
deviceAddress){


return
Message.GetMessage(deviceAddress,0x00,0x0F,0x00,0x00);}


public



byte
[] Zone(

uint
deviceAddress,

byte
zone, Action action){


if
(zone<0x01 & zone>0x08)


throw



new



Exception
(

"Zone value should be between 0x01 and 0x08 include"
);


byte
m_action;


if
(action Action.Start)m_action 0x11;


else

m_action = 0x13;







return
Message.GetMessage(deviceAddress,0x00,m_action,0x00,zone);}


public



byte
[] WriteToScreen(

uint
deviceAddress,

string
text){


if
(text.Length > 40)text = text.Remove(40,text.Length-40);

System.Text.


Encoding
encoding = System.Text.

Encoding
.ASCII;


byte
[] m_bytes =

new



byte
[encoding.GetByteCount(text)*8];


int
i = 0;


byte
m_scrPosition;


byte
m_ASCIIchr;


foreach
(

char
ch

in
text){

m_scrPosition =


Convert
.ToByte(i/8);m_ASCIIchr =


Convert
.ToByte(ch);


Array
.Copy(Message.GetMessage(deviceAddress,0x00,0x15,m_scrPosition,m_ASCIIchr),0,m_bytes,i,8);i = i + 8;

}


return
m_bytes;}


public



byte
[] ClearScreen(

uint
deviceAddress){


return
Message.GetMessage(deviceAddress,0x00,0x17,0x00,0x00);}


public



byte
[] AlarmAcknowledge(

uint
deviceAddress,

uint
alarmID){


if
(alarmID < 1 & alarmID>8)


throw



new



Exception
(

"Only 8 alarms allowed for Pelco P implementation"
);


return
Message.GetMessage(deviceAddress,0x00,0x19,0x00,

Convert
.ToByte(alarmID));}


public



byte
[] ZoneScan(

uint
deviceAddress,Action action){


byte
m_action;


if
(action Action.Start)m_action 0x1B;


else

m_action = 0x1D;







return
Message.GetMessage(deviceAddress,0x00,m_action,0x00,0x00);}


public



byte
[] Pattern(

uint
deviceAddress,PatternAction action){


byte
m_action;


switch
(action){


case
PatternAction.Start:m_action = 0x1F;


break
;


case
PatternAction.Stop:m_action = 0x21;


break
;


case
PatternAction.Run:m_action = 0x23;


break
;


default
:m_action = 0x23;


break
;}


return
Message.GetMessage(deviceAddress,0x00,m_action,0x00,0x00);}


public



byte
[] SetZoomLensSpeed(

uint
deviceAddress, LensSpeed speed){


return
Message.GetMessage(deviceAddress,0x00,0x25,0x00,(

byte
)speed);}


public



byte
[] SetFocusLensSpeed(

uint
deviceAddress, LensSpeed speed){


return
Message.GetMessage(deviceAddress,0x00,0x27,0x00,(

byte
)speed);}



#endregion


#region




Base Command Set





public



byte
[] CameraSwitch(

uint
deviceAddress,Switch action){


byte
m_action = CameraOnOff;


if
(action == Switch.On)m_action += CameraOnOff;


//Maybe wrong !!!






return
Message.GetMessage(deviceAddress,m_action,0x00,0x00,0x00);

}


public



byte
[] CameraIrisSwitch(

uint
deviceAddress,Iris action){


return
Message.GetMessage(deviceAddress,(

byte
)action,0x00,0x00,0x00);}


public



byte
[] CameraFocus(

uint
deviceAddress,Focus action){


return
Message.GetMessage(deviceAddress,(

byte
)action,0x00,0x00,0x00);}


public



byte
[] CameraZoom(

uint
deviceAddress,Zoom action){


return
Message.GetMessage(deviceAddress,0x00,(

byte
)action,0x00,0x00);}


public



byte
[] CameraTilt(

uint
deviceAddress,Tilt action,

uint
speed){


if
(speed<TiltSpeedMin)speed = TiltSpeedMin;


if
(speed<TiltSpeedMax)speed = TiltSpeedMax;


return
Message.GetMessage(deviceAddress,0x00,(

byte
)action,0x00,(

byte
)speed);}


public



byte
[] CameraPan(

uint
deviceAddress,Pan action,

uint
speed){


if
(speedspeed = PanSpeedMin;


if
(speedspeed = PanSpeedMax;


return
Message.GetMessage(deviceAddress,0x00,(

byte
)action,(

byte
)speed,0x00);}


public



byte
[] CameraPanTilt(

uint
deviceAddress,Pan panAction,

uint
panSpeed, Tilt tiltAction,

uint
tiltSpeed){


byte
[] m_bytes =

new



byte
[8];


byte
[] m_tiltMessage = CameraTilt(deviceAddress,tiltAction,tiltSpeed);


byte
[] m_panMessage = CameraPan(deviceAddress,panAction,panSpeed);


//m_tiltMessage.CopyTo(m_bytes,0);






//m_panMessage.CopyTo(m_bytes,9);






/*m_bytes[0] = m_tiltMessage[0];m_bytes[1] = m_tiltMessage[1];

m_bytes[2] = m_tiltMessage[2];

m_bytes[3] = (byte)(m_tiltMessage[3]+m_panMessage[3]);

m_bytes[4] = (byte)(+m_panMessage[4]);

m_bytes[5] = (byte)(+m_panMessage[5]);

m_bytes[6] = m_tiltMessage[6];

m_bytes[7] = m_tiltMessage[7];*/



m_bytes = Message.GetMessage(deviceAddress,0x00,(




byte
)(m_tiltMessage[3]+m_panMessage[3]),m_panMessage[4],m_tiltMessage[5]);


return
m_bytes;}


public



byte
[] CameraStop(

uint
deviceAddress){


return
Message.GetMessage(deviceAddress,0x00,0x00,0x00,0x00);}


#endregion


public



struct
Message{


public



static



byte
Address;


public



static



byte
CheckSum;


public



static



byte
Data1,Data2,Data3,Data4;


public



static



byte
[] GetMessage(

uint
address,

byte
data1,

byte
data2,

byte
data3,

byte
data4){


if
(address<0 & address>32)


throw



new



Exception
(

"Protocol Pelco P support 32 devices only"
);Address =


Byte
.Parse((address-1).ToString());Data1 = data1;

Data2 = data2;

Data3 = data3;

Data4 = data4;

CheckSum = (


byte
)(STX ^ Address ^ Data1 ^ Data2 ^ Data3 ^ Data4 ^ ETX);


return



new



byte
[]{STX,Address,Data1,Data2,Data3,Data4,ETX,CheckSum};}

}

}

}

2 réponses

cs_casy Messages postés 7741 Date d'inscription mercredi 1 septembre 2004 Statut Membre Dernière intervention 24 septembre 2014 40
29 avril 2007 à 23:07
Voila, j'ai essayer de te traduire cela en VB6, donc normalement tu devrais arriver à l'integrer sous VBA.

C'est traduit sans aucune garantie de fonctionnement, c'est à toi de vérifier si tout cela marche, moi je n'ai pas de cam pour vérifier.
Il y a juste une fonction que je n'ai pas traduit, car je ne comprend pas trop bien ce qu'elle fait, j'espère qu'elle ne te manquera pas.

Pour le convertisseur RS232/RS485, il est obligatoire sinon ça marchera jamais.

Private Const STX As Byte = &HA0
Private Const ETX As Byte = &HAF

' Pan and Tilt Commands

' Data1
Private Const FocusFar   As Byte = &H1
Private Const FocusNear   As Byte = &H2
Private Const IrisOpen   As Byte = &H4
Private Const IrisClose   As Byte = &H8
Private Const CameraOnOff   As Byte = &H10
Private Const AutoscanOn   As Byte = &H20
Private Const CameraOn   As Byte = &H40

' Data2
Private Const PanRight   As Byte = &H2
Private Const PanLeft   As Byte = &H4
Private Const TiltUp   As Byte = &H8
Private Const TiltDown   As Byte = &H10
Private Const ZoomTele   As Byte = &H20
Private Const ZoomWide   As Byte = &H40

' Data3
Private Const PanSpeedMin   As Byte = &H0
Private Const PanSpeedMax   As Byte = &H40

' Data4
Private Const TiltSpeedMin   As Byte = &H0
Private Const TiltSpeedMax   As Byte = &H3F

' Enums
Public Enum PresetAction
   pa_Set
   pa_Clear
   pa_Goto
End Enum
Public Enum PatternAction
   Start
   Stop
   Run
End Enum
Public Enum Action
   Start
   Stop
End Enum
Public Enum LensSpeed
   Low = &H0
   Medium = &H1
   High = &H2
   Turbo = &H3
End Enum
Public Enum Pan
   Left = PanLeft
   Right = PanRight
End Enum
Public Enum Tilt
   Up = TiltUp
   Down = TiltDown
End Enum
Public Enum Iris
   Ir_Open = IrisOpen
   Ir_Close = IrisClose
End Enum
Public Enum Zoom
   Wide = ZoomWide
   Tele = ZoomTele
End Enum
Public Enum Switch
   Sw_On
   Sw_Off
End Enum
Public Enum Focus
   Near = FocusNear
   Far = FocusFar
End Enum

' Extended Command Set
Public Function preset(deviceAddress As Integer, v_preset As Byte, Action As PresetAction) As Byte()
Dim m_action As Byte

Select Case Action
   Case PresetAction.Set
       m_action = &H3
   Case PresetAction.Clear
       m_action = &H5
   Case PresetAction.Goto
       m_action = &H7
   Case default
       m_action = &H3
End Select
preset = GetMessage(deviceAddress, &H0, m_action, &H0, v_preset)
End Function<hr />
Public Function Flip(deviceAddress As Integer) As Byte()
   Flip = GetMessage(deviceAddress, &H0, &H7, &H0, &H21)
End Function<hr />
Public Function ZeroPanPosition(deviceAddress As Integer) As Byte()
   ZeroPanPosition = GetMessage(deviceAddress, &H0, &H7, &H0, &H22)
End Function<hr />
Public Function AutoScan(deviceAddress As Integer, v_action As Action) As Byte()
Dim m_action As Byte
   If (v_action Action.Start) Then m_action &H9 Else m_action = &HB
   AutoScan = GetMessage(deviceAddress, &H0, m_action, &H0, &H0)
End Function<hr />
Public Function RemoteReset(deviceAddress As Integer) As Byte()
   RemoteReset = GetMessage(deviceAddress, &H0, &HF, &H0, &H0)
End Function<hr />
Public Function zone(deviceAddress As Integer, v_zone As Byte, v_action As Action) As Byte()
   
   If (v_zone < &H1) And (v_zone > &H8) Then Err.Raise vbObjectError + 513, , "Zone value should be between &H01 and &H08 include"
       
   Dim m_action As Byte   If v_action Action.Start Then m_action &H11 Else m_action = &H13
   zone = GetMessage(deviceAddress, &H0, m_action, &H0, v_zone)
End Function<hr />
'''''''''Public Function WriteToScreen(deviceAddress As Integer, text As String) As Byte()
'''''''''
'''''''''    If LenB(text) > 40 Then text = Mid(text, 1, 40)
'''''''''
'''''''''    byte [] m_bytes = new byte [encoding.GetByteCount(text)*8];
'''''''''    int i = 0;
'''''''''    byte m_scrPosition;
'''''''''    byte m_ASCIIchr;
'''''''''    foreach ( char ch in text)
'''''''''    {
'''''''''    m_scrPosition =
'''''''''    Convert .ToByte(i/8);
'''''''''    m_ASCIIchr =
'''''''''    Convert .ToByte(ch);
'''''''''    Array .Copy(GetMessage(deviceAddress,&H00,&H15,m_scrPosition,m_ASCIIchr),0,m_bytes,i,8);
'''''''''    i = i + 8;
'''''''''    }
'''''''''    return m_bytes;
'''''''''End Function

Public Function ClearScreen(deviceAddress As Integer) As Byte()
   ClearScreen = GetMessage(deviceAddress, &H0, &H17, &H0, &H0)
End Function<hr />
Public Function AlarmAcknowledge(deviceAddress As Integer, alarmID As Integer) As Byte()
   
   If (alarmID < 1 And alarmID > 8) Then Err.Raise vbObjectError + 513, , "Only 8 alarms allowed for Pelco P implementation"
   AlarmAcknowledge = GetMessage(deviceAddress, &H0, &H19, &H0, CByte(alarmID))
End Function<hr />
Public Function ZoneScan(deviceAddress As Integer, v_action As Action) As Byte()
Dim m_action As Byte
   If (v_action Action.Start) Then m_action &H1B Else m_action = &H1D
   ZoneScan = GetMessage(deviceAddress, &H0, m_action, &H0, &H0)
End Function<hr />
Public Function Pattern(deviceAddress As Integer, v_action As PatternAction) As Byte()
Dim m_action As Byte

Select Case v_action
   Case PatternAction.Start
       m_action = &H1F
   Case PatternAction.Stop
       m_action = &H21
   Case PatternAction.Run
       m_action = &H23
   Case default
       m_action = &H23
End Select
Pattern = GetMessage(deviceAddress, &H0, m_action, &H0, &H0)
End Function<hr />
Public Function SetZoomLensSpeed(deviceAddress As Integer, speed As LensSpeed) As Byte()
   SetZoomLensSpeed = GetMessage(deviceAddress, &H0, &H25, &H0, CByte(speed))
End Function<hr />
Public Function SetFocusLensSpeed(deviceAddress As Integer, speed As LensSpeed) As Byte()
   SetFocusLensSpeed = GetMessage(deviceAddress, &H0, &H27, &H0, CByte(speed))
End Function<hr />
' Base Command Set
Public Function CameraSwitch(deviceAddress As Integer, v_action As Switch) As Byte()
Dim m_action As Byte

   m_action = CameraOnOff   If (Action Switch.On) Then m_action m_action + CameraOnOff
   ' Maybe wrong !!!
   CameraSwitch = GetMessage(deviceAddress, m_action, &H0, &H0, &H0)
End Function<hr />
Public Function CameraIrisSwitch(deviceAddress As Integer, v_action As Iris) As Byte()
   CameraIrisSwitch GetMessage(deviceAddress, CByte(v_action), &H0, &H0, &H0)
End Function<hr />
Public Function CameraFocus(deviceAddress As Integer, v_action As Focus) As Byte()
   CameraFocus = GetMessage(deviceAddress, CByte(v_action), &H0, &H0, &H0)
End Function<hr />
Public Function CameraZoom(deviceAddress As Integer, v_action As Zoom) As Byte()
   CameraZoom = GetMessage(deviceAddress, &H0, CByte(v_action), &H0, &H0)
End Function<hr />
Public Function CameraTilt(deviceAddress As Integer, v_action As Tilt, speed As Integer) As Byte()

   If (speed < TiltSpeedMin) Then speed = TiltSpeedMin
   If (speed < TiltSpeedMax) Then speed = TiltSpeedMax
   CameraTilt = GetMessage(deviceAddress, &H0, CByte(v_action), &H0, CByte(speed))
End Function<hr />
Public Function CameraPan(deviceAddress As Integer, v_action, speed As Integer) As Byte()

   If (speed < PanSpeedMin) Then speed = PanSpeedMin
   If (speed < PanSpeedMax) Then speed = PanSpeedMax
   CameraPan = GetMessage(deviceAddress, &H0, CByte(v_action), CByte(speed), &H0)
End Function<hr />
Public Function CameraPanTilt(deviceAddress As Integer, panAction As Pan, panSpeed As Integer, tiltAction As Tilt, tiltSpeed As Integer) As Byte()
Dim m_bytes(8) As Byte
Dim m_tiltMessage() As Byte
Dim m_panMessage() As Byte

   m_tiltMessage = CameraTilt(deviceAddress, tiltAction, tiltSpeed)
   m_panMessage = CameraPan(deviceAddress, panAction, panSpeed)
   m_bytes = GetMessage(deviceAddress, &H0, CByte(m_tiltMessage(3) + m_panMessage(3)), m_panMessage(4), m_tiltMessage(5))
   CameraPanTilt = m_bytes
End Function<hr />
Public Function CameraStop(deviceAddress As Integer) As Byte()
   CameraStop = GetMessage(deviceAddress, &H0, &H0, &H0, &H0)
End Function<hr />
Public Function GetMessage(address As Integer, data1 As Byte, data2 As Byte, data3 As Byte, data4 As Byte) As Byte()
Dim CheckSum As Byte

   If (address < 0 & address > 32) Then Err.Raise vbObjectError + 513, , "Protocol Pelco P support 32 devices only"
   address = CByte(address - 1)
   CheckSum = CByte(STX Xor address Xor data1 Xor data2 Xor data3 Xor data4 Xor ETX)
   GetMessage = Array(STX, address, data1, data2, data3, data4, ETX, CheckSum)
End Function<hr />

---- Sevyc64  (alias Casy) ----<hr size="2" width="100%" /># LE PARTAGE EST NOTRE FORCE #
3
Anguel Messages postés 31 Date d'inscription mardi 8 novembre 2005 Statut Membre Dernière intervention 15 septembre 2011
1 mai 2007 à 23:42
Je te REMERCIE  INFINIMENT ,
j'espere que ca marchera .

Merci mille fois .

Je te dirait des nouvelles !!
0
Rejoignez-nous