找传奇、传世资源到传世资源站!

C# 串口数据传送(适合新手)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Threading;using System.IO.Ports;//新增加using System.Net;//新增加using System.IO;//新增加using System.Text.RegularExpressions;//新增加??namespace AGV{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { ToolTip a = new ToolTip(); a.ShowAlways = true; a.SetToolTip(this.button1, "要显示的文字"); comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());//检查有哪些COM口 } public struct myStruct//全局变量 { } private void WriteDataToNet(myStruct info) { myclass.Dt106 = myclass.Dt106 1; myclass.Dt106 ; textBox11.Text = myclass.Dt106.ToString(); } public class myclass //全局变量 { public static int Dt100;// public static int Dt101; public static int Dt102; public static int Dt103; public static int Dt104; public static int Dt105; public static int Dt106; public static bool Sm100; public static bool Sm101; public static bool Sm102; public static bool Sm103; public static bool Sm104; public static bool Sm105; public static bool Sm106; public static int Tix0; public static int Tix1; public static int Tix2; public static int Tix3; public static int Tix4; public static int Tix5; public static int Tix6; } private void button1_Click(object sender, EventArgs e) { try { if (comboBox1.Text != "") { if (button1.Text == "打开") { serialPort1.PortName = comboBox1.Text; //提取com口号 button1.Text = "已打开"; serialPort1.Open(); //打开串口 button1.BackColor = Color.SpringGreen; //serialPort1.BaudRate = 19200; // serialPort1.DataBits = 8; //停止为8 comboBox1.BackColor = Color.SpringGreen; } else if (button1.Text == "已打开") { button1.Text = "打开"; serialPort1.Close();//关闭COM口 button1.BackColor = Color.WhiteSmoke; comboBox1.BackColor = Color.WhiteSmoke; } } else if (comboBox1.Text == "") { MessageBox.Show("未选择COM口"); } } catch { MessageBox.Show("1"); } } private void button3_Click(object sender, EventArgs e) { } private void button4_Click(object sender, EventArgs e) { if (button1.Text == "已打开") { if (textBox1.Text != "") { string str = textBox1.Text; //由文本框读入想要发送的数据 this.serialPort1.Write(str); //发送数据 } } else if (button1.Text == "打开") { MessageBox.Show("COM未打开"); } } private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { Thread.Sleep(1); //10毫秒 int DataLength = serialPort1.BytesToRead; //字节长度ReadByte ReadChar int DT0 = serialPort1.ReadByte(); //第一个字符ASILL码 string DataLength2 = serialPort1.ReadExisting(); //读取所有数字 char DataLength1 = (char)DT0; //将ASILL码转为STRING this.textBox2.Invoke ( new MethodInvoker ( //MethodInvoker 是不带参数的委托。EventHandler 是可带参数的委托。 delegate { this.textBox2.AppendText(DateTime.Now.ToString() " " DataLength1 DataLength2 "\r\n");//输出到主窗口文本控件 this.textBox3.Text = (DateTime.Now.ToString() DataLength1 DataLength2); this.textBox4.Text = (DataLength1 DataLength2); } ) ); } private void button5_Click(object sender, EventArgs e) { if (button1.Text == "已打开") { if (button5.Text == "访问") { button5.BackColor = Color.SpringGreen; button5.Text = "访问中"; textBox5.Text = "开始"; } else if (button5.Text == "访问中") { button5.BackColor = Color.WhiteSmoke; button5.Text = "访问"; textBox5.Text = "结束"; } } } private void timer1_Tick(object sender, EventArgs e) { try { if (textBox5.Text == "开始") { textBox5.Text = "17601"; } if (button5.Text == "访问中") { Byte[] BY = new Byte[8]; //建立临时字节数组对象 BY[0] = Byte.Parse(this.textBox5.Text.Substring(0, 3)); //由文本框读入想要发送的数据 BY[1] = Byte.Parse(this.textBox5.Text.Substring(3, 2)); // this.serialPort1.Write(BY, 0, 8);//发送数据 } if (textBox5.Text == "开始") { textBox5.Text = "17601"; } else if (textBox5.Text == "17601") { textBox5.Text = "17602"; } else if (textBox5.Text == "17602") { textBox5.Text = "17603"; } else if (textBox5.Text == "17603") { textBox5.Text = "17604"; } else if (textBox5.Text == "17604") { textBox5.Text = "17605"; } else if (textBox5.Text == "17605") { textBox5.Text = "17606"; } else if (textBox5.Text == "17606") { textBox5.Text = "17607"; } else if (textBox5.Text == "17607") { textBox5.Text = "17608"; } else if (textBox5.Text == "17608") { textBox5.Text = "17609"; } else if (textBox5.Text == "17609") { textBox5.Text = "17616"; } else if (textBox5.Text == "17616") { textBox5.Text = "17617"; } else if (textBox5.Text == "17617") { textBox5.Text = "17618"; } else if (textBox5.Text == "17618") { textBox5.Text = "17619"; } else if (textBox5.Text == "17619") { textBox5.Text = "17620"; } else if (textBox5.Text == "17620") { textBox5.Text = "17621"; } else if (textBox5.Text == "17621") { textBox5.Text = "17622"; } else if (textBox5.Text == "17622") { textBox5.Text = "17623"; } else if (textBox5.Text == "17623") { textBox5.Text = "17624"; } else if (textBox5.Text == "17624") { textBox5.Text = "17625"; } else if (textBox5.Text == "17625") { textBox5.Text = "17601"; } } catch { } } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { string m1,m2; m1 = comboBox2.Text; m2 = comboBox3.Text; textBox1.Text = m1 m2; } private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) { string m1,m2; m1= comboBox2.Text; m2=comboBox3.Text; textBox1.Text = m1 m2; } private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox4.Text == "0.5") { timer1.Interval = 500; } else if (comboBox4.Text == "0.7") { timer1.Interval = 700; } else if (comboBox4.Text == "0.9") { timer1.Interval = 900; } else if (comboBox4.Text == "1.1") { timer1.Interval = 1100; } else if (comboBox4.Text == "1.3") { timer1.Interval = 1300; } else if (comboBox4.Text == "1.5") { timer1.Interval = 1500; } } private void textBox4_TextChanged_1(object sender, EventArgs e) { if (textBox4.Text != "") { string d0; d0 = this.textBox4.Text.Substring(0, 4); if (d0 == "B001") { textBox6.Text = textBox4.Text; myclass.Dt100 = 0; myclass.Sm100 = true; } else if (d0 == "B002") { textBox7.Text = textBox4.Text; myclass.Sm101 = true; } else if (d0 == "B003") { textBox8.Text = textBox4.Text; myclass.Sm102 = true; } else if (d0 == "B004") { textBox9.Text = textBox4.Text; myclass.Sm103 = true; } else if (d0 == "B005") { textBox10.Text = textBox4.Text; myclass.Sm104 = true; } } } private void textBox6_TextChanged(object sender, EventArgs e) { if (textBox6.Text != "") { string d0, d1; d0 = this.textBox6.Text.Substring(0, 4); d1 = this.textBox6.Text.Substring(4, 2); if (d0 == "B001") { button6.Visible = true; }} else if (textBox6.Text == "") { button6.Visible = false; } } private void button7_Click(object sender, EventArgs e) { } private void timer2_Tick(object sender, EventArgs e) { if (myclass.Sm100 == true) { textBox26.Text = myclass.Dt100.ToString(); if (textBox4.Text.Substring(0, 4) != "B001") { myclass.Dt100 = myclass.Dt100 1; } if (myclass.Dt100 == 20) { textBox26.Text = ""; textBox6.Text = ""; myclass.Sm100 = false; myclass.Dt100 = 0; myclass.Dt101 = 0; } if (textBox6.Text != "") { if (textBox6.Text.Substring(0, 4) == "B001")//是否是A1车 { string Agv1 = this.textBox6.Text.Substring(4, 2);//AGV状态 switch (Agv1) {case "02": myclass.Dt101 = myclass.Dt101 1; string dt10 = textBox6.Text.Substring(6, 2);//AGV位置 int dt11 = Int32.Parse(dt10); //SRI转为INT dt11 = myclass.Dt101 dt11; //AGV位置 button6.Location = new System.Drawing.Point(dt11, 32);//坐标更改 y.x break;case "200": MessageBox.Show("d0不等于100"); break; } } } } } private void button2_Click_1(object sender, EventArgs e) { //this.Close();//只关闭当前窗口 // Application.Exit();//强制所有消息中止 // Application.ExitThread();//强制所有消息中止 System.Environment.Exit(0);//所有的都会退出 } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复