📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace CSharpDevice
{
public partial class MainForm : Form
{
string InputData = String.Empty;
delegate void SetTextCallback(string text);
public MainForm()
{
InitializeComponent();
string[] ports = SerialPort.GetPortNames();
// Add all port names to the combo box:
foreach (string port in ports)
{
this.cmbCOMPort.Items.Add(port);
}
}
private void btnGPRSConnect_Click(object sender, EventArgs e)
{
string strAPN = this.textAPN.Text.Trim();
if (strAPN.Length <= 0) strAPN = "GPRS";
//Urovo.DHC3500.Init(strAPN);
//Urovo.DHC3500.Connect(1);
if (!Urovo.DHC3500.GetGprsPowerStatus())
{
if (!Urovo.DHC3500.EnableGprs())
{
this.textGPRSProcess.Text = "Can't turn on GPRS power!";
return;
}
}
if (!Urovo.DHC3500.GPRS_Status())
{
if (!Urovo.DHC3500.GPRS_Connect(strAPN))
{
this.textGPRSProcess.Text = "Can't connect to GPRS, please retry later!";
return;
}
}
this.textGPRSProcess.Text = "Now, we have connected to GPRS!";
}
private void btnGPRSDisconnect_Click(object sender, EventArgs e)
{
Urovo.DHC3500.GPRS_DisConnect();
Urovo.DHC3500.DisableGprs();
}
private void btnWLANConnect_Click(object sender, EventArgs e)
{
//Urovo.DHC3500.Init(strAPN);
//Urovo.DHC3500.Connect(1);
if (!Urovo.DHC3500.GetWlanPowerStatus())
{
if (!Urovo.DHC3500.EnableWlan())
{
this.textWLANProcess.Text = "Can't turn on WLAN power!";
return;
}
}
if (!Urovo.DHC3500.WLAN_Connect())
{
this.textWLANProcess.Text = "Can't connect to WLAN, please retry later!";
return;
}
this.textWLANProcess.Text = "Now, we have connected to WLAN!";
}
private void btnWLANDisconnect_Click(object sender, EventArgs e)
{
Urovo.DHC3500.DisableWlan();
}
private void btnBCStart_Click(object sender, EventArgs e)
{
Urovo.DHC3500.StartEngine();
}
private void btnBCStop_Click(object sender, EventArgs e)
{
Urovo.DHC3500.StopEngine();
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 135 || e.KeyValue == 134)
{
Urovo.DHC3500.StartScan();
StringBuilder strBarcode = new StringBuilder(100), strType = new StringBuilder(100);
Urovo.DHC3500.GetBarcode(strBarcode, strType);
this.textBarcode.Text = strBarcode.ToString();
this.textBCProcess.Text = strType.ToString();
Urovo.DHC3500.StopScan();
}
}
private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyValue == 135)
{
Urovo.DHC3500.StopScan();
}
}
private void btnCOMStart_Click(object sender, EventArgs e)
{
if (Port.IsOpen) Port.WriteLine(this.textCOMData.Text);
else MessageBox.Show("Serial port is closed!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
this.textCOMData.Text = "";
}
private void Port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
char[] nTrim = new char[3];
nTrim[0] = '\r';
nTrim[1] = '\n';
nTrim[2] = '\t';
byte[] byArray = new byte[100];
//InputData = Port.ReadExisting().TrimEnd(nTrim);
int nRead = Port.Read(byArray, 0, 100);
while (nRead > 0)
{
for (int i = 0; i < nRead; i++)
SetText(Convert.ToString(byArray[i], 16));
nRead = Port.Read(byArray, 0, 100);
}
//if (InputData != String.Empty)
//{
// //this.textRead.Text = InputData;
// SetText(InputData);
//}
}
private void cmbCOMPort_SelectedIndexChanged(object sender, EventArgs e)
{
if (Port.IsOpen) Port.Close();
Port.PortName = this.cmbCOMPort.SelectedItem.ToString();
this.textCOMProcess.Text = Port.PortName + ": 9600,8N1";
// try to open the selected port:
try
{
Port.Open();
}
catch
{
// give a message, if the port is not available:
MessageBox.Show("Serial port " + Port.PortName + " cannot be opened!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
this.cmbCOMPort.SelectedIndex = -1;
this.textCOMProcess.Text = "Select serial port!";
}
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textRead.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else this.textRead.Text += text;
}
private void MainForm_Closed(object sender, EventArgs e)
{
if (Port.IsOpen) Port.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -