C# 使用BaseStream.BeginRead从COM端口读取并获取子字符串
我试图从com端口读取,这是我的代码C# 使用BaseStream.BeginRead从COM端口读取并获取子字符串,c#,winforms,serial-port,C#,Winforms,Serial Port,我试图从com端口读取,这是我的代码 public string HouseState() { string state = string.Empty; if (!Variables.portBusy) { // Begin communications var blockLimit = 13; openSerial();
public string HouseState()
{
string state = string.Empty;
if (!Variables.portBusy)
{
// Begin communications
var blockLimit = 13;
openSerial();
byte[] buffer = new byte[blockLimit];
Action kickoffRead = null;
kickoffRead = delegate
{
serialPort.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
{
try
{
int actualLength = serialPort.BaseStream.EndRead(ar);
byte[] received = new byte[actualLength];
Buffer.BlockCopy(buffer, 0, received, 0, actualLength);
state += System.Text.Encoding.UTF8.GetString(received);
//MessageBox.Show(state);
}
catch (IOException exc)
{
//handleAppSerialError(exc);
}
if (state.Count() <= 13)
kickoffRead();
}, null);
};
kickoffRead();
}
MessageBox.Show(state);
var index = state.IndexOf("R");
var command = string.Empty;
if (index >= 0)
command = state.Substring(index, 13);
return command;
}
公共字符串HouseState()
{
字符串状态=string.Empty;
如果(!Variables.portBusy)
{
//开始沟通
var blockLimit=13;
openSerial();
字节[]缓冲区=新字节[blockLimit];
动作kickoffRead=null;
kickoffRead=委托
{
serialPort.BaseStream.BeginRead(缓冲区、0、缓冲区、长度、委托(IAsyncResult ar)
{
尝试
{
int actualLength=serialPort.BaseStream.EndRead(ar);
字节[]接收=新字节[实际长度];
Buffer.BlockCopy(Buffer,0,received,0,actualLength);
state+=System.Text.Encoding.UTF8.GetString(已接收);
//MessageBox.Show(状态);
}
捕获(IOException)
{
//handleAppSerialError(exc);
}
如果(state.Count()=0)
command=state.Substring(索引,13);
返回命令;
}
我想得到的是一个以R开头的字符串,有13个字符。因为有时候端口会发送一半的字符串,所以我会这样做:if(state.Count()的
BeginRead
方法SerialPort.BaseStream
是异步的,所以当你到达MessageBox.Show(state)时
实际读取可能尚未完成,并且状态仍然为空。您需要等待,直到读取所有必要的数据:
// .....................
var readComplete = new ManualResetEvent(false);
kickoffRead = delegate
{
serialPort.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
{
// ...................
if (state.Count() <= 13)
kickoffRead();
else
readComplete.Set();
}, null);
};
kickoffRead();
readComplete.WaitOne();
// ......................
/。。。。。。。。。。。。。。。。。。。。。
var readComplete=新手动重置事件(错误);
kickoffRead=委托
{
serialPort.BaseStream.BeginRead(缓冲区、0、缓冲区、长度、委托(IAsyncResult ar)
{
// ...................
如果(SerialPort.BaseStream
的BeginRead
方法(state.Count()是异步的,那么当您进入MessageBox.Show(state);
时,实际的读取可能还没有完成,并且state
仍然为空。您需要等待,直到读取所有必要的数据:
// .....................
var readComplete = new ManualResetEvent(false);
kickoffRead = delegate
{
serialPort.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar)
{
// ...................
if (state.Count() <= 13)
kickoffRead();
else
readComplete.Set();
}, null);
};
kickoffRead();
readComplete.WaitOne();
// ......................
/。。。。。。。。。。。。。。。。。。。。。
var readComplete=新手动重置事件(错误);
kickoffRead=委托
{
serialPort.BaseStream.BeginRead(缓冲区、0、缓冲区、长度、委托(IAsyncResult ar)
{
// ...................
if(state.Count()你摇滚兄弟,我不知道它是异步的,为什么它不需要异步命令?我也不知道这个手动重置事件,谢谢async
关键字应该与方法一起使用。作为旧的异步模型实现,BeginRead
不知道任务和async
/等待。你摇滚兄弟,我我不知道它是异步的,为什么它不需要异步命令?我也不知道这个手动重置事件,感谢async
关键字应该与方法一起使用。作为旧的异步模型实现,BeginRead
不知道任务,而async
//code>wait
。