我創建了一個軟件,它從RFID標籤讀取一個值並通過串行端口連接到計算機。當我以調試模式運行程序時,會收到正確的值,但是當我在釋放模式下運行它時,會顯示不同的值。在調試模式下獲得正確的值,在串行編程的發佈模式下錯誤
RFID在調試模式下發送的值是\n00200054476720D\r\n
,但是當我在發佈模式下運行時,它顯示小塊中的值或有時會顯示一個空值,然後是該代碼。
這裏是我的代碼:作爲接收數據的字節接收到的數據
try
{
_port2.PortName = "COM" + doorport_txt.Text;
_port2.BaudRate = 9600;
_port2.Parity = Parity.None;
_port2.DataBits = 8;
_port2.StopBits = StopBits.One;
_port2.DataReceived += DoorPortDataReceivedHandler;
_port2.ReadTimeout = 2000;
if (!_port2.IsOpen)
{
_port2.Open();
}
MessageBox.Show(@"Door Port is Ready", @"Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, @"Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
private void DoorPortDataReceivedHandler(object sender,
SerialDataReceivedEventArgs e)
{
var sp = (SerialPort) sender;
string indata = sp.ReadExisting();
CheckTheft(indata);
}
*「它會如何知道信息是完整的?」* - 這是一個線性規則。你基本上描述了原始或非規範輸入。但OP從這個標籤閱讀器輸入的數據似乎是帶有行終止的字母數字文本,所以規範閱讀應該可以工作(即閱讀一行)。 – sawdust
@sawdust - 很可能是這種情況。 –