BroadcastModule.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BroadcastModule
  10. {
  11. public class BroadcastBoard
  12. {
  13. private UdpClient DUCP_NetSocket;
  14. private IPAddress DeviceIP;
  15. private IPEndPoint RPoint = null;
  16. private IPEndPoint TPoint = null;
  17. private int volume = 10;
  18. //*********************************************** 动态引用dll ******************************************
  19. //实现协议栈数据输出回调函数
  20. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  21. unsafe delegate byte cbDUCP_DataOutType(byte* Data, int Size);
  22. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  23. extern static void MB_STK_SetOutCallback(cbDUCP_DataOutType cb);
  24. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  25. unsafe extern static sbyte MB_STK_In(byte* Data, int Size);
  26. //回调对象
  27. private cbDUCP_DataOutType cbDuCP_dataout;
  28. //回调函数实现
  29. unsafe byte cbDUCP_DataOut(byte* Data, int Size)
  30. {
  31. sbyte Ret;
  32. byte[] buff = new byte[256];
  33. byte[] RxBuff = new byte[256];
  34. for (int i = 0; i < Size; i++)
  35. {
  36. buff[i] = Data[i];
  37. }
  38. /*发送数据到显示屏*/
  39. DUCP_NetSocket.Send(buff, Size, TPoint);
  40. /*接收显示屏回复的数据*/
  41. RxBuff = DUCP_NetSocket.Receive(ref RPoint);
  42. /*将显示屏的回复的数据传输给协议栈*/
  43. unsafe
  44. {
  45. fixed (byte* pArray = RxBuff)
  46. Ret = MB_STK_In(pArray, RxBuff.Length);
  47. }
  48. return (byte)Ret;
  49. }
  50. //调节音量
  51. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  52. unsafe extern static byte DUCP_HOST_SYS_SetVol(int vol);
  53. //播放语音
  54. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  55. unsafe extern static byte DUCP_HOST_TTS_Play(byte* pText, byte Opt);
  56. //停止播放语音
  57. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  58. unsafe extern static byte DUCP_HOST_TTS_Stop();
  59. //重启系统
  60. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  61. unsafe extern static byte DUCP_HOST_SYS_Reset();
  62. //显示已存文件
  63. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  64. unsafe extern static byte DUCP_HOST_TWIN_DisFile(int WinID, int FileID);
  65. //显示文本
  66. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  67. unsafe extern static byte DUCP_HOST_TWIN_DisText(int WinID,
  68. byte* pText,
  69. int EnterMode,
  70. int EnterSpeed,
  71. int DelayMode,
  72. int DelayTime,
  73. int EixtMode,
  74. int EixtSpeed,
  75. int FontIndex,
  76. int TextColor,
  77. int BkColor,
  78. int DisTimes);
  79. //创建文本控件
  80. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  81. extern static byte DUCP_HOST_TWIN_Create(int WinID, int Stile, int x0, int y0, int xSize, int ySize);
  82. //删除文本控件中文本文件
  83. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  84. extern static byte DUCP_HOST_TWIN_DelFile(int WinID, int FileID);
  85. //删除文本控件
  86. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  87. extern static byte DUCP_HOST_TWIN_Del(int WinID);
  88. //删除图片控件
  89. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  90. extern static byte DUCP_HOST_PIC_Del(int WinID);
  91. //设置绘图颜色
  92. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  93. extern static byte DUCP_HOST_2D_SetColor(int Color);
  94. //填充矩形
  95. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  96. extern static byte DUCP_HOST_2D_FillRect(int x0, int y0, int x1, int y1);
  97. //绘制系统自带位图
  98. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  99. extern static byte DUCP_HOST_2D_DrawSysIcon(int x0, int y0, int Num, int Denom, int IconIndex);
  100. //创建图片控件
  101. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  102. extern static byte DUCP_HOST_PIC_Create(int WinID, int x0, int y0, int xSize, int ySize);
  103. //图片控件追加图片
  104. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  105. extern static byte DUCP_HOST_PIC_Add(int WinID, int FID, byte EnterMode, byte EnterSpeed,
  106. byte StopMode, byte StopTime, byte ExitMode, byte ExitSpeed);
  107. //更新本地时间
  108. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  109. extern static byte DUCP_HOST_SYS_UpdateTime(short Year, byte Month, byte Day, byte Week, byte Hour, byte Minute, byte Second);
  110. //更新端口(更新ip方法异常,手动更新)
  111. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  112. unsafe extern static byte DUCP_HOST_SYS_SetPort(int port);
  113. //下载文本文件并与文本控件关联
  114. [DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
  115. unsafe extern static byte DUCP_HOST_TWIN_DownTextFile(int WinID,
  116. int FileID,
  117. byte* pText,
  118. int EnterMode,
  119. int EnterSpeed,
  120. int DelayMode,
  121. int DelayTime,
  122. int EixtMode,
  123. int EixtSpeed,
  124. int FontIndex,
  125. int TextColor,
  126. int BkColor);
  127. //************************************************ 方法块 **************************************************
  128. //构造函数
  129. public BroadcastBoard(string ip, int port, int volume = 10)
  130. {
  131. //初始化网络连接配置
  132. DeviceIP = IPAddress.Parse(ip);
  133. RPoint = new IPEndPoint(DeviceIP, port);
  134. TPoint = new IPEndPoint(DeviceIP, port);
  135. //设置协议栈的回调函数
  136. unsafe
  137. {
  138. cbDuCP_dataout = new cbDUCP_DataOutType(cbDUCP_DataOut);
  139. MB_STK_SetOutCallback(cbDuCP_dataout);
  140. }
  141. //GC.KeepAlive(cbDuCP_dataout);
  142. /*#2.创建UDP网络套接字*/
  143. DUCP_NetSocket = new UdpClient();
  144. this.volume = volume;
  145. DUCP_HOST_SYS_SetVol(volume);
  146. //Refresh();
  147. }
  148. public void UpdateTime()
  149. {
  150. DateTime dt = DateTime.Now;
  151. DUCP_HOST_SYS_UpdateTime((short)dt.Year, (byte)dt.Month, (byte)dt.Day, (byte)dt.DayOfWeek, (byte)dt.Hour, (byte)dt.Minute, (byte)dt.Second);
  152. }
  153. /// <summary>
  154. /// 刷新屏幕
  155. /// </summary>
  156. public void Refresh()
  157. {
  158. DUCP_HOST_TWIN_Del(0);
  159. DUCP_HOST_TWIN_Del(1);
  160. DUCP_HOST_TWIN_Del(2);
  161. DUCP_HOST_TWIN_Del(3);
  162. DUCP_HOST_TWIN_Create(0, 0, 0, 0, 64, 16);
  163. DUCP_HOST_TWIN_Create(1, 0, 0, 16, 64, 16);
  164. DUCP_HOST_TWIN_Create(2, 0, 0, 32, 64, 16);
  165. DUCP_HOST_TWIN_Create(3, 0, 0, 48, 64, 16);
  166. }
  167. /// <summary>
  168. /// 显示特定行文字
  169. /// </summary>
  170. /// <param name="winID"></param>
  171. /// <param name="str"></param>
  172. /// <returns></returns>
  173. public bool DispString(int winID, string str, int time)
  174. {
  175. byte[] buffer = Encoding.Default.GetBytes(str);
  176. if (winID < 0 || winID > 3) { return false; }
  177. unsafe
  178. {
  179. fixed (byte* pArray = buffer)
  180. {
  181. try
  182. {
  183. if (time >= 0 && time <= 255)
  184. {
  185. DUCP_HOST_TWIN_DisText(winID, pArray,
  186. 0X15, 1,
  187. 0X00, time,
  188. 0X15, 1,
  189. 3, 0Xff, 0X00, 0);
  190. }
  191. else
  192. {
  193. DUCP_HOST_TWIN_DisText(winID, pArray,
  194. 0X15, 1,
  195. 0X02, 255,
  196. 0X15, 1,
  197. 3, 0Xff, 0X00, 0);
  198. }
  199. }
  200. catch (Exception ex) { Console.WriteLine(ex.Message); return false; }
  201. }
  202. }
  203. return true;
  204. }
  205. /// <summary>
  206. /// 显示多行文字
  207. /// </summary>
  208. /// <param name="strs"></param>
  209. /// <returns></returns>
  210. public bool DispString(string[] strs, bool refresh)
  211. {
  212. bool normal = true;
  213. for (int i = 0; i < 4; i++)
  214. {
  215. normal = DispString(i, strs[i], 0);
  216. if (!normal)
  217. {
  218. break;
  219. }
  220. }
  221. return normal;
  222. }
  223. /// <summary>
  224. /// 配置需循环显示的文字,将导致存储器擦写,请勿频繁调用
  225. /// </summary>
  226. /// <param name="winID"></param>
  227. /// <param name="str"></param>
  228. public void DownloadString(int winID, string str, int index)
  229. {
  230. unsafe
  231. {
  232. byte[] Buff;
  233. if (str.Equals("time"))
  234. {
  235. Buff = Encoding.Default.GetBytes("`H:`N:`S");
  236. fixed (byte* pArray = Buff)
  237. DUCP_HOST_TWIN_DownTextFile(winID,
  238. index >= 0 ? (index <= 15 ? index : 15) : 0,
  239. pArray,
  240. 0X15,
  241. 1,
  242. 0X08,
  243. 255,
  244. 0X15,
  245. 1,
  246. 3,
  247. 0XFF,
  248. 0);
  249. }
  250. else if (str.Equals("date"))
  251. {
  252. Buff = Encoding.Default.GetBytes("`M月`D日");
  253. fixed (byte* pArray = Buff)
  254. DUCP_HOST_TWIN_DownTextFile(winID,
  255. index >= 0 ? (index <= 15 ? index : 15) : 0,
  256. pArray,
  257. 0X15,
  258. 1,
  259. 0X09,
  260. 255,
  261. 0X15,
  262. 1,
  263. 3,
  264. 0XFF,
  265. 0);
  266. }
  267. else
  268. {
  269. Buff = Encoding.Default.GetBytes(str);
  270. fixed (byte* pArray = Buff)
  271. DUCP_HOST_TWIN_DownTextFile(winID,
  272. index >= 0 ? (index <= 15 ? index : 15) : 0,
  273. pArray,
  274. 0X15,
  275. 1,
  276. 0X02,
  277. 255,
  278. 0X15,
  279. 1,
  280. 3,
  281. 0XFF,
  282. 0);
  283. }
  284. }
  285. }
  286. /// <summary>
  287. /// 配置需循环显示的文字,将导致存储器擦写,请勿频繁调用。写入多个字符串
  288. /// </summary>
  289. /// <param name="winID"></param>
  290. /// <param name="strs"></param>
  291. public void DownloadString(int winID, string[] strs)
  292. {
  293. for (int i = 0; i < Math.Min(16, strs.Length) && strs[i] != ""; i++)
  294. {
  295. DownloadString(winID, strs[i], i);
  296. }
  297. }
  298. /// <summary>
  299. /// 显示已载入文本文件
  300. /// </summary>
  301. /// <param name="winID"></param>
  302. /// <param name="fieldID"></param>
  303. public void DispDownStr(int winID, int fieldID)
  304. {
  305. DUCP_HOST_TWIN_DisFile(winID, fieldID);
  306. }
  307. /// <summary>
  308. /// 播放语音
  309. /// </summary>
  310. /// <param name="str"></param>
  311. public void AudioPlay(string str)
  312. {
  313. unsafe
  314. {
  315. try
  316. {
  317. byte[] Buff = System.Text.Encoding.Default.GetBytes(str);
  318. fixed (byte* pArray = Buff)
  319. {
  320. DUCP_HOST_TTS_Stop();
  321. DUCP_HOST_TTS_Play(pArray, 0x01);
  322. }
  323. }
  324. catch (Exception ex)
  325. { Console.WriteLine(ex.Message); }
  326. }
  327. }
  328. /// <summary>
  329. /// 音量控制
  330. /// </summary>
  331. /// <param name="offset"></param>
  332. public void VolumeControl(int offset, out int vol)
  333. {
  334. volume += offset;
  335. DUCP_HOST_SYS_SetVol(volume);
  336. vol = volume;
  337. }
  338. public void VolumeControl(int vol)
  339. {
  340. DUCP_HOST_SYS_SetVol(volume >= 0 ? (volume <= 50 ? volume : 50) : 0);
  341. }
  342. /// <summary>
  343. /// 重启机器
  344. /// </summary>
  345. public void Restart()
  346. {
  347. DUCP_HOST_SYS_Reset();
  348. }
  349. /// <summary>
  350. /// 删除已下载文本文件
  351. /// </summary>
  352. /// <param name="winID"></param>
  353. /// <param name="startIndex"></param>
  354. /// <param name="endIndex"></param>
  355. public void DelFile(int winID, int startIndex, int endIndex)
  356. {
  357. if (winID >= 0 && winID < 4)
  358. {
  359. for (int i = (startIndex >= 0 ? startIndex : 0); i < (endIndex >= 15 ? 15 : endIndex); i++)
  360. {
  361. DUCP_HOST_TWIN_DelFile(winID, i);
  362. }
  363. }
  364. }
  365. /// <summary>
  366. /// CRC16
  367. /// </summary>
  368. /// <param name="data">要进行计算的数组</param>
  369. /// <returns>计算后的数组</returns>
  370. private static byte[] CRC16(byte[] crcbuf)
  371. {
  372. int crc = 0xffff;
  373. int len = crcbuf.Length;
  374. byte[] redata = new byte[2];
  375. for (int n = 0; n < len; n++)
  376. {
  377. byte i;
  378. crc = crc ^ crcbuf[n];
  379. for (i = 0; i < 8; i++)
  380. {
  381. int TT;
  382. TT = crc & 1;
  383. crc = crc >> 1;
  384. crc = crc & 0x7fff;
  385. if (TT == 1)
  386. {
  387. crc = crc ^ 0xa001;
  388. }
  389. crc = crc & 0xffff;
  390. }
  391. }
  392. redata[1] = (byte)((crc >> 8) & 0xff);
  393. redata[0] = (byte)((crc & 0xff));
  394. return redata;
  395. }
  396. /// <summary>
  397. /// 配置ip与端口
  398. /// </summary>
  399. /// <param name="ip"></param>
  400. /// <param name="port"></param>
  401. public void IpConfig(string ip, int port)
  402. {
  403. IPAddress ipa = null;
  404. try
  405. {
  406. ipa = IPAddress.Parse(ip);
  407. if (ipa != null)
  408. {
  409. unsafe
  410. {
  411. try
  412. {
  413. byte[] protocol = new byte[] {
  414. 0x00,0x64,0xff,0xff,0x08,0x04,
  415. 0x00,0x00,0x00,0x00,
  416. 0xE6,0x17
  417. };
  418. byte[] Buff = ipa.GetAddressBytes();
  419. Buff.CopyTo(protocol, 6);
  420. byte[] temp = new byte[10];
  421. Array.Copy(protocol, temp, 10);
  422. byte[] crc = CRC16(temp);
  423. Array.Copy(crc, 0, protocol, 10, 2);
  424. fixed (byte* pArray = protocol)
  425. {
  426. cbDUCP_DataOut(pArray, 12);
  427. }
  428. }
  429. catch (Exception ex)
  430. { Console.WriteLine(ex.Message); }
  431. }
  432. DUCP_HOST_SYS_SetPort(port);
  433. }
  434. }
  435. catch (Exception ex) { Console.WriteLine(ex.Message); }
  436. }
  437. }
  438. }