Socket通讯采集推送LED(SER)

来源:互联网 发布:深圳java薪水 编辑:程序博客网 时间:2024/06/01 13:50
xaml:
<Window x:Class="_123.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">    <Grid x:Name="gird" Loaded="Grid_Loaded_1">        <ComboBox Name="com1" HorizontalAlignment="Left" Margin="10,8,0,0" VerticalAlignment="Top" Width="120"/>        <Button Name="comYes" Content="确定" HorizontalAlignment="Left" Margin="88,110,0,0" VerticalAlignment="Top" Width="75"/>        <TextBox  Name="comtextSend" HorizontalAlignment="Left" Height="71" Margin="10,34,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="153"/>        <Button Name="serviceStart" Content="开启服务" HorizontalAlignment="Left" Margin="239,10,0,0" VerticalAlignment="Top" Width="75" Click="serviceStart_Click"/>        <TextBox  Name="textreceive" HorizontalAlignment="Left" Height="71" Margin="239,34,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>        <TextBox  Name="_resendText" HorizontalAlignment="Left" Height="60" Margin="239,165,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>        <ListBox Name="_listBox" HorizontalAlignment="Left" Height="71" Margin="394,34,0,0" VerticalAlignment="Top" Width="100" SelectionChanged="_listBox_SelectionChanged"/>        <Button  Name="_resendBtn" Content="确定" HorizontalAlignment="Left" Margin="284,230,0,0" VerticalAlignment="Top" Width="75" Click="_resendBtn_Click"/>    </Grid></Window>
cs:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;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.Net;using System.Net.Sockets;using System.Windows.Threading;using System.Threading;using LEDLibrary;using System.IO.Ports;namespace _123{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        DispatcherTimer timer = new DispatcherTimer();        private LEDPlayer player;        string mes;        string mesrec;          public MainWindow()        {            InitializeComponent();            timer.Tick+=new EventHandler(timer_Tick);            timer.Interval = TimeSpan.FromSeconds(15);            timer.Start();        }        private void timer_Tick(object sender, EventArgs e)        {            if (com1.SelectedIndex < 0)            {                MessageBox.Show("");                return;            }            player = new LEDPlayer(com1.SelectedValue.ToString());            if (mesrec == null)                mes = this.comtextSend.Text.Trim() + DateTime.Now.ToString();            else                mes = mesrec.ToString() + DateTime.Now.ToString();            if (mes == "")            {                MessageBox.Show("请输入消息");            }            string result = player.DisplayText(mes);            if (result != "")            {                MessageBox.Show(result);            }        }        private void Window_Loaded_1(object sender, RoutedEventArgs e)        {            string[] combox = SerialPort.GetPortNames();            foreach (var item in combox)            {                com1.Items.Add(item);            }        }        Thread threadWacth = null;        Socket serversocket = null;        byte[] bytebox=new byte[1024*2];        Socket clientsocket = null;        Dictionary<string, Socket> socketlist = new Dictionary<string, Socket>();        private void WatchConnection()        {            showMess("等待客户连接");            clientsocket = serversocket.Accept();            Socket policynew = clientsocket;            showMess("连接成功");            _listBox.Dispatcher.Invoke(new Action(() =>                {                    _listBox.Items.Add(policynew.RemoteEndPoint.ToString());                }));            socketlist.Add(policynew.RemoteEndPoint.ToString(), clientsocket);            while (true)            {                int strlong = clientsocket.Receive(bytebox);                mesrec = System.Text.Encoding.UTF8.GetString(bytebox,0,strlong);                showMess(mesrec);            }        }        private void showMess(string msgg)        {            Action action = () => textreceive.AppendText(msgg+"\r\n");            if (System.Threading.Thread.CurrentThread != textreceive.Dispatcher.Thread)            {                textreceive.Dispatcher.Invoke                    (System.Windows.Threading.DispatcherPriority.Normal, action);            }            else             {                action();            }        }        private void serviceStart_Click(object sender, RoutedEventArgs e)        {            startsocket();        }        private void startsocket()        {            serversocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);            IPAddress address = IPAddress.Parse("");            IPEndPoint endpoint = new IPEndPoint(address, int.Parse(""));            serversocket.Bind(endpoint);            serversocket.Listen(10);            threadWacth = new Thread(WatchConnection);            threadWacth.IsBackground = true;            threadWacth.Start();        }        private void _resendBtn_Click(object sender, RoutedEventArgs e)        {            clientsocket.Send(System.Text.Encoding.UTF8.GetBytes(_resendText.Text));            _resendText.Clear();        }          private void _listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)        {            string clientselected = _listBox.SelectedItem.ToString();            clientsocket = socketlist[clientselected];        }        private void Grid_Loaded_1(object sender, RoutedEventArgs e)        {            Grid grid = sender as Grid;            foreach (var child in grid.Children)            {                if (child is ListBox)                {                    if ((child as ListBox).Name == "ClientListbox")                        _listBox = child as ListBox;                }            }        }    }}


0 0
原创粉丝点击