实例介绍
【实例简介】
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Interop; using System.Windows.Threading; using System.Runtime.InteropServices; using System.Windows.Media.Composition; using System.Windows.Media.Animation; using System.Threading; using System.Net.Sockets; using System.Reflection; using System.Net; using MyCom; using System.IO.Ports; namespace LJKJMedia { /// <summary> /// MediaCtl.xaml 的交互逻辑 /// </summary> public partial class MediaCtl : Window { #region 系统变量 /// <summary> /// 读写配置文件帮助类 /// </summary> private ReaderWriterIni rw; private string m_IsHideTask = "" ; //是否隐藏任务栏 private string m_IsHideMouse = "" ; //是否隐藏鼠标 private string m_AppPath = "" ; private string m_light1_on = string .Empty; private string m_light2_on = string .Empty; private string m_light3_on = string .Empty; private string m_light4_on = string .Empty; private string m_light5_on = string .Empty; private string m_light1_off = string .Empty; private string m_light2_off = string .Empty; private string m_light3_off = string .Empty; private string m_light4_off = string .Empty; private string m_light5_off = string .Empty; private string m_projector_on = string .Empty; private string m_projector_off = string .Empty; private bool light1OnOff = false ; private bool light2OnOff = false ; private bool light3OnOff = false ; private bool light4OnOff = false ; private bool light5OnOff = false ; private bool lightAllOnOff = false ; private string m_light1_name = string .Empty; private string m_light2_name = string .Empty; private string m_light3_name = string .Empty; private string m_light4_name = string .Empty; private string m_light5_name = string .Empty; private string [] projectorCom; private ComHelper com_Light = new ComHelper(); private ComHelper com_Projector = new ComHelper(); private bool bLight; private bool bProjector; UdpClient uc = null ; //声明UDPClient private int m_UdpPort = 19000; private string m_UdpSvrIP = "" ; private Socket m_SvrSocket = null ; private Socket m_ClentSocket = null ; private Thread m_ThreadSocket = null ; private string m_LocalIpAddress = "" ; private int m_LocalPort = 19001; //初始化串口端口 private void InitComm() { //com.myEventRec = new ComHelper.com1_DataReceived(com1RedDev_eventRec); bLight = com_Light.OpenPort_light(); if (!bLight) { MessageBox.Show(com_Light.GetOpenedComLogMsg()); } bProjector = com_Projector.OpenPort_projector(); if (!bProjector) { MessageBox.Show(com_Projector.GetOpenedComLogMsg()); } } #endregion #region 构造函数 public MediaCtl() { InitializeComponent(); } #endregion #region 产品化 private const int SW_HIDE = 0; //隐藏任务栏 private const int SW_RESTORE = 9; //显示任务栏 private void HideMouse() { cls_HideMouse.ShowCursor( false ); } private void ShowMouse() { cls_HideMouse.ShowCursor( true ); } private void HideTask() { cls_HideTask.ShowWindow(cls_HideTask.FindWindow( "Shell_TrayWnd" , null ), SW_HIDE); //隐藏任务栏 cls_HideTask.ShowWindow(cls_HideTask.FindWindow( "Button" , null ), SW_HIDE); //隐藏开始按钮 } private void ShowTask() { cls_HideTask.ShowWindow(cls_HideTask.FindWindow( "Shell_TrayWnd" , null ), SW_RESTORE); //显示任务栏 cls_HideTask.ShowWindow(cls_HideTask.FindWindow( "Button" , null ), SW_RESTORE); //显示开始按钮 } #endregion #region 初始化加载 private void UserControl_Loaded( object sender, RoutedEventArgs e) { try { m_AppPath = AppDomain.CurrentDomain.BaseDirectory; rw = new ReaderWriterIni(GlobalValue.iniPath); m_UdpPort = int .Parse(rw.ReaderIni( "LJKJSetting" , "UdpPort" )); //UDP服务端口号 m_UdpSvrIP = rw.ReaderIni( "LJKJSetting" , "UdpSvrIP" ); //UDP服务ip地址 m_LocalPort = int .Parse(rw.ReaderIni( "LJKJSetting" , "LocalPort" )); //tcp服务端口号 m_LocalIpAddress = rw.ReaderIni( "LJKJSetting" , "LocalIpAddress" ); //tcp服务ip地址 m_IsHideTask = rw.ReaderIni( "LJKJSetting" , "IsHideTask" ); m_IsHideMouse = rw.ReaderIni( "LJKJSetting" , "IsHideMouse" ); m_light1_on = rw.ReaderIni( "lightComSetting" , "light1_on" ); m_light2_on = rw.ReaderIni( "lightComSetting" , "light2_on" ); m_light3_on = rw.ReaderIni( "lightComSetting" , "light3_on" ); m_light4_on = rw.ReaderIni( "lightComSetting" , "light4_on" ); m_light5_on = rw.ReaderIni( "lightComSetting" , "light5_on" ); m_light1_off = rw.ReaderIni( "lightComSetting" , "light1_off" ); m_light2_off = rw.ReaderIni( "lightComSetting" , "light2_off" ); m_light3_off = rw.ReaderIni( "lightComSetting" , "light3_off" ); m_light4_off = rw.ReaderIni( "lightComSetting" , "light4_off" ); m_light5_off = rw.ReaderIni( "lightComSetting" , "light5_off" ); light1.Content = rw.ReaderIni( "LJKJSetting" , "light1_name" ); light2.Content = rw.ReaderIni( "LJKJSetting" , "light2_name" ); light3.Content = rw.ReaderIni( "LJKJSetting" , "light3_name" ); light4.Content = rw.ReaderIni( "LJKJSetting" , "light4_name" ); light5.Content = rw.ReaderIni( "LJKJSetting" , "light5_name" ); lightAll.Content = rw.ReaderIni( "LJKJSetting" , "lightAll_name" ); m_projector_on = rw.ReaderIni( "projectorComSetting" , "projector_on" ); m_projector_off = rw.ReaderIni( "projectorComSetting" , "projector_off" ); if (m_IsHideTask== "1" ) HideTask(); if (m_IsHideMouse == "1" ) HideMouse(); //InitUdp(); StartListen(); InitComm(); //初始化串口端口 } catch (Exception ex) { System.IO.File.AppendAllText( "log.txt" , DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" ) "-MediaCtl--UserControl_Loaded-" ex.Message "\r\n" ); } } #endregion private void Window_Closing( object sender, System.ComponentModel.CancelEventArgs e) { ShowMouse(); ShowTask(); } #region tcp控制 private string GetLoacalIP() { string hostName = Dns.GetHostName(); System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName); for ( int i = 0; i < addressList.Length; i ) { if (addressList[i].IsIPv6LinkLocal == false ) { return addressList[i].ToString(); } } return "" ; } private void StartListen() { try { IPAddress address; m_SvrSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (m_LocalIpAddress == null ) address = IPAddress.Parse(GetLoacalIP()); else address = IPAddress.Parse(m_LocalIpAddress); IPEndPoint endpoint = new IPEndPoint(address, m_LocalPort); m_SvrSocket.Bind(endpoint); m_SvrSocket.Listen(30); m_ThreadSocket = new Thread(StartSocket); m_ThreadSocket.IsBackground = true ; m_ThreadSocket.Start(); } catch { m_SvrSocket.Close(); } } private void StartSocket() { while ( true ) { try { m_ClentSocket = m_SvrSocket.Accept(); if (m_ClentSocket.Connected) { byte [] stream = new byte [80]; int iLen = m_ClentSocket.Receive(stream); string message = System.Text.Encoding.UTF8.GetString(stream, 0, iLen); RecMsg(message.Trim().ToLower()); } } catch { m_ClentSocket.Close(); } } } //处理接收的消息 private void RecMsg( string strRec) { switch (strRec) { case "single|1|light_on" : lightSendCmd(m_light1_on); break ; case "single|2|light_on" : lightSendCmd(m_light2_on); break ; case "single|3|light_on" : lightSendCmd(m_light3_on); break ; case "single|4|light_on" : lightSendCmd(m_light4_on); break ; case "single|5|light_on" : lightSendCmd(m_light5_on); break ; case "single|1|light_off" : lightSendCmd(m_light1_off); break ; case "single|2|light_off" : lightSendCmd(m_light2_off); break ; case "single|3|light_off" : lightSendCmd(m_light3_off); break ; case "single|4|light_off" : lightSendCmd(m_light4_off); break ; case "single|5|light_off" : lightSendCmd(m_light5_off); break ; case "single|alllight_on" : //所有灯光开 关 lightSendCmd(m_light1_on); Thread.Sleep(100); lightSendCmd(m_light2_on); Thread.Sleep(100); lightSendCmd(m_light3_on); Thread.Sleep(100); lightSendCmd(m_light4_on); Thread.Sleep(100); lightSendCmd(m_light5_on); break ; case "single|alllight_off" : lightSendCmd(m_light1_off); Thread.Sleep(100); lightSendCmd(m_light2_off); Thread.Sleep(100); lightSendCmd(m_light3_off); Thread.Sleep(100); lightSendCmd(m_light4_off); Thread.Sleep(100); lightSendCmd(m_light5_off); break ; case "single|projector_on" : projectorCom = m_projector_on.Split( ',' ); for ( int i = 0; i < projectorCom.Length; i ) { projectorSendCmd(projectorCom[i]); Thread.Sleep(100); } break ; case "single|projector_off" : projectorCom = m_projector_off.Split( ',' ); for ( int i = 0; i < projectorCom.Length; i ) { projectorSendCmd(projectorCom[i]); Thread.Sleep(100); } break ; default : break ; } } #endregion #region udp控制 private void InitUdp() { //注意此处端口号要与发送方相同 uc = new UdpClient(m_UdpPort); //开一线程 Thread th = new Thread( new ThreadStart(StartSocketSvr)); //设置为后台 th.IsBackground = true ; th.Start(); } /// <summary> /// 获取本机地址列表 /// </summary> public string GetLocalAddresses() { // 获取主机名 string strHostName = Dns.GetHostName(); // 根据主机名进行查找 IPHostEntry iphostentry = Dns.GetHostEntry(strHostName); string retval = "" ; foreach (IPAddress ipaddress in iphostentry.AddressList) { if (ipaddress.AddressFamily == AddressFamily.InterNetwork) { retval = ipaddress.ToString(); break ; } } return retval; } /// <summary> /// 开启UDP服务 /// </summary> private void StartSocketSvr() { if (m_UdpSvrIP == "" || m_UdpSvrIP == null || m_UdpSvrIP == "-1" ) { m_UdpSvrIP = GetLocalAddresses(); } //声明终结点 IPEndPoint iep = new IPEndPoint(IPAddress.Parse(m_UdpSvrIP), m_UdpPort); while ( true ) { //获得Form1发送过来的数据包 string text = System.Text.Encoding.UTF8.GetString(uc.Receive( ref iep)); Logs.Log.WriteLog( "msg" , "接受到的UDP消息--" text); //Cls_Log.WriteLog("收到消息" text); //this.Dispatcher.Invoke(new Action(() => //{ // lstLog.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") " 收到命令:" text "\r"); //})); switch (text) { case "single|1|light_on" : lightSendCmd(m_light1_on); break ; case "single|2|light_on" : lightSendCmd(m_light2_on); break ; case "single|3|light_on" : lightSendCmd(m_light3_on); break ; case "single|4|light_on" : lightSendCmd(m_light4_on); break ; case "light5_on" : lightSendCmd(m_light5_on); break ; case "single|1|light_off" : lightSendCmd(m_light1_off); break ; case "single|2|light_off" : lightSendCmd(m_light2_off); break ; case "single|3|light_off" : lightSendCmd(m_light3_off); break ; case "single|4|light_off" : lightSendCmd(m_light4_off); break ; case "single|5|light_off" : lightSendCmd(m_light5_off); break ; case "single|projector_on" : projectorCom = m_projector_on.Split( ',' ); for ( int i = 0; i < projectorCom.Length; i ) { projectorSendCmd(projectorCom[i]); Thread.Sleep(100); } break ; case "single|projector_off" : projectorCom = m_projector_off.Split( ',' ); for ( int i = 0; i < projectorCom.Length; i ) { projectorSendCmd(projectorCom[i]); Thread.Sleep(100); } break ; default : break ; } } } #endregion private void Button_Click( object sender, RoutedEventArgs e) { try { switch ((sender as Button).Tag.ToString()) { case "light1" : if (!light1OnOff) { lightSendCmd(m_light1_on); light1OnOff = true ; } else { lightSendCmd(m_light1_off); light1OnOff = false ; } break ; case "light2" : if (!light2OnOff) { lightSendCmd(m_light2_on); light2OnOff = true ; } else { lightSendCmd(m_light2_off); light2OnOff = false ; } break ; case "light3" : if (!light3OnOff) { lightSendCmd(m_light3_on); light3OnOff = true ; } else { lightSendCmd(m_light3_off); light3OnOff = false ; } break ; case "light4" : if (!light4OnOff) { lightSendCmd(m_light4_on); light4OnOff = true ; } else { lightSendCmd(m_light4_off); light4OnOff = false ; } break ; case "light5" : if (!light5OnOff) { lightSendCmd(m_light5_on); light5OnOff = true ; } else { lightSendCmd(m_light5_off); light5OnOff = false ; } break ; case "lightAll" : if (!lightAllOnOff) { lightSendCmd(m_light1_on); Thread.Sleep(100); lightSendCmd(m_light2_on); Thread.Sleep(100); lightSendCmd(m_light3_on); Thread.Sleep(100); lightSendCmd(m_light4_on); Thread.Sleep(100); lightSendCmd(m_light5_on); lightAllOnOff = true ; } else { lightSendCmd(m_light1_off); Thread.Sleep(100); lightSendCmd(m_light2_off); Thread.Sleep(100); lightSendCmd(m_light3_off); Thread.Sleep(100); lightSendCmd(m_light4_off); Thread.Sleep(100); lightSendCmd(m_light5_off); lightAllOnOff = false ; } break ; case "projector_on" : projectorCom = m_projector_on.Split( ',' ); for ( int i = 0; i < projectorCom.Length; i ) { projectorSendCmd(projectorCom[i]); Thread.Sleep(100); } break ; case "projector_off" : projectorCom = m_projector_off.Split( ',' ); for ( int i = 0; i < projectorCom.Length; i ) { projectorSendCmd(projectorCom[i]); Thread.Sleep(100); } break ; case "return" : this .Close(); break ; default : break ; } } catch (Exception ex) { System.IO.File.AppendAllText( "log.txt" , DateTime.Now.ToString( "yyyy-MM-dd HH:mm:ss" ) "-MediaCtl--Image_MouseDown-" ex.Message "\r\n" ); } } /// <summary> /// light灯发送命令 /// </summary> public void lightSendCmd( string cmd) { try { if (cmd == null ) { Logs.Log.WriteLog( "com" , "cmd == null" ); return ; } if (cmd == "" ) { Logs.Log.WriteLog( "com" , "cmd == 空" ); return ; } com_Light.SendMessage(cmd, true ); } catch (Exception e1) { Logs.Log.WriteLog( "error" , "异常执行 ZmdSendCmd()" e1.Message); } } /// <summary> /// projector灯发送命令 /// </summary> public void projectorSendCmd( string cmd) { try { if (cmd == null ) { Logs.Log.WriteLog( "com" , "cmd == null" ); return ; } if (cmd == "" ) { Logs.Log.WriteLog( "com" , "cmd == 空" ); return ; } com_Projector.SendMessage(cmd, true ); } catch (Exception e1) { Logs.Log.WriteLog( "error" , "异常执行 ZmdSendCmd()" e1.Message); } } } } |
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论