如下說明均以心恬科技的XT-106控制卡做爲硬件基礎設備。網絡
與LED顯示屏通信,第一步是創建鏈接(咱們稱之爲Hello)。工具
網絡通信協議方式爲UDP,能夠本身定義UDP數據包結構,也能夠利用系統提供的Socket工具來訪問LED顯示屏。spa
準備好硬件(顯示屏配置成功)以後就能夠試圖經過代碼來與顯示屏創建鏈接了。線程
首先進行UDP的組包,咱們以C#爲例:調試
public void Hello() { byte[] data = new byte[256]; data[0] = 0xF2; data[1] = 0xFF; for (int i = 0; i < 8; i++) { data[2 + i] = 0xFF; } IPAddress Adrr = IPAddress.Parse("255.255.255.255"); // IPAddress Adrr = IPAddress.Parse("239.0.0.1"); IPEndPoint ipep = new IPEndPoint(Adrr, 9888); Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newsock.EnableBroadcast = true; newsock.SendTo(data, ipep); newsock.Close(); }
關於廣播地址,視網絡環境而定。調試代碼過程當中注意防火牆攔截,如使用WinPcap自行組包即可以忽略防火牆。code
下面,創建一個線程來監聽LED顯示屏返回的Hello數據包。blog
Thread nthread=null; nthread = new Thread(new ThreadStart(ListenStart)); nthread.IsBackground = true; nthread.Start(); public void ListenStart() { UdpClient udp = new UdpClient(9889); udp.EnableBroadcast = true; IPEndPoint AllIPEndPoint = new IPEndPoint(IPAddress.Any, 9889); do { byte[] mesg = udp.Receive(ref AllIPEndPoint); if (mesg[0] == 0xF2 && mesg[1] == 0xFF) { Dispatcher.Invoke((Action)delegate { // Do Something ... }); // Do Something ... } mesg = new byte[] { }; } while (true); }
發送至LED顯示屏的UDP數據端口爲9888,LED顯示屏返回至設備的UDP數據端口爲9889.ip