Xcom
窗体:
(groupBox组合框,comboBox下拉框)
xcom代码:
namespace _01_作业
{// 1kb 1024B 1200B// 1MB public partial class Form1 : Form{public List<string> botelv = new List<string> { "600","1200", "2400", "4800", "9600","14400", "19200", "28800","38400", "43000", "57600","76800","115200","128000","230400","256000","460800","921600","1382400", "自定义" };public List<string> tizhiwei = new List<string>() { "1", "1.5", "2" };public List<string> shujuwei = new List<string>() { "5", "6", "7", "8" };public List<string> xiaojianwei = new List<string>() { "None", "Odd", "Even"};public List<string> xiaoshi = new List<string>() { "16禁止(HEX)", "字符显示(ASCII)" };SerialPort port;public Form1(){InitializeComponent();initPort();// 初始化串口的方法comboBox2.DataSource = botelv; // 初始化波特率 所有波特率显示下拉框里面 comboBox3.DataSource = tizhiwei;comboBox4.DataSource = shujuwei;comboBox5.DataSource = xiaojianwei;comboBox6.DataSource = xiaoshi;comboBox2.Text = "9600";comboBox4.Text = "8";port = new SerialPort();port.DataReceived += Port_DataReceived;port.DtrEnable = true;port.RtsEnable = true;}private void Port_DataReceived(object sender, SerialDataReceivedEventArgs e){byte[] bs = new byte[port.BytesToRead];// 读取缓存区长度port.Read(bs, 0, bs.Length);string value = null;this.Invoke((EventHandler)delegate{if (comboBox6.Text == "16进制(HEX)"){// 16进制显示foreach (var item in bs){value += "" + item.ToString("X");}}else{value = Encoding.GetEncoding("gb2312").GetString(bs);}richTextBox1.Text = value + "\t\n";richTextBox1.SelectionStart = richTextBox1.Text.Length;richTextBox1.ScrollToCaret();port.DiscardInBuffer();// 清空缓存区});}void initPort(){comboBox1.DataSource = null;// 给下拉框添加数据源// 获取电脑下所有的串行端口 GetPortNames()string[] names = SerialPort.GetPortNames();if (names.Length != 0){comboBox1.DataSource = names;}else{MessageBox.Show("未扫描到串口");}}// 检测当前电脑串口行端口private void button1_Click(object sender, EventArgs e){initPort();}private void button2_Click(object sender, EventArgs e){if (port.IsOpen == true){port.Close();button2.Text = "打开串口";comboBox1.Enabled = comboBox2.Enabled = comboBox3.Enabled = comboBox4.Enabled = comboBox5.Enabled = comboBox6.Enabled = true;}else{port.Open();// 打开串口comboBox1.Enabled = comboBox2.Enabled = comboBox3.Enabled = comboBox4.Enabled = comboBox5.Enabled = comboBox6.Enabled = false;button2.Text = "关闭串口";}}private void button3_Click(object sender, EventArgs e){byte[] bs = Encoding.UTF8.GetBytes(this.richTextBox2.Text);port.Write(bs, 0, bs.Length);}}
}
跳转连接
创建两个窗体
From1 一个button按钮
namespace _02_作业
{public partial class Form1 : Form{UdpClient udp;public List<string> ip = new List<string> {"224.0.0.188","224.0.1.188"}; public Form1(){InitializeComponent();}// 跳转private void button2_Click(object sender, EventArgs e){Form2 f2 = new Form2(udp,ip);f2.ShowDialog();}}
}
From2
namespace _02_作业
{public partial class Form2 : Form{UdpClient udpclient;List<string> list;public Form2(UdpClient udp,List<string> ip){InitializeComponent();udpclient = udp;list = ip;TianJia();}public void TianJia() {this.listBox1.Items.Clear();this.listBox1.Items.AddRange(list.ToArray());this.Refresh();}// 添加组播private void button1_Click(object sender, EventArgs e){if (textBox1.Text == ""){MessageBox.Show("数据为空!");}else{if (list.Contains(textBox1.Text)){MessageBox.Show("数据重复");}else{list.Add(textBox1.Text);TianJia();}}}// 删除组播private void button2_Click(object sender, EventArgs e){if (list.Contains(textBox1.Text)){list.RemoveAt(list.IndexOf(textBox1.Text));TianJia();}else{MessageBox.Show("数据不存在");}}// 打开private void button3_Click(object sender, EventArgs e){udpclient = new UdpClient(new IPEndPoint(IPAddress.Any, 8080));strartReceive();}private void button4_Click(object sender, EventArgs e){byte[] bs = Encoding.UTF8.GetBytes(this.textBox2.Text);udpclient.Send(bs, bs.Length, this.textBox1.Text, 8080);}private void button5_Click(object sender, EventArgs e){udpclient.JoinMulticastGroup(IPAddress.Parse(this.textBox1.Text));}async void strartReceive(){while (true){UdpReceiveResult body = await udpclient.ReceiveAsync();BeginInvoke((Action)(() =>{richTextBox1.AppendText(body.RemoteEndPoint.ToString() + ":" + Encoding.UTF8.GetString(body.Buffer) + "\t\n");richTextBox1.ScrollToCaret();}));}}}
}