如何正确读取串行端口C#
我有以下问题: 我从串行端口读取数据,得到非常奇怪的输出 这是我的代码:如何正确读取串行端口C#,c#,serial-port,console.writeline,C#,Serial Port,Console.writeline,我有以下问题: 我从串行端口读取数据,得到非常奇怪的输出 这是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.IO.Ports; using System.Threading; using System.Text.RegularExpressi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions;
using System.Globalization;
namespace SerialPort_Test
{
class Program
{
static SerialPort mySerialPort;
static void Main(string[] args)
{
mySerialPort = new SerialPort("COM3", 19200, Parity.Even, 7, StopBits.One);
//mySerialPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
mySerialPort.RtsEnable = true;
mySerialPort.Open();
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
while (true)
{
Console.ReadLine();
}
}
public static string clean(string s)
{
string ss = "";
//s = string.Join("", Regex.Split(s, "(ID|I<|P<|V<|VI)").Skip(1));
char[] arr = s.ToCharArray();
char[] db = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P','Q','R','S','T','U','V','X','Y','Z','<','|','0','1','2','3','4','5','6',
'7','8','9'};
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < db.Length; j++)
{
if (arr[i] == db[j])
{
ss += arr[i];
}
}
}
return ss;
}
private static void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string s = clean(mySerialPort.ReadExisting().Replace("\r", "|"));
Console.WriteLine(s);
}
}
}
这似乎是好的,我得到它在一行,但我不想这样,我需要使用
WriteLine();
每次刷卡都能得到一致的输出
谢谢你的建议
问候,,
伊利亚利不要用
Console.WriteLine(“#“++s+”#”)替换换行符并调试代码
或控制台。Write
似乎您的设备不会在一次“读取”中返回所有数据代码>我得到的不是新行,而是子字符串被#包围,这是预期的。然而,这仅表明,当我刷卡时,串行端口缓冲区未被完全读取,但读取一部分,然后读取另一部分,然后读取另一部分,直到缓冲区为空,并且由于WriteLine(),所有新的读数都被放在新行中如果我使用Write()代码>我得到开头的#和结尾的一个。就这些。有什么想法吗?现在我注意到如果我使用控制台
只有第一个输入是应该的,也就是说,s
是一个完整的字符串,每次我刷卡时,几乎每个字符都会被#包围。如果没有这个设备进行调试,很难权威地回答,但我不确定您的总体设计是否会始终有效。文档中的“不能保证为接收到的每个字节引发DataReceived事件。请使用BytesToRead属性确定缓冲区中还有多少数据需要读取。”请中断clean(mySerialPort.ReadExisting().Replace(“\r”,“|”)将>编码到每个部分中,并分别调试每个部分,而不是作为一行代码。@IliaLazarov您是否可以尝试使用var stream=new StreamReader(mySerialPort.BaseStream)在“线程”中读取数据;someloop{stream.ReadLine();}
不附加到DataReceived
event不替换换行符并使用Console.WriteLine(“#“++s+”#”)调试代码
或控制台。Write
似乎您的设备不会在一次“读取”中返回所有数据代码>我得到的不是新行,而是子字符串被#包围,这是预期的。然而,这仅表明,当我刷卡时,串行端口缓冲区未被完全读取,但读取一部分,然后读取另一部分,然后读取另一部分,直到缓冲区为空,并且由于WriteLine(),所有新的读数都被放在新行中如果我使用Write()代码>我得到开头的#和结尾的一个。就这些。有什么想法吗?现在我注意到如果我使用控制台
只有第一个输入是应该的,也就是说,s
是一个完整的字符串,每次我刷卡时,几乎每个字符都会被#包围。如果没有这个设备进行调试,很难权威地回答,但我不确定您的总体设计是否会始终有效。文档中的“不能保证为接收到的每个字节引发DataReceived事件。请使用BytesToRead属性确定缓冲区中还有多少数据需要读取。”请中断clean(mySerialPort.ReadExisting().Replace(“\r”,“|”)将>编码到每个部分中,并分别调试每个部分,而不是作为一行代码。@IliaLazarov您是否可以尝试使用var stream=new StreamReader(mySerialPort.BaseStream)在“线程”中读取数据;someloop{stream.ReadLine();}
未附加到数据接收
事件
WriteLine();