我想用SerialPort类从COM口读数据,谁有这方面代码,能不能给参考下,谢谢!
QQ:94210717
QQ:94210717
解决方案 »
- listbox无刷新的实现textbox显示选定内容
- 怎么给以个固定按钮赋值
- gridview怎么动态显示“编辑,删除”这些字段
- 别名、[否决的]
- 问一下dataset 的HasChanges事件(看看啊!)
- DataGridView中DataGridViewComboBoxColumn如何进行初始化
- C#摄像头程序获取byte数据并在其他控件上转换成视频数据显示
- 各位大侠..怎样编写程序让Windows执行一系列的操作..比如打开我的电脑..进入C盘..谢谢!
- 这种赋值会出现什么情况???
- C#操作活动目录,映射网络磁盘的问题
- 在 Windows CE 5.0 的程序中,怎样实现颜色下拉选择ComboBox控件, 谢谢!
- 问两个简单的问题:1、在c#的代码编辑窗口左边怎样加上行的序号?2、如果代码编辑窗口中代码很多,怎样…………
http://www.codeproject.com/dotnet/DotNetComPorts.aspto vs2005
http://www.codeproject.com/csharp/serialcommunication.asp
http://www.codeproject.com/csharp/serialcommunication.asp这个我看了,照搬上去以后,出现操作超时异常,这是为什么啊?不捕捉异常的话提示:由于线程退出或应用程序请求,已放弃I/O操作。
无奈中...
我是用c#.net做的一个应用程序。
用了线程没有?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;namespace test
{
public partial class Form1 : Form
{
//create instance of property page
//property page is used to set values for stop bits and
//baud rate PropertyPage pp = new PropertyPage(); //create an Serial Port object
SerialPort sp = new SerialPort(); public Form1()
{
InitializeComponent();
} private void propertyButton_Click(object sender, EventArgs e)
{
//show property dialog
pp.ShowDialog(); propertyButton.Hide();
} private void sendButton_Click(object sender, EventArgs e)
{
try
{
//write line to serial port
sp.WriteLine(textBox.Text);
//clear the text box
textBox.Text = "";
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
} } private void readButton_Click(object sender, EventArgs e)
{
try
{
//clear the text box
textBox.Text = "";
//read serial port and displayed the data in text box
textBox.Text = sp.ReadLine();
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
} private void saveStatusButton_Click(object sender, EventArgs e)
{
//display values
//if no property is set the default values
if (pp.bRate == "" && pp.sBits == "")
{
dataBitLabel.Text = "BaudRate = " + sp.BaudRate.ToString();
readTimeOutLabel.Text = "StopBits = " + sp.StopBits.ToString();
}
else
{
dataBitLabel.Text = "BaudRate = " + pp.bRate;
readTimeOutLabel.Text = "StopBits = " + pp.sBits;
} parityLabel.Text = "DataBits = " + sp.DataBits.ToString();
stopBitLabel.Text = "Parity = " + sp.Parity.ToString();
readTimeOutLabel.Text = "ReadTimeout = " +
sp.ReadTimeout.ToString(); if (propertyButton.Visible == true)
propertyButton.Hide();
saveStatusButton.Hide();
startCommButton.Show(); try
{
//open serial port
sp.Open();
//set read time out to 500 ms
sp.ReadTimeout = 500;
}
catch (System.Exception ex)
{
baudRatelLabel.Text = ex.Message;
}
} private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
readButton.Show();
textBox.Show();
}
}
}
StringBuilder sb = new StringBuilder();
sb.Append((char)27);
sb.Append((char)118);
SerialPort sp = null;
int i = -1;
try
{
sp = new SerialPort(sPort);
sp.Open();
sp.WriteLine(sb.ToString());
sp.ReadTimeout = 3000;
i = sp.ReadByte();
sp.Close();
}
catch
{
if (sp != null) sp.Close();
return i;
}
return i;
读取热敏打印机状态