求一个C#控制台应用程序将串口数据接收?串口来的数据十六进制的 有代码的话贴出来吧 十分感激啊!
2个回答
展开全部
给一个控制台应用程序示例:
using System.IO.Ports; // 应用命名空间
namespace csCon
{
class Program
{
static void Main(string[] args)
{
port = new SerialPort("COM1");
port.BaudRate = 51200;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
if (port.IsOpen)
{
char ch;
while((Console.ReadKey())!= 'q')
{
Console.WriteLine("按q键退出, 其它继续读取com口数据.");
}
}
}
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buf = new byte[1024 * 8];
int recvLen = port.Read(buf, 0, buf.Length);
Console.WriteLine(Encoding.Default.GetString(buf, 0, recvLen));
}
private static SerialPort port = null;
}
}
using System.IO.Ports; // 应用命名空间
namespace csCon
{
class Program
{
static void Main(string[] args)
{
port = new SerialPort("COM1");
port.BaudRate = 51200;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
if (port.IsOpen)
{
char ch;
while((Console.ReadKey())!= 'q')
{
Console.WriteLine("按q键退出, 其它继续读取com口数据.");
}
}
}
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
byte[] buf = new byte[1024 * 8];
int recvLen = port.Read(buf, 0, buf.Length);
Console.WriteLine(Encoding.Default.GetString(buf, 0, recvLen));
}
private static SerialPort port = null;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询