我目前正在编写一个简单的应用程序,该应用程序将通过ftdi232芯片(串行端口)与基于arduino的设备进行通信
我碰到了basestream读取的墙-我的应用程序在虚拟端口(com0com)上进行测试时效果很好,但是当我切换到ftdi设备时,接收到的数据变得混乱和/或重复。
发送和接收端口都配置为相同:
目前(测试),两个ftdi板均已连接到同一台计算机。电路板通过3条线(rx,tx [交叉]和接地)连接。
问题方法:
public async Task StartReceivingAsync()
{
_isLissening = true;
string errorData = string.Empty;
byte[] mainBuffer = new byte[_completeCommandSizeWithSep];
while(_port.IsOpen && !_receiveToken.IsCancellationRequested)
{
int bytesRead = await _port.BaseStream.ReadAsync(mainBuffer, 0, _completeCommandSizeWithSep, _receiveToken);
string rawData = Encoding.GetEncoding(_port.Encoding.CodePage).GetString(mainBuffer);
if(_port.Encoding.CodePage == Encoding.ASCII.CodePage)
_receivedBuffer.Append(RemoveNonAsciiChars(rawData));
else
_receivedBuffer.Append(rawData);
if(_receivedBuffer.Length >= _completeCommandSizeWithSep)
{
ICommandModel command = _commandModelFac();
string workPiece = _receivedBuffer.ToString(0, _completeCommandSizeWithSep);
int whereToCut = CheckRawData(workPiece);
if(whereToCut == -1)
{
command.Data = workPiece;
_receivedBuffer.Remove(0, _completeCommandSizeWithSep);
}
else if(whereToCut > 0)
{
command.Data = _receivedBuffer.ToString(0, whereToCut);
_receivedBuffer.Remove(0, whereToCut);
}
if (whereToCut != 0)
command.CommandType = CommandType.Error;
else
{
command = CommandTranslator(workPiece);
_receivedBuffer.Remove(0, _completeCommandSizeWithSep);
}
DataReceived?.Invoke(this, command);
}
else
{
continue;
}
}
_isLissening = false;
}
发送方式:
while (true)
{
serial.Write($@"*{"0015"}*" + i.ToString().PadLeft(4, '0') + '*' + i.ToString().PadLeft(30, '.'));
Console.WriteLine(serial.ReadExisting());
i++;
Console.WriteLine("...");
Console.ReadLine();
}
_completeCommandSizeWithSep是应接收的命令长度。
命令格式为* 0001 * AAAA * 123456789123asdfgh12345678584a
发送数据时来自workPiece变量的数据示例(几乎是普通数据,只是删除了非ASCII字符):
发送的数据:
*0015*0EF9*.............................0
*0015*2K3R*.............................1
*0015*C80S*.............................2
收到的数据:
*0015*0EF9*.0EF9*.0EF9*.0EF9*.0EF9*.0EF9*
...F9*...F9*...F9*...F9*...F9*...F9*...F9
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*...F
*...F9*...F9*...F9*...F9*...F9*...F9*0..F
*...F9*...F9*...F9*...F9*...F9*0..F9**..F
*...F9*...F9*...F9*...F9*0..F9**..F9*00.F
*...F9*...F9*...F9*0..F9**..F9*00.F9*10.F
*...F9*...F9*0..F9**..F9*00.F9*10.F9*50.F
*...F9*0..F9**..F9*00.F9*10.F9*50.F9**0.F
*0..F9**..F9*00.F9*10.F9*50.F9**0.F9*00.F
**..F9*00.F9*10.F9*50.F9**0.F9*00.F9*E0.F
*..F9*00.F9*10.F9*50.F9**0.F9*00.F9*E0.F9
*00.F9*10.F9*50.F9**0.F9*00.F9*E0.F9*F0.F
*10.F9*50.F9**0.F9*00.F9*E0.F9*F0.F9*90.F
*50.F9**0.F9*00.F9*E0.F9*F0.F9*90.F9**0.F
**0.F9*00.F9*E0.F9*F0.F9*90.F9**0.F9*.0.F
*0.F9*00.F9*E0.F9*F0.F9*90.F9**0.F9*.0.F9
*00.F9*E0.F9*F0.F9*90.F9**0.F9*.0.F9*...F
*E0.F9*F0.F9*90.F9**0.F9*.0.F9*...F9*....
*F0.F9*90.F9**0.F9*.0.F9*...F9*....9*....
*90.F9**0.F9*.0.F9*...F9*....9*....9*....
**0.F9*.0.F9*...F9*....9*....9*....9*....
*0.F9*.0.F9*...F9*....9*....9*....9*....9
*.0.F9*...F9*....9*....9*....9*....9*....
*...F9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*0...
*....9*....9*....9*....9*....9*0...9**...
*....9*....9*....9*....9*0...9**...9*0...
*....9*....9*....9*0...9**...9*0...9*01..
*....9*....9*0...9**...9*0...9*01..9*51..
*....9*0...9**...9*0...9*01..9*51..9**1..
*0...9**...9*0...9*01..9*51..9**1..9*21..
**...9*0...9*01..9*51..9**1..9*21..9*K1..
*...9*0...9*01..9*51..9**1..9*21..9*K1..9
*0...9*01..9*51..9**1..9*21..9*K1..9*31..
*01..9*51..9**1..9*21..9*K1..9*31..9*R1..
*51..9**1..9*21..9*K1..9*31..9*R1..9**1..
**1..9*21..9*K1..9*31..9*R1..9**1..9*.1..
*1..9*21..9*K1..9*31..9*R1..9**1..9*.1..9
*21..9*K1..9*31..9*R1..9**1..9*.1..9*.1..
*K1..9*31..9*R1..9**1..9*.1..9*.1..9*.1..
*31..9*R1..9**1..9*.1..9*.1..9*.1..9*.1..
*R1..9**1..9*.1..9*.1..9*.1..9*.1..9*....
**1..9*.1..9*.1..9*.1..9*.1..9*....9*....
*1..9*.1..9*.1..9*.1..9*.1..9*....9*....9
*.1..9*.1..9*.1..9*.1..9*....9*....9*....
*.1..9*.1..9*.1..9*....9*....9*....9*....
*.1..9*.1..9*....9*....9*....9*....9*....
*.1..9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*1...
*....9*....9*....9*....9*....9*1...9**...
*....9*....9*....9*....9*1...9**...9*0...
*....9*....9*....9*1...9**...9*0...9*0...
*....9*....9*1...9**...9*0...9*0...9*1...
*....9*1...9**...9*0...9*0...9*1...9*5...
*1...9**...9*0...9*0...9*1...9*5...9**...
**...9*0...9*0...9*1...9*5...9**...9*2...
*...9*0...9*0...9*1...9*5...9**...9*2...9
*0...9*0...9*1...9*5...9**...9*2...9*K...
*0...9*1...9*5...9**...9*2...9*K...9*3...
*1...9*5...9**...9*2...9*K...9*3...9*R*..
*5...9**...9*2...9*K...9*3...9*R*..9*.*..
**...9*2...9*K...9*3...9*R*..9*.*..9*.*..
*...9*2...9*K...9*3...9*R*..9*.*..9*.*..9
*2...9*K...9*3...9*R*..9*.*..9*.*..9*.*..
*K...9*3...9*R*..9*.*..9*.*..9*.*..9*.*..
*3...9*R*..9*.*..9*.*..9*.*..9*.*..9*.*..
*R*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..
*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*
*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..
*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*
*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..
*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*
*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..
*..9*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*
*.*..9*.*..9*.*..9*.*..9*.*..9*.*..9*....
*..9*.*..9*.*..9*.*..9*.*..9*.*..9*....9*
*.*..9*.*..9*.*..9*.*..9*.*..9*....9*....
*..9*.*..9*.*..9*.*..9*.*..9*....9*....9*
*.*..9*.*..9*.*..9*.*..9*....9*....9*....
*..9*.*..9*.*..9*.*..9*....9*....9*....9*
*.*..9*.*..9*.*..9*....9*....9*....9*....
*..9*.*..9*.*..9*....9*....9*....9*....9*
*.*..9*.*..9*....9*....9*....9*....9*....
*..9*.*..9*....9*....9*....9*....9*....9*
*.*..9*....9*....9*....9*....9*....9*....
*..9*....9*....9*....9*....9*....9*....9*
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*....
*....9*....9*....9*....9*....9*....9*1...
*....9*....9*....9*....9*....9*1...9**...
*....9*....9*....9*....9*1...9**...9*0...
*....9*....9*....9*1...9**...9*0...9*01..
*....9*....9*1...9**...9*0...9*01..9*5*..
*....9*1...9**...9*0...9*01..9*5*..9*C8..
*1...9**...9*0...9*01..9*5*..9*C8..9*08..
**...9*0...9*01..9*5*..9*C8..9*08..9*S8..
*...9*0...9*01..9*5*..9*C8..9*08..9*S8..9
*0...9*01..9*5*..9*C8..9*08..9*S8..9**...
*01..9*5*..9*C8..9*08..9*S8..9**...9*....
*5*..9*C8..9*08..9*S8..9**...9*....9*....
*..9*C8..9*08..9*S8..9**...9*....9*....9*
*C8..9*08..9*S8..9**...9*....9*....9*....
*08..9*S8..9**...9*....9*....9*....9*....
*S8..9**...9*....9*....9*....9*....9*....
**...9*....9*....9*....9*....9*..........
*...9*....9*....9*....9*....9*...........
*....9*....9*....9*....9*................
*....9*....9*....9*......................
*....9*....9*............................
*....9*..................................
*........................................
....................2*....0*....015...*C5
另外-来自端口侦听器的数据:
1619781: 2019-09-21 23:37:52,7484713 +0,2099872
2A 30 30 31 35 2A 30 45 46 39 2A 2E 2E 2E 2E 2E *0015*0EF9*.....
2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E ................
2E 2E 2E 2E 2E 2E 2E 2E 30 2A 30 30 31 35 2A 30 ........0*0015*0
45 46 39 2A 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E EF9*............
2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E ................
2E 30 .0
1620349: 2019-09-21 23:37:54,4023089 +0,3609614
2A 30 30 31 35 2A 32 4B 33 52 2A 2E 2E 2E 2E 2E *0015*2K3R*.....
2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E ................
2E 2E 2E 2E 2E 2E 2E 2E 31 2A 30 30 31 35 2A 32 ........1*0015*2
4B 33 52 2A 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E K3R*............
2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E ................
2E 31 .1
1620937: 2019-09-21 23:37:54,8706713 +0,3255464
2A 30 30 31 35 2A 43 38 30 53 2A 2E 2E 2E 2E 2E *0015*C80S*.....
2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E ................
2E 2E 2E 2E 2E 2E 2E 2E 32 2A 30 30 31 35 2A 43 ........2*0015*C
38 30 53 2A 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 80S*............
2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E 2E ................
2
我找到了解决方案!
InStartReceivingAsync
方法byte[] mainBuffer
被初始化一次。
初始化之后,出现“ while ...”循环,该循环进行数据的实际流读取。
我不知道FTDI芯片的确切规格,但是当持续发送时,它确实以相当随机的部分发送数据(发送方和接收方没有超时)
因此,在执行时,
int bytesRead = await _port.BaseStream.ReadAsync(mainBuffer, 0, _completeCommandSizeWithSep, _receiveToken);
我会得到随机发送的数据部分。该数据将按预期方式写入mainBuffer
字节数组,然后将其添加到中_receivedBuffer
。
事情来了:mainBuffer
当流接收到小于的数据部分时,不会被清除_completeCommandSizeWithSep
。
BaseStream阅读器仅从mainBuffer
接收到的0索引中替换尽可能多的字符,而忽略其余部分。
解决方法是只将长度等于
bytesRead
value的子字符串而不是整个字符串添加mainBuffer
到接收的缓冲区中:
int bytesRead = await _port.BaseStream.ReadAsync(mainBuffer, 0, _completeCommandSizeWithSep, _receiveToken);
Bad code:
string rawData = Encoding.GetEncoding(_port.Encoding.CodePage).GetString(mainBuffer);
Working code:
string rawData = Encoding.GetEncoding(_port.Encoding.CodePage).GetString(mainBuffer).Substring(0, bytesRead);
另一种解决方案是mainBuffer
在While循环中重新初始化数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句