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;
}
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;
}
相关文章推荐
- c#中跨线程调用windows窗体控件 .我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来做这个问题,下面我将详细的介绍。
- C#的SerialPort使用,动态创建串口连接,短信猫通信
- C# SerialPort使用方法
- C#串口通信:MSComm控件使用详解
- c# SerialPort控件串口通信
- c# SerialPort控件串口通信
- C# SerialPort控件串口通信
- C# SerialPort 使用DataReceived接口利用空闲中断原理接收完整一帧数据
- C# SerialPort使用方法
- c# SerialPort使用时出现“已关闭Safe handle”的错误
- C# serialport控件的一些理解
- serialPort基本属性与使用(串口)——C#
- C# 使用FileUpload控件上传图片,将文件转换成二进制进行存储与读取
- C# SerialPort使用方法 .
- 使用.net的SerialPort串口监控控件接收串口数据
- C# SerialPort的简单使用
- c#上传文件(一)使用 .net 控件上传文件
- c# winform项目treeview控件绑定本地电脑磁盘信息,浏览磁盘图片,上传本地图片控件
- C# ASP.NET上传控件fileupload的使用
- C#串口通信:MSComm控件使用详解