📄 comport.cs
字号:
this.nlist_RTh.Name = "nlist_RTh";
this.nlist_RTh.TabIndex = 15;
this.nlist_RTh.Value = new System.Decimal(new int[] {
3,
0,
0,
0});
//
// label7
//
this.label7.Location = new System.Drawing.Point(184, 32);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(72, 16);
this.label7.TabIndex = 17;
this.label7.Text = "RThreshold:";
//
// cb_handshaking
//
this.cb_handshaking.Items.AddRange(new object[] {
"None",
"RTS",
"RTSXOnXOff",
"XOnXoff"});
this.cb_handshaking.Location = new System.Drawing.Point(184, 104);
this.cb_handshaking.Name = "cb_handshaking";
this.cb_handshaking.Size = new System.Drawing.Size(120, 21);
this.cb_handshaking.TabIndex = 19;
this.cb_handshaking.Text = "None";
//
// cb_DTRenable
//
this.cb_DTRenable.Location = new System.Drawing.Point(184, 136);
this.cb_DTRenable.Name = "cb_DTRenable";
this.cb_DTRenable.Size = new System.Drawing.Size(88, 16);
this.cb_DTRenable.TabIndex = 21;
this.cb_DTRenable.Text = "DTR Enable";
//
// btDefault
//
this.btDefault.Location = new System.Drawing.Point(176, 184);
this.btDefault.Name = "btDefault";
this.btDefault.Size = new System.Drawing.Size(56, 23);
this.btDefault.TabIndex = 3;
this.btDefault.Text = "Default";
this.btDefault.Click += new System.EventHandler(this.btDefault_Click);
//
// ComPort
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(368, 269);
this.Controls.Add(this.groupBox1);
this.Name = "ComPort";
this.Text = "Com Port Setting";
this.Load += new System.EventHandler(this.ComPort_Load);
this.Closed += new System.EventHandler(this.ComPort_Closed);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.nlist_inputLen)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nlist_port)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nlist_RTh)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void ComPort_Load(object sender, System.EventArgs e)
{
import();
}
private void ComPort_Closed(object sender, System.EventArgs e)
{
try
{
export();
}
catch
{
MessageBox.Show ("Com Port Setting Fail !!!");
}
}
private void import()
{
AxMSCommLib.AxMSComm s = new AxMSCommLib.AxMSComm();
s = obj_MyMSComm;
// import port setting
// com port
nlist_port.Value = s.CommPort ;
// com setting : baud,parity,databits,stopbits
int m_pointString;
if (s.Settings .Substring (s.Settings .Length-2,1) ==",")
m_pointString =0;
else
m_pointString =2;
cb_baud.Text = s.Settings.Substring (0,s.Settings.Length-6 -(m_pointString));
string m_parity = s.Settings.Substring (s.Settings .Length -5 -(m_pointString),1);
switch (m_parity)
{
case ("n"):
cb_parity.Text = "none";
break;
case ("o"):
cb_parity.Text = "odd";
break;
case ("e"):
cb_parity.Text = "even";
break;
case ("m"):
cb_parity.Text = "mark";
break;
case ("s"):
cb_parity.Text = "Space";
break;
}
cb_dataBits.Text = s.Settings.Substring (s.Settings .Length -3-(m_pointString),1);
cb_stopBits.Text = s.Settings.Substring (s.Settings .Length -1-(m_pointString));
// com input mode setting
cb_inputMode.Text = s.InputMode.ToString ().Substring (12);
// com input len
nlist_inputLen.Value = s.InputLen ;
// import port advance setting
// com RThreshold
nlist_RTh.Value = s.RThreshold ;
// com DTREnable
cb_DTRenable.Checked = s.DTREnable ;
// com Handshaking
cb_handshaking.Text = s.Handshaking .ToString ().Substring (3);
// com Null Discard
cb_NullDiscard.Checked = s.NullDiscard;
}
private void export()
{
AxMSCommLib.AxMSComm s = new AxMSCommLib.AxMSComm();
s = obj_MyMSComm;
// exportt port setting
// com port
s.CommPort = short.Parse (nlist_port.Value.ToString ());
// com setting : baud,parity,databits,stopbits
string m_settings;
m_settings = cb_baud.Text + "," + cb_parity.Text .Substring (0,1) + "," + cb_dataBits.Text +"," +cb_stopBits.Text ;
s.Settings = m_settings;
// com input mode setting
if (cb_inputMode.Text =="Text")
s.InputMode = MSCommLib.InputModeConstants .comInputModeText ;
else
s.InputMode = MSCommLib.InputModeConstants .comInputModeBinary ;
// com input len
s.InputLen = short.Parse (nlist_inputLen.Value.ToString ()) ;
// import port advance setting
// com RThreshold
s.RThreshold = short.Parse (nlist_RTh.Value.ToString ()) ;
// com DTREnable
s.DTREnable = cb_DTRenable.Checked ;
// com Handshaking
string m_handshaking;
m_handshaking = cb_handshaking.Text ;
switch (m_handshaking)
{
case ("None"):
s.Handshaking = MSCommLib.HandshakeConstants .comNone ;
break;
case ("RTS"):
s.Handshaking = MSCommLib.HandshakeConstants .comRTS ;
break;
case ("RTSXOnXOff"):
s.Handshaking = MSCommLib.HandshakeConstants.comRTSXOnXOff ;
break;
case ("XOnXoff"):
s.Handshaking = MSCommLib.HandshakeConstants.comXOnXoff ;
break;
}
// com Null Discard
s.NullDiscard = cb_NullDiscard.Checked;
// to be change, if the MainForm name was changed.
MainForm rtMSComm = new MainForm (s);
}
private void btCancel_Click(object sender, System.EventArgs e)
{
obj_MyMSComm =obj_BaseMSCommTemp ;
import();
}
private void btOK_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btDefault_Click(object sender, System.EventArgs e)
{
obj_MyMSComm = DefaultSetting(obj_MyMSComm);
import();
}
/* ************************************************************************************************************
*
* ComPort Function (extern)
*
* ************************************************************************************************************/
public static AxMSCommLib.AxMSComm DefaultSetting(AxMSCommLib.AxMSComm objMSComm)
{
AxMSCommLib.AxMSComm s = new AxMSCommLib.AxMSComm();
s = objMSComm;
s.RThreshold =1;
s.Settings ="9600,n,8,1";
s.DTREnable = false; // if error try set to true
s.Handshaking = MSCommLib.HandshakeConstants.comNone ;
s.InputMode = MSCommLib.InputModeConstants .comInputModeBinary ;
s.InputLen =0;
s.NullDiscard = false;
return (s);
}
}
/* ************************************************************************************************************
*
* Numeric Function (extern)
*
* ************************************************************************************************************/
public class Numeric
{
public string Hex;
public int Dec;
public byte Byte;
public string Symbol;
public string Ascii;
public string Binary;
public Numeric(int m_Dec)
{
Dec = m_Dec;
Hex = Convert.ToString (m_Dec,16);
Byte = byte.Parse (m_Dec.ToString ());
Symbol = m_Dec.ToString ();
Ascii = ((char) Dec).ToString();
Binary = Convert.ToString (Dec,2);
}
public void FromDec(int m_Dec)
{
Dec = m_Dec;
Hex = Convert.ToString (m_Dec,16);
Byte = byte.Parse (m_Dec.ToString ());
Symbol = m_Dec.ToString ();
Ascii = ((char) Dec).ToString();
Binary = Convert.ToString (Dec,2);
}
public void FromHex(string m_Hex)
{
Dec = Convert.ToInt32 (m_Hex,16);
Hex = m_Hex;
Byte = byte.Parse (Dec.ToString ());
Symbol = Dec.ToString ();
Ascii = ((char) Dec).ToString();
Binary = Convert.ToString (Dec,2);
}
}
}
// step to import data and export data in between this form and MainForm
// step 1
// include the namespace in both form.
// step 2
/* write the following example code to MainForm, to open this "setting form"
private void btSetting_Click(object sender, System.EventArgs e)
{
ComPort f = new ComPort (MSComm1);
f.ShowDialog();
}
*/
// step 3
/* add one more function at MainForm to access the value from "this form"
public MainForm (AxMSCommLib.AxMSComm objMSComm)
{
MSComm1 = objMSComm;
}
// step 4
// then write the following example code at this form to transfer data to MainForm
MainForm rtMSComm = new MainForm (MSComm2);
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -