FTDI芯片C#编程-如何读取所有缓冲区?
我目前正在做一个FTDI芯片的项目 我正在用C#编程,我在FTDI网站上尝试了其中一个(第三个带有数据环回) 代码正在运行,我能够编写“Hello world”并将其读回。在这种情况下,我们知道我们期望从缓冲区返回多少数据:FTDI芯片C#编程-如何读取所有缓冲区?,c#,serial-port,buffer,ftdi,C#,Serial Port,Buffer,Ftdi,我目前正在做一个FTDI芯片的项目 我正在用C#编程,我在FTDI网站上尝试了其中一个(第三个带有数据环回) 代码正在运行,我能够编写“Hello world”并将其读回。在这种情况下,我们知道我们期望从缓冲区返回多少数据: // Perform loop back - make sure loop back connector is fitted to the device // Write string data to the device string dataToWrite = "
// Perform loop back - make sure loop back connector is fitted to the device
// Write string data to the device
string dataToWrite = "Hello world!";
UInt32 numBytesWritten = 0;
// Note that the Write method is overloaded, so can write string or byte array data
ftStatus = myFtdiDevice.Write(dataToWrite, dataToWrite.Length, ref numBytesWritten);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to write to device (error " + ftStatus.ToString() + ")");
Console.ReadKey();
return;
}
// Check the amount of data available to read
// In this case we know how much data we are expecting,
// so wait until we have all of the bytes we have sent.
UInt32 numBytesAvailable = 0;
do
{
ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytesAvailable);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
Console.ReadKey();
return;
}
Thread.Sleep(10);
} while (numBytesAvailable < dataToWrite.Length);
// Now that we have the amount of data we want available, read it
string readData;
UInt32 numBytesRead = 0;
// Note that the Read method is overloaded, so can read string or byte array data
ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to read data (error " + ftStatus.ToString() + ")");
Console.ReadKey();
return;
}
Console.WriteLine(readData);
//执行环回-确保设备上安装了环回连接器
//将字符串数据写入设备
字符串dataToWrite=“你好,世界!”;
UInt32 numbyteswrited=0;
//请注意,Write方法是重载的,因此可以写入字符串或字节数组数据
ftStatus=myFtdiDevice.Write(dataToWrite,dataToWrite.Length,ref NumbyteSwrited);
如果(ftStatus!=FTDI.FT\u STATUS.FT\u OK)
{
//等待按键
Console.WriteLine(“未能写入设备(错误“+ftStatus.ToString()+”);
Console.ReadKey();
回来
}
//检查可读取的数据量
//在这种情况下,我们知道需要多少数据,
//所以,等到我们有了所有发送的字节。
UInt32 numbytsavailable=0;
做
{
ftStatus=myFtdiDevice.getrxbytsavailable(参考numbytsavailable);
如果(ftStatus!=FTDI.FT\u STATUS.FT\u OK)
{
//等待按键
Console.WriteLine(“无法获取可读取的字节数(错误“+ftStatus.ToString()+”));
Console.ReadKey();
回来
}
睡眠(10);
}while(numbytsavailable
但是,如果我想读取所有数据,却不知道从缓冲区中可以得到多少数据,该怎么办
感谢使用事件驱动方法
using System;
using System.Timers;
namespace Example
{
public class Program
{
private Timer m_timer;
private event EventHandler<EventArgs<string>> ReadingAvailable;
protected virtual void OnReadingAvailable(string value)
{
EventHandler<EventArgs<string>> handler = ReadingAvailable;
if (handler != null)
{
handler(this, new EventArgs<string>(value));
}
}
public static void Main(string[] args)
{
var foo = new Program();
foo.Initialise();
Console.ReadLine();
}
private void Initialise()
{
ReadingAvailable += Program_ReadingAvailable;
m_timer = new Timer {Interval = 1000};
m_timer.Elapsed +=timer_Elapsed;
m_timer.Enabled = true;
m_timer.Start();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
string readData;
UInt32 numBytesRead = 0;
// Note that the Read method is overloaded, so can read string or byte array data
ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);
// add the condition checking here to validate that the readData in not empty.
OnReadingAvailable(readData);
}
private void Program_ReadingAvailable(object sender, EventArgs<string> e)
{
string readData= e.Value;
Console.WriteLine(readData);
}
}
///
/// Helper class to parse argument in the EventArg
public class EventArgs<T> : EventArgs
{
private readonly T m_value;
protected EventArgs()
: base()
{
m_value = default(T);
}
public EventArgs(T value)
{
m_value = value;
}
public T Value
{
get { return m_value; }
}
}
}
使用系统;
使用系统计时器;
名称空间示例
{
公共课程
{
私人定时器;
私有事件事件处理程序ReadingAvailable;
读取时受保护的虚拟无效可用(字符串值)
{
EventHandler=ReadingAvailable;
if(处理程序!=null)
{
处理程序(此,新事件参数(值));
}
}
公共静态void Main(字符串[]args)
{
var foo=新程序();
foo.Initialise();
Console.ReadLine();
}
私有无效初始化()
{
ReadingAvailable+=程序\u ReadingAvailable;
m_定时器=新定时器{Interval=1000};
m_timer.appeased+=定时器_appeased;
m_timer.Enabled=真;
m_timer.Start();
}
私有无效计时器\u已过(对象发送器,ElapsedEventArgs e)
{
字符串读取数据;
UInt32 numBytesRead=0;
//请注意,Read方法是重载的,因此可以读取字符串或字节数组数据
ftStatus=myFtdiDevice.Read(out readData,numbytsavailable,ref numbytsread);
//在此处添加条件检查以验证readData是否为空。
OnReadingAvailable(读取数据);
}
私有无效程序\u ReadingAvailable(对象发送方,事件参数e)
{
字符串readData=e.值;
Console.WriteLine(读取数据);
}
}
///
///用于分析EventArg中的参数的帮助器类
公共类EventArgs:EventArgs
{
私有只读T m_值;
受保护的事件参数()
:base()
{
m_值=默认值(T);
}
公共事件参数(T值)
{
m_值=值;
}
公共价值
{
获取{返回m_值;}
}
}
}
使用事件驱动方法
using System;
using System.Timers;
namespace Example
{
public class Program
{
private Timer m_timer;
private event EventHandler<EventArgs<string>> ReadingAvailable;
protected virtual void OnReadingAvailable(string value)
{
EventHandler<EventArgs<string>> handler = ReadingAvailable;
if (handler != null)
{
handler(this, new EventArgs<string>(value));
}
}
public static void Main(string[] args)
{
var foo = new Program();
foo.Initialise();
Console.ReadLine();
}
private void Initialise()
{
ReadingAvailable += Program_ReadingAvailable;
m_timer = new Timer {Interval = 1000};
m_timer.Elapsed +=timer_Elapsed;
m_timer.Enabled = true;
m_timer.Start();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
string readData;
UInt32 numBytesRead = 0;
// Note that the Read method is overloaded, so can read string or byte array data
ftStatus = myFtdiDevice.Read(out readData, numBytesAvailable, ref numBytesRead);
// add the condition checking here to validate that the readData in not empty.
OnReadingAvailable(readData);
}
private void Program_ReadingAvailable(object sender, EventArgs<string> e)
{
string readData= e.Value;
Console.WriteLine(readData);
}
}
///
/// Helper class to parse argument in the EventArg
public class EventArgs<T> : EventArgs
{
private readonly T m_value;
protected EventArgs()
: base()
{
m_value = default(T);
}
public EventArgs(T value)
{
m_value = value;
}
public T Value
{
get { return m_value; }
}
}
}
使用系统;
使用系统计时器;
名称空间示例
{
公共课程
{
私人定时器;
私有事件事件处理程序ReadingAvailable;
读取时受保护的虚拟无效可用(字符串值)
{
EventHandler=ReadingAvailable;
if(处理程序!=null)
{
处理程序(此,新事件参数(值));
}
}
公共静态void Main(字符串[]args)
{
var foo=新程序();
foo.Initialise();
Console.ReadLine();
}
私有无效初始化()
{
ReadingAvailable+=程序\u ReadingAvailable;
m_定时器=新定时器{Interval=1000};
m_timer.appeased+=定时器_appeased;
m_timer.Enabled=真;
m_timer.Start();
}
私有无效计时器\u已过(对象发送器,ElapsedEventArgs e)
{
字符串读取数据;
UInt32 numBytesRead=0;
//请注意,Read方法是重载的,因此可以读取字符串或字节数组数据
ftStatus=myFtdiDevice.Read(out readData,numbytsavailable,ref numbytsread);
//在此处添加条件检查以验证readData是否为空。
OnReadingAvailable(读取数据);
}
私有无效程序\u ReadingAvailable(对象发送方,事件参数e)
{
字符串readData=e.值;
Console.WriteLine(读取数据);
}
}
///
///用于分析EventArg中的参数的帮助器类
公共类EventArgs:EventArgs
{
私有只读T m_值;
受保护的事件参数()
: