当点击发送时,就卡住不能操作了 是哪的问题啊
下面是完整版代码。 //定义串口对象
SerialPort myCom = new SerialPort();
public static int RX = 0;
public static int TX = 0;
//定义动态字符串,用来存储接受的数据
public StringBuilder RecStr = new StringBuilder(4096); private void FrmRecAndSend_Load(object sender, EventArgs e)
{
//初始化comboBox控件
this.comName.SelectedIndex = 0;
this.comBaudRate.SelectedIndex = 3;
this.comByteSize.SelectedIndex = 0;
this.comParity.SelectedIndex = 0;
this.comStopBits.SelectedIndex = 0;
}
//打开或关闭串口
private void btnOpen_Click(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
myCom.Close();
//状态栏显示串口名称,以及是否为打开状态
this.statusCom.Text = "STATUS:" + myCom.PortName + "CLOSED!";
this.btnOpen.Text = "打开串口";
}
else
{
//初始化串口属性
this.comboBoxChang();
myCom.Open();
//状态栏显示串口名称以及是否为打开状态
this.statusCom.Text = "STATUS:" + myCom.PortName + "OPENED";
this.btnOpen.Text = "关闭串口";
this.timerRec.Enabled = true;
}
} //初始化串口信息
public void comboBoxChang()
{
try
{
myCom.PortName = this.comName.SelectedItem.ToString();
myCom.BaudRate = Convert.ToInt16(this.comBaudRate.SelectedItem.ToString());
myParity();
myCom.DataBits = Convert.ToInt16(this.comByteSize.SelectedItem.ToString());
myStopBits();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//根据选择确认校验位
public void myParity()
{
if (this.comParity.SelectedIndex == 0)
{
myCom.Parity = Parity.None;
}
else
{
if (this.comParity.SelectedIndex == 1)
{
myCom.Parity = Parity.Odd;
}
else
{
myCom.Parity = Parity.Even;
}
}
} //根据选择确定停止位
public void myStopBits()
{
int caseSwitch = this.comStopBits.SelectedIndex;
switch (caseSwitch)
{
case 1:
myCom.StopBits = StopBits.OnePointFive;
break;
case 2:
myCom.StopBits = StopBits.Two;
break;
default:
myCom.StopBits = StopBits.One;
break;
}
} //发送按钮,发送下面的要发送的数据
private void btnSend_Click(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
if (this.tBoxSed.Text == "")
{
MessageBox.Show("发送的数据为空");
}
try
{
myCom.Write(this.tBoxSed.Text);
TX += this.tBoxSed.Text.Trim().ToString().Length;
this.tBoxTX.Text = TX.ToString();
}
catch (Exception ex)
{
MessageBox.Show("发送数据失败!" + ex);
}
}
else
{
MessageBox.Show("请确认串口为打开状态");
}
}
//关闭窗体则关闭串口
private void FrmRecAndSend_Closing(object sender, CancelEventArgs e)
{
myCom.Close();
}
//去掉发送数组中的空格
private string delSpace(String putin)
{
string putout = "";
for (int i = 0; i < putin.Length; i++)
{
if(putin[i] != ' ')
{
putout += putin[i];
} }
return putout;
} //显示包信息
public string dis_package(char[] recB)
{
string tempT = "";
foreach(char b in recB)
{
tempT += b.ToString() + "";
}
return tempT;
} //自动检测串口是否为打开状态,若为打开状态,自动把接收的数据显示在接收区内
private void timerRec_Tick(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
try
{
int numByte = myCom.BytesToRead;
if (numByte != 0)
{
char[] TempByte = new char[2048];
int numStr = myCom.Read(TempByte, 0, numByte);
RecStr.Append(TempByte);
this.tBoxRec.Text = tBoxRec.Text + RecStr.ToString();
RX += numStr;
this.tBoxRec.Text = RX.ToString();
if (this.tBoxRec.Text.Length > 138)
{
this.tBoxRec.Text = string.Empty;
}
RecStr.Remove(0, RecStr.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
this.timerRec.Enabled = false;
}
} //根据时间来自动发送数据
private void timerAutoSend_Tick(object sender, EventArgs e)
{
try
{
myCom.Write(this.tBoxSed.Text);
TX += this.tBoxSed.Text.Trim().ToString().Length;
this.tBoxTX.Text = TX.ToString();
}
catch (Exception ex)
{
MessageBox.Show("发送失败!" + ex);
}
}
//检查自动发送的选择按钮是否选中,若选中,则开启自动发送的时间事件
private void cBoxSendSelf_CheckStateChanged(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
if (this.tBoxSed.Text == "")
{
MessageBox.Show("发送为空");
return;
}
else
{
if (this.cBoxSendSelf.Checked == true)
{
this.timerAutoSend.Interval = Convert.ToInt16(this.tTime.Text.Trim().ToString());
this.timerAutoSend.Enabled = true;
}
else
{
this.timerAutoSend.Enabled = false;
}
}
}
else
{
MessageBox.Show("请确定串口为打开状态!");
this.cBoxSendSelf.Checked = false;
}
} private void btnCountReset_Click(object sender, EventArgs e)
{
TX = 0;
RX = 0;
this.tBoxTX.Text = TX.ToString();
this.tBoxRX.Text = RX.ToString();
}
下面是完整版代码。 //定义串口对象
SerialPort myCom = new SerialPort();
public static int RX = 0;
public static int TX = 0;
//定义动态字符串,用来存储接受的数据
public StringBuilder RecStr = new StringBuilder(4096); private void FrmRecAndSend_Load(object sender, EventArgs e)
{
//初始化comboBox控件
this.comName.SelectedIndex = 0;
this.comBaudRate.SelectedIndex = 3;
this.comByteSize.SelectedIndex = 0;
this.comParity.SelectedIndex = 0;
this.comStopBits.SelectedIndex = 0;
}
//打开或关闭串口
private void btnOpen_Click(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
myCom.Close();
//状态栏显示串口名称,以及是否为打开状态
this.statusCom.Text = "STATUS:" + myCom.PortName + "CLOSED!";
this.btnOpen.Text = "打开串口";
}
else
{
//初始化串口属性
this.comboBoxChang();
myCom.Open();
//状态栏显示串口名称以及是否为打开状态
this.statusCom.Text = "STATUS:" + myCom.PortName + "OPENED";
this.btnOpen.Text = "关闭串口";
this.timerRec.Enabled = true;
}
} //初始化串口信息
public void comboBoxChang()
{
try
{
myCom.PortName = this.comName.SelectedItem.ToString();
myCom.BaudRate = Convert.ToInt16(this.comBaudRate.SelectedItem.ToString());
myParity();
myCom.DataBits = Convert.ToInt16(this.comByteSize.SelectedItem.ToString());
myStopBits();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//根据选择确认校验位
public void myParity()
{
if (this.comParity.SelectedIndex == 0)
{
myCom.Parity = Parity.None;
}
else
{
if (this.comParity.SelectedIndex == 1)
{
myCom.Parity = Parity.Odd;
}
else
{
myCom.Parity = Parity.Even;
}
}
} //根据选择确定停止位
public void myStopBits()
{
int caseSwitch = this.comStopBits.SelectedIndex;
switch (caseSwitch)
{
case 1:
myCom.StopBits = StopBits.OnePointFive;
break;
case 2:
myCom.StopBits = StopBits.Two;
break;
default:
myCom.StopBits = StopBits.One;
break;
}
} //发送按钮,发送下面的要发送的数据
private void btnSend_Click(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
if (this.tBoxSed.Text == "")
{
MessageBox.Show("发送的数据为空");
}
try
{
myCom.Write(this.tBoxSed.Text);
TX += this.tBoxSed.Text.Trim().ToString().Length;
this.tBoxTX.Text = TX.ToString();
}
catch (Exception ex)
{
MessageBox.Show("发送数据失败!" + ex);
}
}
else
{
MessageBox.Show("请确认串口为打开状态");
}
}
//关闭窗体则关闭串口
private void FrmRecAndSend_Closing(object sender, CancelEventArgs e)
{
myCom.Close();
}
//去掉发送数组中的空格
private string delSpace(String putin)
{
string putout = "";
for (int i = 0; i < putin.Length; i++)
{
if(putin[i] != ' ')
{
putout += putin[i];
} }
return putout;
} //显示包信息
public string dis_package(char[] recB)
{
string tempT = "";
foreach(char b in recB)
{
tempT += b.ToString() + "";
}
return tempT;
} //自动检测串口是否为打开状态,若为打开状态,自动把接收的数据显示在接收区内
private void timerRec_Tick(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
try
{
int numByte = myCom.BytesToRead;
if (numByte != 0)
{
char[] TempByte = new char[2048];
int numStr = myCom.Read(TempByte, 0, numByte);
RecStr.Append(TempByte);
this.tBoxRec.Text = tBoxRec.Text + RecStr.ToString();
RX += numStr;
this.tBoxRec.Text = RX.ToString();
if (this.tBoxRec.Text.Length > 138)
{
this.tBoxRec.Text = string.Empty;
}
RecStr.Remove(0, RecStr.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
this.timerRec.Enabled = false;
}
} //根据时间来自动发送数据
private void timerAutoSend_Tick(object sender, EventArgs e)
{
try
{
myCom.Write(this.tBoxSed.Text);
TX += this.tBoxSed.Text.Trim().ToString().Length;
this.tBoxTX.Text = TX.ToString();
}
catch (Exception ex)
{
MessageBox.Show("发送失败!" + ex);
}
}
//检查自动发送的选择按钮是否选中,若选中,则开启自动发送的时间事件
private void cBoxSendSelf_CheckStateChanged(object sender, EventArgs e)
{
if (myCom.IsOpen)
{
if (this.tBoxSed.Text == "")
{
MessageBox.Show("发送为空");
return;
}
else
{
if (this.cBoxSendSelf.Checked == true)
{
this.timerAutoSend.Interval = Convert.ToInt16(this.tTime.Text.Trim().ToString());
this.timerAutoSend.Enabled = true;
}
else
{
this.timerAutoSend.Enabled = false;
}
}
}
else
{
MessageBox.Show("请确定串口为打开状态!");
this.cBoxSendSelf.Checked = false;
}
} private void btnCountReset_Click(object sender, EventArgs e)
{
TX = 0;
RX = 0;
this.tBoxTX.Text = TX.ToString();
this.tBoxRX.Text = RX.ToString();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货