1、 string Temp = null; string Temp_memoSend = null; private void memoSend_EditValueChanged(object sender, EventArgs e) public Form1() InitializeComponent(); sp1.DataReceived += sp1_DataReceived; private void Form1_Load(object sender, EventArgs e) this.MaximizeBox = false; /检查是否有串口 string str = SerialP
2、ort.GetPortNames(); if (str = null) MessageBox.Show(本机没有串口!, error); return; /添加串口项目 foreach (string port in System.IO.Ports.SerialPort.GetPortNames() cbSerial.Properties.Items.Add(port); /串口设置默认选择项 cbSerial.SelectedIndex = 0; Control.CheckForIllegalCrossThreadCalls = false; sp1.DataReceived += new
3、SerialDataReceivedEventHandler(sp1_DataReceived); /订阅委托 private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e) private void OpenSerial_Click(object sender, EventArgs e) sp1.Close(); /SerialPort sp1 = new SerialPort(); if (!sp1.IsOpen)/串口是关闭的,设置参数,打开串口 try /获取串口号 string serialNam
4、e = cbSerial.SelectedItem.ToString(); sp1.PortName = serialName; /设置各参数 string strBaudRate = cbBaudRate.Text; string strDateBits = cbDataBits.Text; string strStopBits = cbStop.Text; sp1.BaudRate = Convert.ToInt32(strBaudRate);/波特率 sp1.DataBits = Convert.ToInt32(strDateBits);/数据位 switch (cbStop.Text)
5、 /停止位 case 1: sp1.StopBits = StopBits.One; break;1.52 sp1.StopBits = StopBits.Two; default:Error:参数不正确!Error switch (cbParity.Text) /校验位无 sp1.Parity = Parity.None;奇校验 sp1.Parity = Parity.Odd;偶校验 sp1.Parity = Parity.Even; if (sp1.IsOpen = true) sp1.Open(); /btnOpen.Enabled = false; labSerial.Text = 串
6、口号: + sp1.PortName; labState.Text = 状 态:打开; catch (System.Exception ex) + ex.Message, /private void CloseSerial_Click(object sender, EventArgs e) / / if (sp1.IsOpen) / / sp1.Close(); / labSerial.Text = / labState.Text = 关闭 / / private void Send_Click(object sender, EventArgs e) string StrSend = memo
7、Send.Text;sp1.IsOpen)请先打开串口! else if (rbtnSendStr.Checked) /字符串发送 if (ckTimeSend.Checked & !(string.IsNullOrEmpty(textTime.Text)/判断是否定时发送数据 string Time = textTime.Text; tmSend.Interval = int.Parse(Time) * 1000;/时间单位是秒,化成毫秒单位 tmSend.Start(); /开启定时器 sp1.Write(StrSend); /写入数据 else /十六进制发送(string.IsNull
8、OrEmpty(textTime.Text) string StrSend_1 = StrSend.Trim(); /去掉前后的空格 /string StrSend_2 = StrSend_1.Replace( /string StrSend_3 = string.Join(, Regex.Matches(StrSend_2, .|.).Cast().ToList(); string StrArray = StrSend_1.Split( /用空格符隔开字符串数组 var Str_Send = new List(); / int byteBufferLength = StrArray.Leng
9、th; /数组StrArray的长度 for (int i = 0; i StrArray.Length; i+) if (StrArrayi.Trim() = ) /byteBufferLength-; continue; if (StrArrayi.Length 3) Str_Send.Add(StrArrayi); Str_Send.AddRange(Devide(StrArrayi); int byteBufferLength = Str_Send.Count; byte byteBuffer = new bytebyteBufferLength; int ii = 0; Str_Se
10、nd.Count; /Byte bytesOfStr = Encoding.Default.GetBytes(StrArrayi); int decNum = 0; /if (StrArrayi = / continue; /else /decNum = Convert.ToInt32(Str_Sendi, 16); /把字符串转成16进制数 /byteBufferii = Convert.ToByte(decNum); decNum = Convert.ToInt32(Str_Sendi, 16); byteBufferii = Convert.ToByte(decNum);输入错误!提示
11、ii+; sp1.Write(byteBuffer, 0, byteBuffer.Length);/从第0个开始写入byteBuffer,长度bytebuffer的长度。 private List s.Value.ToString(); return ret; private void tmSend_Tick(object sender, EventArgs e) if (rbtnSendStr.Checked) void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e) byte byteRead = new byt
12、esp1.BytesToRead; if (rbtnRceStr.Checked) memoRcv.Text += sp1.ReadExisting(); memoRcv.SelectionStart = memoRcv.Text.Length; memoRcv.ScrollToCaret(); if (rbtnRce16.Checked) Byte receivedData = new Bytesp1.BytesToRead; /创建接收字节数组 sp1.Read(receivedData, 0, receivedData.Length); string strRcv = null; rec
13、eivedData.Length; strRcv += receivedDatai.ToString(X2 memoRcv.Text += strRcv; private void Stop_Click(object sender, EventArgs e) /退出接收数据 tmSend.Stop(); private void Clear_Click(object sender, EventArgs e) /清空接收数据 memoRcv.Text = private void Time_Press(object sender, KeyPressEventArgs e)Char.IsLette
14、rOrDigit(e.KeyChar) e.Handled = true; e.Handled = false; private void TextTime_KeyPress(object sender, KeyPressEventArgs e) /限制定时发送时间只能输入数字(Char.IsNumber(e.KeyChar) & e.KeyChar != (char)8)请输入数字 private void memoSend_KeyPress(object sender, KeyPressEventArgs e) private void btnClose_Click(object send
15、er, EventArgs e) /关闭串口 if (sp1.IsOpen) private void Send16_KeyPress(object sender, KeyPressEventArgs e) /string Send16 = memoRcv.Text; /byte buff = new byteSend16.Length / 2; /int index = 0; /for (int i = 0; Send16.Length; i += 2) / buffindex = Convert.ToByte(Send16.Substring(i, 2), 16); / +index; /
16、string result = Encoding.Default.GetString(buff); / MessageBox.Show(result); /memoRcv.Text = result; private void SendStr_KeyPress(object sender, KeyPressEventArgs e) /string SendStr = memoRcv.Text; /byte ba = System.Text.ASCIIEncoding.Default.GetBytes(SendStr); /StringBuilder sb = new StringBuilder
17、(); /foreach (byte b in ba) / sb.Append(b.ToString(x); / MessageBox.Show(sb.ToString(); /memoRcv.Text = sb.ToString(); private void rbtnSend16_CheckedChanged(object sender, EventArgs e) StateChanged_16 = 1; StateChanged_Ascii = 0; if (Temp_memoSend=StrSend.Trim()& Flag = 1) memoSend.Text = Temp; Fla
18、g = 0; memoSend.Text = foreach (char S in StrSend) memoSend.Text += (byte)S).ToString( private void rbtnSendStr_CheckedChanged(object sender, EventArgs e) if (StateChanged_16 = 1) Flag = 1; StateChanged_16 = 0; StateChanged_Ascii = 1; Temp = memoSend.Text; /去掉前后的空格 if (StrArray.Length = 0) for (int j = 0; j = 48 & decNum = 65 &= 90 | decNum = 97 &= 122)/除了数字和字母,其他都转成问号 byteBufferii = Convert.To
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1