C# .Read()问题
在用C#和串口连接的时候,收发正常(和其他客户端连接),但是客户端是以hex方式发出的也就是16进制,而SerialPort中的.ReadByte()可以读取之后转换,但...
在用C#和串口连接的时候,收发正常(和其他客户端连接),但是客户端是以hex方式发出的也就是16进制,而SerialPort中的.ReadByte()可以读取之后转换,但是太慢,要一个字节一个字节读,然后一个字节一个字节显示。.ReadLine()和.ReadExisting()读完直接转成string了。有什么办法解决??我在网上下了个实例,串口调试助手,里面转码什么的很快啊。
别给我复制粘贴!看好我的问题。 展开
别给我复制粘贴!看好我的问题。 展开
1个回答
展开全部
ReadLine是一行一行的,有个例子
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
#endregion
namespace Serialexpample
{
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_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}
private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
readButton.Show();
textBox.Show();
}
//when we want to save the status(value)
private void saveStatusButton_Click_1(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;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
#endregion
namespace Serialexpample
{
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_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Do u want to Close the App");
sp.Close();
}
private void startCommButton_Click(object sender, EventArgs e)
{
startCommButton.Hide();
sendButton.Show();
readButton.Show();
textBox.Show();
}
//when we want to save the status(value)
private void saveStatusButton_Click_1(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;
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询