2015-09-09 29 views
0

我正在製作一個鍵盤項目。第一個用戶輸入是一個10位數的ID。然後按照6位密碼。我一直在考慮如何從串口接收它們時將這兩個輸入分配到不同的陣列中。這是我迄今的嘗試。從串口讀取字符串並存儲到2個不同的陣列中

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 
    char[] id = new char[11]; 
    char[] pass = new char[7]; 
    int length; 

    try 
    { 
     serialPort1.ReadTimeout = 100; 
     do 
     { 
      length = serialPort1.Read(id, 0, 11); 
     } while (length > 0); 
    } 
    catch (TimeoutException) { MessageBox.Show(id.ToString()); } 

    try 
    { 
     serialPort1.ReadTimeout = 100; 
     do 
     { 
      length = serialPort1.Read(pass, 0, 7); 
     } while (length > 0); 
    } 
    catch (TimeoutException) { MessageBox.Show(pass.ToString()); } 
} 

調試之後,該問題是:

  1. MessageBox.Show()只會顯示空字符數組。
  2. 數組可能不包含任何內容。
  3. 使用ReadTimeout,用戶需要在給定的時間內按下鍵盤,這在設計上不太靈活。

任何幫助或提示,真的很受歡迎。提前致謝。如果你有這樣的建議,我不介意從頭開始建造。

+0

當你指定數組,你已經刪除了一個從大小,而不是加入一個...他們應該是'char [] id = new char [11]; char [] pass = new char [6];'相反,它們當前不足以容納10個char和6個char字符串。 – Serdalis

+0

我應該用'char [] id = new char [11]; char [] pass = new char [7];'? 6或7無關緊要?糾正我,如果我錯了 –

+0

你是對的,對不起,我錯了第二陣列。額外的值是保存字符串的空終止符,你可能需要也可能不需要,因爲這是一個數據流,但它很好解釋。 – Serdalis

回答

3

我的建議是:

  • 脫鉤事件DataReceived上 方法serialPort1_DataReceived的從serialPort1對象
  • 第一線爲idchar陣列,減少到因爲你是10而不是11 E讀10位ID
  • 同去passchar陣列,將其降低到6

建議代碼:

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
{ 
    // unhook event 
    serialPort1.DataReceived -= serialPort1_DataReceived; 

    char[] id = new char[10]; 
    char[] pass = new char[6]; 

    try 
    { 
     // send unique start bit from connected serial port device to indicate begin of transmission 
     byte ack = serialPort1.ReadByte(); 
     while (ack != 0xC0) 
     { 
      ack = serialPort1.ReadByte(); // <- continuously read from serial port until start bit is found 
     } 

     // try to increase timeout as 100 ms is too fast 
     serialPort1.ReadTimeout = 1000; 
     for (int i = 0; i < 10; i++) 
     { 
      id[i] = (char)serialPort1.ReadByte(); 
     } 
    } 
    catch (TimeoutException) { MessageBox.Show(id.ToString()); } 

    try 
    { 
     // send unique start bit from connected serial port device to indicate begin of transmission 
     byte ack = serialPort1.ReadByte(); 
     while (ack != 0xC0) 
     { 
      ack = serialPort1.ReadByte(); // <- continuously read from serial port until start bit is found 
     } 

     // try to increase timeout as 100 ms is too fast 
     serialPort1.ReadTimeout = 1000; 
     for (int i = 0; i < 6; i++) 
     { 
      pass[i] = (char)serialPort1.ReadByte(); 
     } 
    } 
    catch (TimeoutException) { MessageBox.Show(pass.ToString()); } 

    // rehook event 
    serialPort1.DataReceived += serialPort1_DataReceived; 
} 
+0

謝謝!它終於有效。這太棒了:) –

2

這是它出錯

do 
{ 
    length = serialPort1.Read(id, 0, 11); 
} while (length > 0); 

代碼意味着嘗試讀取11字節到ID,然後返回字節的實際讀取數量。因此,如果有100個字節可用,則長度將爲11.如果只有5個字節可用於讀取,則長度將爲5.因爲長度> 0,它將再次循環以再次嘗試讀取11個字節,直到它不讀取任何內容。隨着您正在將數據讀入同一陣列,隨後的讀取將覆蓋先前讀取中的數據。

也是一樣與

do 
{ 
    length = serialPort1.Read(pass, 0, 7); 
} while (length > 0); 
相關問題