您的位置:首页 > 编程语言 > C#

C# Serialport(小插头) 控件 与 axMSComm(小电话)上传COM口 重量信息的使用

2011-07-25 12:03 525 查看
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();
serialPort1.DiscardInBuffer();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
3 在页面LOAD 事件中添加初始化打开端口的操作
private void Form1_Load(object sender, EventArgs e)
{
try
{
if (!this.serialPort1.IsOpen)
this.serialPort1.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
axMSComm(小电话):
1 拖拉axMSComm 控件到页面,一般默认是读取COM 1 口数据
2 在控件属性面板添加事件 OnComm
private void axMSComm1_OnComm(object sender, EventArgs e)
{
if (axMSComm1.InBufferCount > 0)
{
string strA = (string)axMSComm1.Input;
textBox1.Text = strA;
int I = strA.IndexOf("GW");
int J = strA.IndexOf("KG");
string strB = strA.Substring(I + 2, J - (I + 2));
textBox2.Text = strB;
}
}
3 在页面LOAD 事件中添加初始化打开端口等操作
{
axMSComm2.CommPort = System.Convert.ToInt16(1);
axMSComm2.Settings = "9600,N,8,1";
if (!axMSComm2.PortOpen)
axMSComm2.PortOpen = true;
axMSComm2.InputLen = 0;
axMSComm2.DTREnable = true;
axMSComm2.RTSEnable = true;
axMSComm2.InputMode = MSCommLib.InputModeConstants.comInputModeText;
axMSComm2.RThreshold = 15;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: