public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.btn_Send.Enabled = false;
this.btn_Close.Enabled = false;
GetPortNames();
} /// <summary>
/// 声明一个共有串口
/// </summary>
public System.IO.Ports.SerialPort com;
/// <summary>
/// 记录循环发送数据次数
/// </summary>
int i = 1; /// <summary>
/// 获取当前所有串口名
/// </summary>
protected void GetPortNames()
{
Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
foreach (string s in pc.Ports.SerialPortNames)
{
this.comboBox1.Items.Add(s);
}
} private void btn_OpenPort_Click(object sender, EventArgs e)
{
if (this.comboBox1.SelectedItem != null)
{
if (this.timer1.Enabled == true)
{
this.timer1.Stop();
this.timer1.Enabled = false;
this.btn_Send.Text = "发送";
}
com = new System.IO.Ports.SerialPort();
com.PortName = this.comboBox1.SelectedItem.ToString();
try
{
com.Open();
}
catch (UnauthorizedAccessException uae)
{
this.lbstatus.Text = uae.Message;
}
finally
{
}
if (com.IsOpen)
{
this.btn_OpenPort.Enabled = false;
this.lbstatus.Text = "串口" + this.comboBox1.SelectedItem.ToString() + "已经打开!";
this.btn_Close.Enabled = true;
this.btn_Send.Enabled = true;
}
}
else
{
MessageBox.Show("请选择串口!");
}
} private void btn_Close_Click(object sender, EventArgs e)
{
if (this.timer1.Enabled == true)
{
this.timer1.Stop();
this.timer1.Enabled = false;
this.btn_Send.Text = "发送";
}
if (com.IsOpen)
{
try
{
com.Close();
}
catch { }
finally { }
if (!com.IsOpen)
{
this.btn_Send.Enabled = false;
this.btn_OpenPort.Enabled = true;
this.lbstatus.Text = "串口" + this.comboBox1.SelectedItem.ToString() + "已经关闭!";
this.btn_Close.Enabled = false;
}
} } private void btn_Send_Click(object sender, EventArgs e)
{
if (this.btn_Send.Text == "取消")
{
i = 1;
this.timer1.Stop();
this.timer1.Enabled = false;
this.btn_Send.Text = "发送";
this.lbstatus.Text = "";
}
else if (this.rtb_Send.Text.Trim() != string.Empty || this.rtb_Send.Text.Trim() != "")
{
if (com.IsOpen)
{
string data = this.rtb_Send.Text.Trim();
if (this.chkboxloopsend.Checked == true)
{
this.btn_Send.Text = "取消";
timer1.Start();
timer1.Enabled = true;
this.timer1.Interval = Convert.ToInt32(this.textBox1.Text); }
else
{
if (this.chkboxifox.Checked == true)
data = StrToHex(data);
send(com, data);
string data1 = com.ReadExisting(); //这里我总是获得不到值 如果使用com.ReadLine就卡死了
this.lbstatus.Text = "成功发送数据:" + data;
if (this.chkboxautodelete.Checked == true)
{
this.rtb_Send.Text = "";
}
if (this.chkboxautodelete.Checked == true)
{
this.rtb_Send.Text = "";
}
}
}
}
else
MessageBox.Show("不能发送空数据!");
} /// <summary>
/// 发送数据
/// </summary>
/// <param name="objPort">串口</param>
/// <param name="data">发送的数据</param>
protected void send(System.IO.Ports.SerialPort objPort, string data)
{
if (this.chkboxifox.Checked == true)
data = StrToHex(data);
if (com.IsOpen)
{
try
{
com.WriteLine(data); }
catch { }
finally { }
}
} /// <summary>
/// 字符串转换成16进制
/// </summary>
/// <param name="str">输入字符串</param>
/// <returns>16进制</returns>
protected string StrToHex(string str)
{
string strTemp = "";
if (str == "")
return "";
byte[] bTemp = System.Text.Encoding.Default.GetBytes(str); for (int i = 0; i < bTemp.Length; i++)
{
strTemp += bTemp[i].ToString("X");
}
return strTemp;
} private void chkboxifox_CheckedChanged(object sender, EventArgs e)
{
if (this.chkboxifox.Checked == true)
{
this.rtb_Send.Text = StrToHex(this.rtb_Send.Text.Trim());
}
} private void timer1_Tick(object sender, EventArgs e)
{
string data = rtb_Send.Text.Trim();
if (this.chkboxifox.Checked == true)
data = StrToHex(data);
send(com, data);
this.lbstatus.Text = "成功发送数据:" + data + "第" + i.ToString() + "次";
i = i + 1;
}
}
string data1 = com.ReadExisting(); //这里我总是获得不到值 如果使用com.ReadLine就卡死了