📄 form1.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;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
namespace Demo
{
public partial class btnCLoExit : Form
{
static byte SOH = 0x01;
static byte STX = 0x02;
static byte ETX = 0x03;
static byte S = 0x53;
static int bcc;
byte[] send1 = { SOH, S };
byte[] send4 = { STX};
byte[] send5 = { ETX};
SerialPort sp = new SerialPort();
public btnCLoExit()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ComBoBox_Load();
}
private void ComBoBox_Load()
{
this.comboBox1.Text = comboBox1.Items[0].ToString();
this.cboId.Text = cboId.Items[0].ToString();
this.comboBox3.Text = comboBox3.Items[1].ToString();
this.cmbSend.Text = cmbSend.Items[0].ToString();
this.cboData.Text = cboData.Items[7].ToString();
}
/**
* Open SerialPort
* **/
private void btnOpenPort_Click(object sender, EventArgs e)
{
try
{
sp.PortName = this.comboBox1.SelectedItem.ToString();
sp.BaudRate = (int.Parse)(this.comboBox3.SelectedItem.ToString());
sp.DataBits = (int.Parse)(this.cboData.SelectedItem.ToString());
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.RtsEnable = true;
sp.Open();
MessageBox.Show("串口打开成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/**
*Close SerialPort
* **/
private void btnClosePort_Click(object sender, EventArgs e)
{
try
{
if (sp.IsOpen)
{
sp.Close();
MessageBox.Show("串口已关闭!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/**
* Directly OpenBuzzer Operation
*
* **/
private void btnBuzzer_Click(object sender, EventArgs e)
{
try
{
if (sp.IsOpen)
{
int bcc = 0;
byte[] send = { SOH, S, 0x30, 0x31, 0x42, 0x31, STX, 0x32, 0x30, ETX };
bcc = SOH ^ S ^ 0x30 ^ 0x31 ^ 0x42 ^ 0x31 ^ STX ^ 0x32 ^ 0x30 ^ ETX | 0x20;
byte[] sendCheck ={ Convert.ToByte(bcc) };
sp.Write(send, 0, send.Length);
sp.Write(sendCheck, 0, sendCheck.Length);
this.cmbSend.Text = "B1";
string str = sp.ReadExisting();
byte[] sendRead = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
for (int i = 0; i < sendRead.Length; i++)
{
byte s = sendRead[i];
MessageBox.Show(s.ToString());
}
}
else
{
MessageBox.Show("请开启串口!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/**
* Directly CloseBuzzer Operation
*
* **/
private void closeBuzzer_Click(object sender, EventArgs e)
{
try
{
if (sp.IsOpen)
{
int bcc = 0;
byte[] send = { SOH, S, 0x30, 0x31, 0x42, 0x30, STX, ETX };
bcc = SOH ^ S ^ 0x30 ^ 0x31 ^ 0x42 ^ 0x30 ^ STX ^ ETX | 0x20;
byte[] sendCheck ={ Convert.ToByte(bcc) };
sp.Write(send, 0, send.Length);
sp.Write(sendCheck, 0, sendCheck.Length);
this.cmbSend.Text = "B0";
}
else
{
MessageBox.Show("串口未打开!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/**
* Directly ReverseBuzzer Operation
*
* */
private void btnReverseBuzzer_Click(object sender, EventArgs e)
{
try
{
if (sp.IsOpen)
{
int bcc = 0;
byte[] send = { SOH, S, 0x30, 0x31, 0x42, 0x32, STX, ETX };
bcc = SOH ^ S ^ 0x30 ^ 0x31 ^ 0x42 ^ 0x32 ^ STX ^ ETX | 0x20;
byte[] sendCheck ={ Convert.ToByte(bcc) };
sp.Write(send, 0, send.Length);
sp.Write(sendCheck, 0, sendCheck.Length);
this.cmbSend.Text = "B2";
}
else
{
MessageBox.Show("串口未打开!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/**
* Exit Window
* **/
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
/**
* Date Time Show
* **/
private void timer1_Tick(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
this.lblTime.Text = dt.ToString();
}
/**
* Close SerialPort and Exit window
* **/
/**
* Check SerialProt isOpen
* **/
private void SerialPortOpen()
{
if (!sp.IsOpen)
{
MessageBox.Show("请打开串口!");
}
}
/**
* A0 Single Pattern
* **/
private void singleModle()
{
byte[] send2 = System.Text.ASCIIEncoding.ASCII.GetBytes(this.cboId.SelectedItem.ToString());
byte[] send3 = System.Text.ASCIIEncoding.ASCII.GetBytes(this.cmbSend.SelectedItem.ToString());
bcc = SOH ^ S ^ send2[0] ^ send2[1] ^ send3[0] ^ send3[1] ^ STX ^ ETX | 0x20;
byte[] checkBcc ={ Convert.ToByte(bcc) };
sp.Write(send1,0,send1.Length);
sp.Write(send2, 0, send2.Length);
sp.Write(send3, 0, send3.Length);
sp.Write(send4, 0, send4.Length);
sp.Write(send5, 0, send5.Length);
sp.Write(checkBcc,0,checkBcc.Length);
}
/**
* A1 Card Read Pattern
* **/
public void CardReadPattern()
{
string str = this.txtSend.Text;
byte[] send2 = System.Text.ASCIIEncoding.ASCII.GetBytes(this.cboId.SelectedItem.ToString());
byte[] send3 = System.Text.ASCIIEncoding.ASCII.GetBytes(this.cmbSend.SelectedItem.ToString());
byte[] send = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
if(str.Length == 1)
{
byte data1 = send[0];
bcc = SOH ^ S ^ send2[0] ^ send2[1] ^ send3[0] ^ send3[1] ^ STX ^ data1 ^ ETX | 0x20;
}
else if (str.Length == 2)
{
byte data1 = send[0];
byte data2 = send[1];
bcc = SOH ^ S ^ send2[0] ^ send2[1] ^ send3[0] ^ send3[1] ^ STX ^ data1 ^ data2 ^ ETX | 0x20;
}
else if (str.Length == 3)
{
byte data1 = send[0];
byte data2 = send[1];
byte data3 = send[2];
bcc = SOH ^ S ^ send2[0] ^ send2[1] ^ send3[0] ^ send3[1] ^ STX ^ data1 ^ data2 ^ data3 ^ ETX | 0x20;
}
byte[] checkBcc ={ Convert.ToByte(bcc) };
sp.Write(send1,0,send1.Length);
sp.Write(send2,0,send2.Length);
sp.Write(send3,0,send3.Length);
sp.Write(send4, 0, send4.Length);
sp.Write(send, 0, send.Length);
sp.Write(send5, 0, send5.Length);
sp.Write(checkBcc, 0, checkBcc.Length);
}
/**
* A2 Number Read Pattern
* **/
private void NumberReadPattern()
{
CardReadPattern();
}
/**
* A3 Card Number Read Pattern
* **/
private void CardNumberReadPattern()
{
CardReadPattern();
}
/**
* A4 Special Key Input
* **/
private void SpecialKeyInput()
{
CardReadPattern();
}
/**
* A5 Card Special Read Pattern
* **/
private void CardSpecialReadPattern()
{
CardReadPattern();
}
/**
* A6 Number Special Input Pattern
* **/
private void NumberSpecialInputPattern()
{
CardReadPattern();
}
/**
* A7 CardNumberSpecial Read Pattern
* **/
private void CardNumberSpecial()
{
CardReadPattern();
}
/**
* B0 Close Buzzer
* **/
private void CloseBuzzer()
{
singleModle();
}
/**
* B1 Open Buzzer
* **/
private void OpenBuzzer()
{
CardReadPattern();
}
/**
* B2 Reverse Buzzer
* **/
private void ReverseBuzzer()
{
singleModle();
}
/**
* Function Send Implementation
* **/
private void btnSend_Click(object sender, EventArgs e)
{
try
{
SerialPortOpen();
if (this.cmbSend.Text == "A0")
singleModle();
else if (this.cmbSend.Text == "A1")
CardReadPattern();
else if (this.cmbSend.Text == "A2")
NumberReadPattern();
else if (this.cmbSend.Text == "A3")
CardNumberReadPattern();
else if (this.cmbSend.Text == "A4")
SpecialKeyInput();
else if (this.cmbSend.Text == "A5")
CardSpecialReadPattern();
else if (this.cmbSend.Text == "A6")
NumberSpecialInputPattern();
else if (this.cmbSend.Text == "A7")
CardNumberSpecial();
else if (this.cmbSend.Text == "B0")
CloseBuzzer();
else if (this.cmbSend.Text == "B1")
OpenBuzzer();
else if (this.cmbSend.Text == "B2")
ReverseBuzzer();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -