【发布时间】:2019-12-14 06:32:54
【问题描述】:
实际上,当人们/顾客打电话到餐厅订购时,我正在从 com 串行端口接收数据以获取 电话/手机号码。
第一次完美监听串行端口,然后 windows 窗体正确显示电话号码。要打开窗体,我必须使用 this.showdialog() 而不是 this.show()。因为在当前表单后面已经打开了几个表单。
问题出现在第一次显示对话框后,串口未监听电话或触发串口功能,并且表单未使用新的呼叫者电话号码更新。
我知道这是因为使用了 showdialog() 而发生的,但我必须使用它。我记下了我使用的代码..
private void FindCom()
{
try
{
//Messagebox.show("Called Find Com");
LogFile.WriteLogFile("File:Caller", "Method:FindCom", "--FindCom Called--");
string COM = RunProcess("FindCOMET.exe"); // Using the autofind to find the unit's
string[] COMPorts = COM.Split(';'); // Split the AutoFind string into individual ports
COM = COMPorts[0]; // Select the first COM port
// Initialise COM port with all settings.
serialPort = new SerialPort();
serialPort.PortName = COM;
serialPort.BaudRate = 1200;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Parity = Parity.None;
serialPort.DataReceived += new SerialDataReceivedEventHandler(this.serialPort_DataReceived);
serialPort.ReadTimeout = 100; // Required for end of packet Timeout notification
serialPort.Open();
}
catch (Exception ex)
{
//MessageBox.Show("Exception1"+ ex.Message.ToString());
LogFile.WriteLogFile("File:Caller", "Method:FindCom", "EM: " + ex.Message.ToString());
}
// Loop until user presses a key then exit
//System.Console.WriteLine("Press any key to exit");
//Console.ReadKey();
}
// Variables for the data read
int[] buffer = new int[255];
int pointer;
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
// Read all the current buffer into the array
for (int c = 0; c != serialPort.BytesToRead; c++)
{
buffer[pointer] = serialPort.ReadByte();
pointer++;
}
}
catch (TimeoutException x)
{
try
{
COMET_Data cid = new COMET_Data(buffer);
// Output the data or call something within your program here
string mobileNo = cid.getCIDNumber();
string telePhone =string.Empty;
var getName = string.Empty;
if (mobileNo != null)
{
mobileNo = Regex.Replace(mobileNo, "[^0-9]+", string.Empty);
// MessageBox.Show("FullNumber" + mobileNo);
if (mobileNo[0] == '9')
{
mobileNo = mobileNo.Substring(1);
}
//MessageBox.Show(mobileNo.Substring(0, 2)+" OF =>"+ mobileNo);
if (mobileNo.Substring(0,2) == "07")
{
//mobileNo = mobileNo;
txtbxMobileNo.Text = mobileNo;
lblMobileNo.Text = mobileNo;
}
else
{
telePhone = mobileNo;
//MessageBox.Show("telephone:" + telePhone);
txtbxTelephone.Text = telePhone;
lblMobileNo.Text = telePhone;
}
getName = cid.getCIDName();
}
else
{
lblCustomerName.Text = "Not Available";
txtbxMobileNo.Text = "Withheld";
lblMobileNo.Text = "Withheld";
}
//MessageBox.Show(mobileNo);
SaveCommetCaller(mobileNo);
// Reset the buffer pointer and buffer
buffer = new int[255];
pointer = 0;
//for (int i = 1; i >= 0; i--)
isFormOpen = true;
this.ShowDialog();
serialPort.DiscardInBuffer();
//is_open = false;
}
catch (Exception ex)
{
LogFile.WriteLogFile("File:Caller", "Method:DataReceived", "EM: " + ex.Message.ToString());
}
//serialPort.DataReceived += new SerialDataReceivedEventHandler(this.serialPort_DataReceived);
}
finally
{
// serialPort.DataReceived += serialPort_DataReceived;
}
}
我想在表单仍处于打开状态时使用 showdialog() 方法从串行端口获取数据,而无需用户活动来关闭或隐藏表单。
【问题讨论】:
标签: c# winforms serial-port showdialog