项目使用科星互联 CX-830S-E 语音播报控制,使用 C#中的 Socket对象,连接到语音控制器的Tcp Server端口,然后进行语音播报操作。
一、网络语音设备
二、CX-830S-E设置
三、C#中TcpSocketClient类
class TcpSocketClient
{
public TcpSocketClient()
{
//创建心跳包
tmrRetry.Elapsed += Timer_Elapsed;
tmrRetry.Interval = 2000;
tmrRetry.Enabled = true;
}
//创建负责通信的Socket
Socket sendSocket;
private bool _isConnected = false;
private bool _stop = false;
private int lostConnetTime = 0;
private string _remoteIp;
private int _remotePort;
private System.Timers.Timer tmrRetry = new System.Timers.Timer();
private byte[] heart = new byte[]{0xCC, 0xDD, 0xAA };
// 数据产生时,触发此事件
public delegate void DataReceiveEventHandler(string cardNo);
public event DataReceiveEventHandler DataReceive;
public delegate void ConnectStatusEventHandler(int index, string Msg, int status);
public event ConnectStatusEventHandler ConnectStatus;
public bool IsConnected
{
get
{
return _isConnected;
}
set
{
_isConnected = value;
}
}
public bool Stop
{
get
{
return _stop;
}
set
{
_stop = value;
}
}
public string RemoteIp
{
get
{
return _remoteIp;
}
set
{
_remoteIp = value;
}
}
public int RemotePort
{
get
{
return _remotePort;
}
set
{
_remotePort = value;
}
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Send(heart);
}
public void Send(string sendMessage)
{
try
{
byte[] send = Encoding.GetEncoding("GBK").GetBytes(sendMessage);
sendSocket.Send(send);
}
catch
{
lostConnetTime += 1;
}
try
{
if (lostConnetTime >= 10)
{
ConnectStatus?.Invoke(0, "TTS network voice device reconnection!", 0);
lostConnetTime = 0;
_stop = true;
_isConnected = false;
Thread.Sleep(1000);
if (sendSocket != null)
{
sendSocket.Close();
}
ConnectionRemote();
}
}
catch (Exception ex)
{
ConnectStatus?.Invoke(0, "TTS network voice device reconnection failure:" + ex.Message, -1);
}
}
public void Send(byte[] send)
{
try
{
sendSocket.Send(send);
}
catch
{
lostConnetTime += 1;
}
try
{
if (lostConnetTime >= 10)
{
ConnectStatus?.Invoke(0, "TTS network voice device reconnection!", 0);
lostConnetTime = 0;
_stop = true;
_isConnected = false;
Thread.Sleep(1000);
if (sendSocket != null)
{
sendSocket.Close();
}
ConnectionRemote();
}
}
catch (Exception ex)
{
ConnectStatus?.Invoke(0, "TTS network voice device reconnection failure:" + ex.Message, -1);
}
}
public void ConnectionRemote()
{
try
{
sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Parse(_remoteIp);
IPEndPoint point = new IPEndPoint(ip, _remotePort);
//获取远程服务器的ip地址和端口号
sendSocket.Connect(point);
_isConnected = true;
_stop = false;
//开启一个新的线程不停的接受服务器端发来的消息
Thread th = new Thread(Receive);
th.IsBackground = true;
th.Start();
ConnectStatus?.Invoke(0,String.Format("TTS network voice device: {0}, connection successful!", RemoteIp), 1);
}
catch (Exception ex)
{
_isConnected = false;
_stop = true;
ConnectStatus?.Invoke(0, String.Format("TTS network voice device: {0}, connection failed: {1}", RemoteIp, ex.Message), -1);
}
}
/// <summary>
/// 不停的接受服务器发来的消息
/// </summary>
private void Receive()
{
while (!_stop)
{
try
{
if (sendSocket != null && sendSocket.Available > 0)
{
byte[] buffer = new byte[40960];
int r = sendSocket.Receive(buffer);//实际接受的有效字节数
if (r == 0)
{
break;
}
string recMsg = "";
if (buffer[0] == 165)
{
byte[] heartBeat = new byte[r];
Buffer.BlockCopy(buffer, 0, heartBeat, 0, r);
sendSocket.Send(heartBeat);
}
else if (buffer[0] == 0xCC) {
//test online return
}
else
{
recMsg = Encoding.UTF8.GetString(buffer, 0, r);
DataReceive?.Invoke(recMsg);
}
}
}
catch (Exception ex)
{
ConnectStatus?.Invoke(0, String.Format("TTS network voice device: {0}, receiving data failed: {1}", RemoteIp, ex.Message), -1);
}
}
}
public void StopServer()
{
try
{
tmrRetry.Stop();
tmrRetry.Enabled = false;
tmrRetry.Close();
tmrRetry.Dispose();
_stop = true;
Thread.Sleep(2000);
sendSocket.Close();
ConnectStatus?.Invoke(0, String.Format("TTS network voice device: {0} closed successfully!", RemoteIp), -1);
}
catch (Exception ex)
{
ConnectStatus?.Invoke(0, String.Format("TTS network voice device: {0} shutdown failed: {1}", RemoteIp, ex.Message), -1);
}
}
}
四、调用方法
1、初始化
showSystemLog为自定义的消息显示,你可以自定义该 函数。
Loghelper为自定义的Log操作类
ttsVoice = new TcpSocketClient();
ttsVoice.ConnectStatus += TtsVoice_ConnectStatus;
ttsVoice.DataReceive += TtsVoice_DataReceive;
ttsVoice.RemoteIp = GlobalVariable.NetworkIp;
ttsVoice.RemotePort = GlobalVariable.NetworkPort;
ttsVoice.ConnectionRemote();
if (ttsVoice.IsConnected)
{
showSystemLog(String.Format("TTS网络语音设备:{0}:{1},连接成功!", ttsVoice.RemoteIp, ttsVoice.RemotePort), 0, LogType.System);
}
else
{
showSystemLog(String.Format("TTS网络语音设备:{0}:{1},连接失败!", ttsVoice.RemoteIp, ttsVoice.RemotePort), 0, LogType.System);
}
private void TtsVoice_DataReceive(string _status)
{
try
{
LogHelper.WriteInfo("TTS网络语音返回数据:" + _status);
}
catch (Exception ex)
{
showSystemLog("TTS网络设备返回数据:" + _status + "。失败:" + ex.Message, 0, LogType.System);
}
}
private void TtsVoice_ConnectStatus(int index, string Msg, int status)
{
showSystemLog(String.Format("{0}#,TTS网络语音:{1},状态:{2}", index + 1, Msg, status), 0, LogType.System);
}
2、语音播放
if (ttsVoice != null)
{
//GlobalVariable.NetworkTTSVolume为声音大小1-10,sVoiceContent为播放内容
ttsVoice.Send(String.Format("#[v{0}]{1}", GlobalVariable.NetworkTTSVolume, sVoiceContent));
}
3、设备释放可以放到 XXX_FormCloseing事件中
if (ttsVoice != null)
{
ttsVoice.Stop = true;
ttsVoice.ConnectStatus -= TtsVoice_ConnectStatus;
ttsVoice.DataReceive -= TtsVoice_DataReceive;
Thread.Sleep(200);
ttsVoice.StopServer();
ttsVoice = null;
}