using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text.RegularExpressions;
using System.Xml;
namespace incomingtelegramdisplay
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private AxMSCommLib.AxMSComm com;
private System.Windows.Forms.RichTextBox rtfTerminal;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label lblPhoneNum;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private string callinfo="";
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
// Initialize the COM Port control
InitComPort();
// Send data out through the COM port
//com.Output = "Serial Terminal Initialized";
com.Output="at+vcid=1"+"\r";
}
private void InitComPort()
{
try
{
// Set the com port to be 1
com.CommPort = 3;
// This port is already open, close it to reset it.
if (com.PortOpen) com.PortOpen = false;
// Trigger the OnComm event whenever data is received
com.RThreshold = 1;
com.SThreshold=0;
// Set the port to 9600 baud, no parity bit, 8 data bits, 1 stop bit (all standard)
com.Settings = "9600,n,8,1";
// Force the DTR line high, used sometimes to hang up modems
com.DTREnable = true;
// No handshaking is used
com.Handshaking = MSCommLib.HandshakeConstants.comNone;
// Don't mess with byte arrays, only works with simple data (characters A-Z and numbers)
com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
//com.InputMode = MSCommLib.InputModeConstants.comInputModeBinary;
// Use this line instead for byte array input, best for most communications
//com.InputMode = MSCommLib.InputModeConstants.comInputModeText;
// Read the entire waiting data when com.Input is used
com.InputLen = 0;
// Don't discard nulls, 0x00 is a useful byte
com.NullDiscard = false;
// Attach the event handler
com.OnComm += new System.EventHandler(this.OnComm);
// Open the com port
com.PortOpen = true;
//com.Output = "Serial Terminal Initialized";
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
}
private void OnComm(object sender, EventArgs e) // MSCommLib OnComm Event Handler
{
this.callinfo=callinfo+com.Input.ToString();
this.rtfTerminal.Text=this.callinfo;
infoToken(this.rtfTerminal.Text);
}
private void infoToken(string temp)
{
//DATE = 0106
//TIME = 0219
//NMBR = 21156896
// Build a regular expression to match data that
Regex r = new Regex(@"NMBR\s=\s[0-9]*");
string num="";
// Cycle through the matches
for (Match m = r.Match(temp); m.Success; m = m.NextMatch())
{
// Display the result to the 'Output' debug window
//this.lblPhoneNum.Text=m.Value;
num=m.Value;
}
num=num.Substring(7);
this.lblPhoneNum.Text=num;
}
private void ProcessComData(string input)
{
// Send incoming data to a Rich Text Box
rtfTerminal.AppendText(input + "\n");
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.com = new AxMSCommLib.AxMSComm();
this.rtfTerminal = new System.Windows.Forms.RichTextBox();
this.lblPhoneNum = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.com)).BeginInit();
this.SuspendLayout();
//
// com
//
this.com.Enabled = true;
this.com.Location = new System.Drawing.Point(16, 192);
this.com.Name = "com";
this.com.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("com.OcxState")));
this.com.Size = new System.Drawing.Size(38, 38);
this.com.TabIndex = 0;
//
// rtfTerminal
//
this.rtfTerminal.Location = new System.Drawing.Point(24, 24);
this.rtfTerminal.Name = "rtfTerminal";
this.rtfTerminal.Size = new System.Drawing.Size(240, 120);
this.rtfTerminal.TabIndex = 1;
this.rtfTerminal.Text = "richTextBox1";
//
// lblPhoneNum
//
this.lblPhoneNum.Location = new System.Drawing.Point(104, 160);
this.lblPhoneNum.Name = "lblPhoneNum";
this.lblPhoneNum.Size = new System.Drawing.Size(160, 23);
this.lblPhoneNum.TabIndex = 2;
this.lblPhoneNum.Text = "label1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 208);
this.button1.Name = "button1";
this.button1.TabIndex = 3;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(192, 208);
this.button2.Name = "button2";
this.button2.TabIndex = 4;
this.button2.Text = "记录号码";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 160);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 23);
this.label1.TabIndex = 5;
this.label1.Text = "来电号码:";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(288, 246);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.lblPhoneNum);
this.Controls.Add(this.rtfTerminal);
this.Controls.Add(this.com);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.com)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
string a="DATE = 0106 TIME = 0219 NMBR = 21156896";
infoToken(a);
}
private void button2_Click(object sender, System.EventArgs e)
{
string fileName="..\\incomingPhoneNumber.xml";
XmlTextWriter tw=new XmlTextWriter(fileName,null);
tw.Formatting=Formatting.Indented;
tw.WriteStartDocument();
tw.WriteStartElement("IncomingCall");
tw.WriteElementString("PhoneNumber",this.lblPhoneNum.Text);
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
}
}
}