Programming the PLC

A PLC project must be programmed in order to use TwinCAT Speech. For a quick start, this is explained below on the basis of this sample.

1. Create a new PLC project.
Programming the PLC 1:
2. Add the Tc3_Speech library.
Programming the PLC 2:
3. Insert the following code from Sample01 into MainTTS.

Declaration part:

PROGRAM MainTTS
VAR
// TTS Trigger Variables
bSpeakTrigger         : BOOL     := FALSE;
bSpeakStopTrigger     : BOOL     := FALSE;
// TTS Command Configuration
nConfigIdTTS          : UINT     := 200;
// Language Id for TTS Output
nLanguageId           : UINT     := 1033;
//Update Code before Release
fbTTS                 : FB_TextToSpeech  := (nConfigurationId := nConfigIdTTS);
fbRetrieveUtterance   : FB_RetrieveUtterance;
// TTS Variables
bSpeak                : BOOL     := FALSE;
{attribute 'TcEncoding':= 'UTF-8'}
sText2Speech          : STRING(4095)  := '<speak>TcSpeech beta demo project is greeting you.</speak>';
bInit                 : BOOL     := FALSE;
{attribute 'hide'}
nLanguageIdOld : UINT := 1033;
END_VAR

Process part:

IF NOT binit THEN
    fbTTS.SetAmsNetAddr(GVL_SpeechDemo.sAmsNetId);
    binit := TRUE;
END_IF
// TTS - Text To Speech
// Trigger Start/Stop Text Output
IF bSpeakTrigger THEN
    bSpeakTrigger :=FALSE;
    bSpeak := TRUE;
END_IF
IF (nLanguageIdOld <> nLanguageId) THEN
    fbRetrieveUtterance(nLanguageId := nLanguageId, sText2Speech => sText2Speech, nOldLanguageId := nLanguageIdOld, sOldText2Speech := sText2Speech);
    nLanguageIdOld := nLanguageId;
END_IF
IF bSpeak THEN // if set manually via bSpeakTrigger OR for answering a recognized ASR command ...
    IF bSpeakStopTrigger THEN
        fbTTS(sUtterance := sText2Speech,bSpeak := FALSE, nConfigurationId:= nConfigIdTTS);
    ELSE
        fbTTS(sUtterance := sText2Speech,bSpeak := TRUE, nConfigurationId:= nConfigIdTTS, nLanguageId:=nLanguageId);
    END_IF    
    IF NOT fbTTS.bBusy THEN
        fbTTS(sUtterance := sText2Speech,bSpeak := FALSE, nConfigurationId:= nConfigIdTTS);
        bSpeak := FALSE;
        bSpeakStopTrigger:=FALSE;
    END_IF
END_IF
4. Set "bSpeak" to TRUE to trigger a speech output via the default device.
The TwinCAT configuration must have been activated beforehand.

The identification number for the TTS configuration that is to be used is present in the code as ConfigIdTTS.