unity开发展厅中控系统 | 乐文库-ag九游会登录j9入口
文章目录 前言一、硬件环境二、整体构造1.灯光控制模块2.主中控模块3.电脑主机的开关机控制4.控制电视机的开关 前言 记录自己独自完成的一个完整的展厅中控系统。整体实现思路:灯光线路接上网络继电器,网络继电器接上串口服务器,串口服务器接上路由器,电脑接上路由器,华为paid连接路由器无线网络,实现所有设备连接在一个局域网内。 中控的主要功能: 1.控制灯光的开关 2.控制电脑开关机 3.控制电视机的开机和关机 4.控制内容: a、ppt的上下翻页(有动态效果的) b、视频的暂停、播放、停止 c、系统声音的加减 一、硬件环境
交换机:光电信息转换 路由器:创建局域网络,连接不同的网络设备 ac控制器:管理所有ap设备(无线wifi) 串口服务器:可以直连传统串口设备(不带ip的模块),通过网络协议发送指令给模块 时序电源:提送稳定可控的电源(可通过ip或串口控制各个电源开闭) 网络继电器:通过网络控制灯光开关 电脑:每台电脑作为单独的服务端 电视机:这里用的是电视机作为显示器 华为paid:安卓系统,作为客户端给每台电脑发送指令
二、整体构造 1.灯光控制模块
灯光连接智能照明模块(其实就是一个继电器),智能照明模块再与串口服务器连接
实现:串口服务器作为服务端,自己开发一个客户端,通过网络协议(我这里是udp和端口号)连接到串口服务器上,发送指令,实现智能照明模块的开口 开闭合控制灯光的开关
注意事项:智能照明模块的波特率要与串口服务器的波特率设置一致,网络协议(在串口服 务器内也可设置)要确定好(udp或者是tcp); 确定好智能照明模块的指令(每个模块都有自己的指令,说明书或者供应商提供)
代码如下:
using system;using system.collections;using system.collections.generic;using system.net;using system.net.sockets;using system.text;using system.threading;using unityengine;public class sendinstructionstolamp : monobehaviour{ public socket socket; public int _port = 9600; public string _ip = "192.168.0.7"; void start() { connectlight(); } /// /// 通过tcp协议连接智能照明模块(灯组控制部分) /// public void connectlight() { try { //创建客户端socket,获得远程ip和端口号 socket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); ipaddress ip = ipaddress.parse(_ip); ipendpoint point = new ipendpoint(ip, _port); socket.connect(point); debug.log("连接成功!"); } catch (exception) { debug.log("ip或者端口号错误..."); } } bool quankai; list instructions_temp; /// /// 给智能照明模块发送指令(灯组控制部分) /// /// 发送指令的list集合 /// 发送指令的时间间隔 public void sendinstructions(list instructions, float timespan) { startcoroutine(sendinstructions_timespan(instructions, timespan)); } ienumerator sendinstructions_timespan(list instructions, float timespan) { for (int k = 0; k < 10; k )//这里为什么要做一个循环发送指令,是因为在实际测试中发现,发送一两次指令不稳定,多发送几次确保指令能被网络继电器接收到 { for (int i = 0; i < instructions.count; i ) { byte[] b = new byte[8]; socket.send(instructions[i]); yield return new waitforseconds(0.1f); } } yield return new waitforseconds(timespan); //stopallcoroutines(); } //连接关闭 void socketquit() { socket.close(); } private void onapplicationquit() { debug.log(123456); socketquit(); } /// /// 一键照明指令 /// /// true为全开、false为全关 public void one_keylightswitch(bool b) { if (b) { list list_byte1 = new list(); byte[] bt1 = new byte[8] { 0x04, 0x06, 0x00, 0x02, 0x00, 0x01, 0xe9, 0x9f }; list_byte1.add(bt1); sendinstructions(list_byte1, 0); debug.log("照明全开!"); } else { list list_byte2 = new list(); byte[] bt1 = new byte[8] { 0x04, 0x06, 0x00, 0x01, 0x00, 0x00, 0xd8, 0x5f }; list_byte2.add(bt1); sendinstructions(list_byte2, 0); debug.log("照明全关!"); } }}
2.主中控模块
代码如下:
客户端
using unityengine;using system;//using system.io.ports;using system.net.sockets;using system.net;using system.threading;using system.text;using unityengine.ui;public class spsend : monobehaviour{ public static spsend _instance; public toggle[] toggles; public toggle toggle1; public socket socketsend; socket socketsend_tcp_udp; string ip; private void awake() { _instance = this; } private void start() { bt_connect_click("192.168.0.101"); } public qiehuanvideoppt_1 qiehuanvideoppt_1; public void send_udp_string(string s) { byte[] b = encoding.ascii.getbytes(s.tochararray()); socketsend_tcp_udp.send(b); } public void bt_connect_click(string _ip) { if (socketsend != null) socketsend = null; try { int _port = 8888; socketsend = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); ipaddress ip = ipaddress.parse(_ip); ipendpoint point = new ipendpoint(ip, _port); socketsend.connect(point); debug.log("连接成功!"); } catch (exception) { debug.log("ip或者端口号错误..."); } } public void sendmassagetoclient(string s) { //string s = "u:video1.mp4"; debug.log(s); byte[] b = encoding.ascii.getbytes(s.tochararray()); socketsend.send(b); //socketsend.close(); }}
服务端
using unityengine;using system.collections;using system.net;using system.net.sockets;using system.text;using system.threading;using unityengine.video;using renderheads.media.avprovideo;public class udpserver : monobehaviour{ public static udpserver _instance; private void awake() { _instance = this; } /// /// 模拟按键 按键对应表:http://www.doc88.com/p-895906443391.html /// /// 虚拟键值 /// 0 /// 0为按下,1按住,2释放 /// 0 [system.runtime.interopservices.dllimport("user32.dll", entrypoint = "keybd_event")] public static extern void keybd_event(byte bvk, byte bscan, int dwflags, int dwextrainfo); //控制键盘事件 public void keybdwait(byte t) { keybd_event(t, 0, 0, 0); keybd_event(t, 0, 2, 0); } public string ipaddress = "192.168.0.10"; public int connectport = 8888; public string recvstr; public readconfigjson configjson; public videoplayer videoplayer; public mediaplayer mediaplayer; public soundmanager soundmanager; socket socket; endpoint clientend; ipendpoint ipend; string sendstr; byte[] recvdata = new byte[1024]; byte[] senddata = new byte[1024]; int recvlen; thread connectthread; bool isplayer,baohu=true; //初始化 void initsocket() { ipend = new ipendpoint(ipaddress.parse(ipaddress), connectport); socket = new socket(addressfamily.internetwork, sockettype.dgram, protocoltype.udp); socket.bind(ipend); //定义客户端 ipendpoint sender = new ipendpoint(ipaddress.any, 0); clientend = (endpoint)sender; print("等待连接数据"); //开启一个线程连接 connectthread = new thread(new threadstart(socketreceive)); connectthread.start(); } public void socketsend(string sendstr) { senddata = new byte[1024]; senddata = encoding.utf8.getbytes(sendstr); socket.sendto(senddata, senddata.length, socketflags.none, clientend); } //服务器接收 void socketreceive() { while (true) { recvdata = new byte[1024]; recvlen = socket.receivefrom(recvdata, ref clientend); recvstr = encoding.utf8.getstring(recvdata, 0, recvlen); debug.log("收到得信息 " recvstr); //控制object要放在主线程里 控制系统虚拟按键要放在线程里——切记切记 if (recvstr == "f5") { //recvstr = ""; keybdwait(116); } else if (recvstr == "prve") { //recvstr = ""; keybdwait(37); } else if (recvstr == "next") { //recvstr = ""; keybdwait(39); } else if (recvstr == "frist") { // recvstr = ""; windowmod._instance.setfrist("中控播放器"); } //else if (recvstr == "stop"|| recvstr.contains(".mp4")) //{ // keybdwait(27); //} } } //连接关闭 void socketquit() { //关闭线程 if (connectthread != null) { connectthread.interrupt(); connectthread.abort(); } //最后关闭socket if (socket != null) socket.close(); debug.logwarning("断开连接"); } // use this for initialization void start() { ipaddress = ipmanager.getip(addressfam.ipv4); connectport = configjson.configinfo.port; initsocket(); //在这里初始化server toolcontroltaskbar.hidetaskbar(); } public void playerinstruction() { if (recvstr == "frist") { recvstr = ""; windowmod._instance.setfrist("zkplayer"); killprocess("powerpoint 幻灯片放映 - [ppt1.pptx]"); } else if (recvstr.contains(".ppsx")&&baohu) { baohu = false; isplayer = false; killprocess("powerpnt.exe"); functioncontrol._instance.shiping.setactive(false); //videoplayer.stop(); //videoplayer.targettexture.release(); mediaplayer.stop(); application.open; //startcoroutine(waitlookforppt()); //windowmod._instance.setfrist(recvstr " - powerpoint"); recvstr = ""; baohu = true; } else if (recvstr.contains(".mp4")) { isplayer = true; mediaplayer.m_videopath = application.streamingassetspath "/mp4/" recvstr; //mediaplayer. //videoplayer.url = application.streamingassetspath "/mp4/" recvstr; //videoplayer.play(); mediaplayer.openvideofromfile(mediaplayer.filelocation.relativetostreamingassetsfolder, mediaplayer.m_videopath); mediaplayer.play(); recvstr = ""; functioncontrol._instance.shiping.setactive(true); windowmod._instance.setfrist("zkplayer"); killprocess("powerpnt.exe"); } else if (recvstr == "play"&&isplayer) { recvstr = ""; //videoplayer.play(); mediaplayer.play(); } else if (recvstr == "pause") { //videoplayer.pause(); mediaplayer.pause(); recvstr = ""; } else if (recvstr == "stop") { isplayer = false; //videoplayer.stop(); //videoplayer.targettexture.release(); mediaplayer.stop(); windowmod._instance.setfrist("zkplayer"); functioncontrol._instance.shiping.setactive(false); killprocess("powerpnt.exe"); recvstr = ""; } //else if (recvstr!=null && recvstr.contains(".mp4")) //{ // videoplayer.url= application.streamingassetspath "/mp4/" recvstr; // videoplayer.stop(); // videoplayer.play(); // recvstr = ""; //} if (recvstr == "volumeup") { soundmanager.systemvolumeup(); recvstr = ""; } else if (recvstr == "volumedown") { soundmanager.systemvolumedown(); recvstr = ""; } else if (recvstr == "close") { closecomputer(); recvstr = ""; } } private void update() { playerinstruction(); if (screen.width != readconfigjson._instance.configinfo.screenwidth) { screen.setresolution(readconfigjson._instance.configinfo.screenwidth, readconfigjson._instance.configinfo.screenheigth, true); } } void onapplicationquit() { socketquit(); toolcontroltaskbar.showtaskbar(); } public void closecomputer() { system.diagnostics.process p = new system.diagnostics.process(); p.startinfo.filename = "cmd.exe"; p.startinfo.arguments = "/c " "shutdown -s -t 0"; p.start(); } /// /// 杀死进程 /// /// 应用程序名 void killprocess(string processname) { system.diagnostics.process[] processes = system.diagnostics.process.getprocesses(); foreach (system.diagnostics.process process in processes) { debug.log(process.processname); try { if (!process.hasexited) { if (process.processname == processname) { process.kill(); unityengine.debug.log("已杀死进程"); } } } catch (system.invalidoperationexception) { //unityengine.debug.log("holy batman we've got an exception!"); } } }}
本来视频播放用的是unity自带组件videoplayer,但是展厅的主机没有独立显卡,视频播放起来会卡顿,于是便换成了avpro。
3.电脑主机的开关机控制
开机:通过网络给电脑发开机指令 注意事项:你的电脑允许网络唤醒,在boss主板设置勾选允许网络唤醒 网卡驱动也要设置允许网络唤醒
如果你发现你的你没有电源管理,请更新网卡驱动
网络唤醒代码
using system;using system.collections;using system.collections.generic;using system.diagnostics;using system.net;using system.net.sockets;using system.text;using system.text.regularexpressions;using unityengine;public class wakeupcomputer : monobehaviour{ //通过正则表达式设定mac地址筛选标准,关于正则表达式请自行百度 const string maccheckregexstring = @"^([0-9a-fa-f]{2})(([/s:-][0-9a-fa-f]{2}){5})$"; private static readonly regex maccheckregex = new regex(maccheckregexstring); //唤醒主要逻辑方法 public static bool wakeup(string mac) { //查看该mac地址是否匹配正则表达式定义,(mac,0)前一个参数是指mac地址,后一个是从指定位置开始查询,0即从头开始 if (maccheckregex.ismatch(mac, 0)) { byte[] macbyte = formatmac(mac); wakeupcore(macbyte); return true; } return false; } private static void wakeupcore(byte[] mac) { //发送方法是通过udp udpclient client = new udpclient(); //broadcast内容为:255,255,255,255.广播形式,所以不需要ip client.connect(ipaddress.broadcast, 50000); //下方为发送内容的编制,6遍“ff” 17遍mac的byte类型字节。 byte[] packet = new byte[17 * 6]; for (int i = 0; i < 6; i ) packet[i] = 0xff; for (int i = 1; i <= 16; i ) for (int j = 0; j < 6; j ) packet[i * 6 j] = mac[j]; //唤醒动作 client.send(packet, packet.length); } private static byte[] formatmac(string macinput) { byte[] mac = new byte[6]; string str = macinput; //消除mac地址中的“-”符号 string[] sarray = str.split('-'); //mac地址从string转换成byte for (var i = 0; i < 6; i ) { var bytevalue = convert.tobyte(sarray[i], 16); mac[i] = bytevalue; } return mac; } public void button_click_wakeup(string s) { wakeup(s); //print(wakeup("4a-bb-6d-61-75-ae")); //print(wakeup("e4-3a-6e-36-38-aa")); }}
关机:发送指令给电脑关机 注意事项:关机没啥主意事项,很简单 关机代码:
public void closecomputer() { system.diagnostics.process p = new system.diagnostics.process(); p.startinfo.filename = "cmd.exe"; p.startinfo.arguments = "/c " "shutdown -s -t 0"; p.start();}
4.控制电视机的开关
开关机:rs232红外学习模块 串口服务器, 实现:串口服务器做服务端,rs232红外学习模块与串口服务器直连,自己开发客户端通过网络协议(我这里是udp和端口号)连接到串口服务器上,发送指令给服务端,从而控制rs232红外学习模块。 (连接方式和设置同智能照明模块一样,接线可能有所区别) 注意事项:红外学习模块学习电视机的开关机频率,同时设置指令; 学习的时候,电视遥控器对准红外学习指示灯,按住遥控器开机键,直到红外学习指示灯 快速连闪3下,学习结束
本文来自网络,不代表乐文库立场,如若转载,请注明出处:https://www.lewenku.com/?p=452419