Serialport(小插头):
1 拖拉Serialport 控件到页面,一般默认是读取COM 1 口数据
2 在控件属性面板添加事件 DataReceived
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
if (serialPort1.BytesToRead > 0)
{
System.Threading.Thread.Sleep(1000);
string strInput = this.serialPort1.ReadExisting();
richTextBox1.Text = strInput;
int i = strInput.IndexOf("kg");
int j = strInput.IndexOf("GW");
richTextBox2.Text = "重量为:" + strInput.Substring(j + 2, i - j - 2).Trim() + " 字符长度为" + strInput.Length.ToString();