private void Timer1_Tick(object sender, EventArgs e) { if (m_CharNums > 0) { if (m_strLineNum >= m_strAll.Length) { Timer1.Enabled = false; return; } string strsend; if (m_strAll.Length - m_strLineNum < m_CharNums) { strsend = m_pHead + m_strAll.Substring(m_strLineNum) + m_pTail; } else { strsend = m_pHead + m_strAll.Substring(m_strLineNum, m_CharNums) + m_pTail; } m_strLineNum += m_CharNums; ComName.Write(strsend); } else { if (m_strLineNum >= m_strarr.Length) { Timer1.Enabled = false; return; } string strsend = m_pHead + m_strarr[m_strLineNum] + m_pTail; m_strLineNum++; ComName.Write(strsend); } }
private void BtnComOpen_Click(object sender, EventArgs e) { Dictionary <string, Parity> ComParity = new Dictionary <string, Parity>() { { "N", Parity.None }, { "O", Parity.Odd }, { "E", Parity.Even }, { "M", Parity.Mark }, { "S", Parity.Space } }; Dictionary <int, StopBits> ComStopBits = new Dictionary <int, StopBits>() { { 0, StopBits.None }, { 1, StopBits.One }, { 2, StopBits.Two }, { 3, StopBits.OnePointFive } }; string[] strComSet = TxtComSet.Text.Split(','); ComName.BaudRate = Convert.ToInt32(strComSet[0]); ComName.Parity = ComParity[strComSet[1]]; ComName.DataBits = Convert.ToInt32(strComSet[2]); ComName.StopBits = ComStopBits[Convert.ToInt32(strComSet[3])]; if (BtnComOpen.Text == "打开") { BtnComOpen.Text = "关闭"; ComName.PortName = TxtComPortName.Text; if (!ComName.IsOpen) { ComName.Open(); } LabelComStatus.Text = "串口已打开"; } else { BtnComOpen.Text = "打开"; //MyComPort.PortName = textBox2.Text; if (ComName.IsOpen) { ComName.Close(); } LabelComStatus.Text = "串口已关闭"; Timer1.Enabled = false; } }