C# serialport写入时需要显示新表单
当IDTECH写卡器正在等待刷卡时,尝试显示一个写有“请刷卡”的新表单C# serialport写入时需要显示新表单,c#,serial-port,C#,Serial Port,当IDTECH写卡器正在等待刷卡时,尝试显示一个写有“请刷卡”的新表单 private void writecard(string track1) //string track2) { SerialPort sp1 = new SerialPort(encoderport, 9600, Parity.None, 8, StopBits.One); sp1.ReadTimeout = 10000; if (sp1.IsOpen == tr
private void writecard(string track1) //string track2)
{
SerialPort sp1 = new SerialPort(encoderport, 9600, Parity.None, 8, StopBits.One);
sp1.ReadTimeout = 10000;
if (sp1.IsOpen == true)
{
sp1.Close();
}
sp1.Open();
sp1.Write(new byte[] { 0x1B, 0x77 }, 0, 2);
Thread.Sleep(100);
sp1.Write(new byte[] { 0x1B, 0x73 }, 0, 2);
Thread.Sleep(100);
sp1.Write(new byte[] { 0x1B, 0x01 }, 0, 2);
Thread.Sleep(100);
sp1.Write(track1); // Track 1
Thread.Sleep(100);
sp1.Write(new byte[] { 0x3f, 0x1c }, 0, 2);
swipeform swipepop = new swipeform();
swipepop.ShowDialog(); // **Problem**
Thread.Sleep(100);
try
{
swipepop.Close(); // **Problem**
int firstChar = sp1.ReadChar();
Thread.Sleep(500);
string data = String.Concat(Convert.ToChar(firstChar), sp1.ReadExisting());
//textBox3.Text = String.Format(data);
if (data != "")
{
char result = data[1];
if (result == '0')
MessageBox.Show("Encoding Successful");
else
MessageBox.Show("Encoding Failed - Please try again");
}
}
catch (System.TimeoutException)
{
MessageBox.Show("Timed out. Please try again.");
sp1.Write(new byte[2] { 0x1b, 0x61 }, 0, 2);
}
sp1.Close();
}
我遇到的问题是,一旦调用writecard方法,swipeform就会弹出,设备希望您进行刷卡。但是,一旦您刷卡,表单将保持并不会关闭,并且在您关闭刷卡表单之前,该方法不会继续。form.ShowDialog()
正在阻塞。这意味着它在表单关闭之前不会返回
解决方案之一是:检查
swipepop
表单中的串行端口。您可以通过表单的构造函数传递SerialPort
对象。我尝试使用do while循环,该循环可以正常工作,但是对话框永远不会关闭,除非我点击X。我认为当X=1时它会保持打开状态