Je voulais apporter une interface vocale à la programmation TARGET (un beep pour signifier l'appui d'une touche, ou une phrase pous signifier une commande par exemple)
Vu que l'interface Target est un "service" windows, toutes les fonctions Windows ne sont pas accessibles, donc je dois obligatoirement passer par un programme tierce pour apporter les fonctions vocales. En glanant les informations par ci par là j'ai fait un serveur TCP en autoit http://www.autoitscript.com/site/autoit/
et programmer le client TCP dans le TARGET (j'ai choisi le port 1000 par exemple pour le dialogue)
je vous mets le source et en piece jointe la version compilée x86 du programme en autoit (facile à mettre en oeuvre)
il faut lancer le serveur en premier puis le script target
partie Serveur en Autoit:
Code : Tout sélectionner
#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.10.2
Author: myName
Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
; Script Start - Add your code below here
TCPStartup()
Dim $port = 1000, $connection=-1, $listen = TCPListen('127.0.0.1', $port), $pingTimer, $pongTimer, $awaitPong = False
; Build a GUI, with a simple log,to Show Incoming Events
GUICreate('Server', 500, 200, Default, Default, 0x10C80000)
$log = GUICtrlCreateList('', 0, 0, 500, 205)
AddLog('Server is running on Port '&$port)
;[OBJECTS]
Global $voice = ObjCreate("SAPI.SpVoice")
$oMyError = ObjEvent("AutoIt.Error","_COMError")
While GUIGetMsg()<>-3
; Handle Connection Requests (Only one Client at one Time)
If $connection < 0 Then
$connection = TCPAccept($listen)
If $connection>=0 Then AddLog('Client Connected!')
Else
#region PacketHandling
$rcv = TCPRecv($connection, 1024)
Switch $rcv
Case ''
; Do Nothing
Case '+BYE'
AddLog("Client disconnected")
ExitLoop
Case Else
AddLog($rcv)
ParseInput($rcv)
EndSwitch
#endregion
EndIf
WEnd
TCPShutdown()
Func AddLog($msg)
GUICtrlSetData($log, @HOUR&':'&@MIN&':'&@SEC&':'&@MSEC&' - '&$msg)
EndFunc
Func ParseInput(ByRef $t)
Local $f=StringSplit($t,":")
Switch $f[1]
Case 'Beep'
Beep($f[2], $f[3])
Case 'Say'
$Voice.Speak($f[2])
EndSwitch
EndFunc
Func _COMError()
;~ Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !" & @CRLF & @CRLF & _
;~ "err.description is: " & @TAB & $oMyError.description & @CRLF & _
;~ "err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _
;~ "err.number is: " & @TAB & hex($oMyError.number,8) & @CRLF & _
;~ "err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _
;~ "err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _
;~ "err.source is: " & @TAB & $oMyError.source & @CRLF & _
;~ "err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _
;~ "err.helpcontext is: " & @TAB & $oMyError.helpcontext _
;~ )
Local $err = $oMyError.number
If $err = 0 Then $err = -1
Debug("Com Error", "Description: " & $oMyError.description, "ErrNumber: " & $oMyError.number)
SetError($err) ; to check for after this function returns
EndFunc
Code : Tout sélectionner
include "target.tmh"
//program startup
int p_socket;
int main()
{
if(Init(&EventHandle)) return 1; // declare the event handler, return on error
InitSocket(); // function defined below in this script
// RegisterGameCallback(1000, &TCPCallback);
MapKey(&Joystick, TG1, EXEC("SendSocket(\"Say:bonjour à tous\");")); //Say:Phrase = TG1
MapKey(&Joystick, S3, EXEC("SendSocket(\"Beep:2000:500\");")); //Beep:frequency:duration = S3
MapKey(&Joystick, S4, EXEC("CloseAll(\"+BYE\");")); // send +BYE at TCP port 1000 when Joystick S4 is pressed (Ferme proprement TCP et le serveur)
}
//event handler
int EventHandle(int type, alias o, int x)
{
DefaultMapping(&o, x);
}
//int TCPCallback(int buf, int size) // receives an integer at TCP port 1000 and print it
//{
// Map(&buf, buf);
// printf("Received data: %d\xa", buf);
//}
// ----------------------------------------------------- socket stuff --------------------------------
// structures required by the socket library
struct WSAData
{
word wVersion;
word wHighVersion;
char szDescription[257];
char szSystemStatus[257];
word iMaxSockets;
word iMaxUdpDg;
int lpVendorInfo;
}
struct sockaddr_in
{
word sin_family;
word sin_port;
char sin_addr_s_b1;
char sin_addr_s_b2;
char sin_addr_s_b3;
char sin_addr_s_b4;
char sin_zero[8];
}
// socket functions
int WSAStartup(word version, int data){ Map(&WSAStartup, GetProcAddress(ws2_32, "WSAStartup")); return WSAStartup(version, data); }
int WSACleanup(){ Map(&WSACleanup, GetProcAddress(ws2_32, "WSACleanup")); return WSACleanup(); }
int socket(int af, int type, int protocol){ Map(&socket, GetProcAddress(ws2_32, "socket")); return socket(af, type, protocol); }
int closesocket(int s){ Map(&closesocket, GetProcAddress(ws2_32, "closesocket")); return closesocket(s); }
int connect(int socket, int name, int namelen){ Map(&connect, GetProcAddress(ws2_32, "connect")); return connect(socket, name, namelen); }
int send(int socket, int buf, int len, int flags){ Map(&send, GetProcAddress(ws2_32, "send")); return send(socket, buf, len, flags); }
int ws2_32;
WSAData data;
sockaddr_in addr = {2, 0xe803, 127, 0, 0, 1}; // AF_INET, port 1000 swapped, localhost IP 1000 = 03E8 -> 0xe803)
int InitSocket()
{
ws2_32 = LoadLibrary("ws2_32.dll");
WSAStartup(2, &&data);
p_socket = socket(2, 1, 6); // AF_INET, SOCK_STREAM, IPPROTO_TCP
if(connect(p_socket, &&addr, sizeof(&addr)))
{
printf("Cannot connect to TCP port 1000\xa");
closesocket(p_socket);
}
}
int SendSocket(alias data)
{
printf(" %s,%d\xa",&data,strlen(&data));
send(p_socket, &&data, strlen(&data), 0);
}
int CloseAll(alias data)
{
printf("Close ALL\xa");
send(p_socket, &&data, strlen(&data), 0);
closesocket(p_socket);
WSACleanup();
FreeLibrary(ws2_32);
}