I'm making a keypad project. 1st user input is a 10-digit id. Then follow with 6-digit password. I've been thinking how to assign both input into different arrays upon receiving them from the serial port. Here is my attempt so far.
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()); }
}
After debugging, the problem are:
- The
MessageBox.Show()
would only display empty char array. - The arrays maybe did not contain anything.
- Using the
ReadTimeout
,user need to press the keypad in a given time which is not so flexible in design.
Any help or tips are really welcomed. Thanks in advance. I don't mind to built from scratch if you suggest so.