📄 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 Microsoft.WindowsCE.Forms;
namespace DeviceApplication4
{
public partial class Form1 : Form
{
public
string str;
MsgWindow MsgWin;
Message msg;
Boolean bHexFlag;
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 3;
comboBox3.SelectedIndex = 0;
comboBox4.SelectedIndex = 0;
comboBox5.SelectedIndex = 0;
this.MsgWin = new MsgWindow(this);
this.msg = Message.Create(MsgWin.Hwnd, MsgWindow.WM_CUSTOMMSG, (IntPtr)0, (IntPtr)0);
bHexFlag = false;
//hndl= this.Handle;
}
private void SetCom()
{
serialPort1.Close();
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = int.Parse(comboBox2.Text.Trim());
if (comboBox3.SelectedIndex == 0)
serialPort1.Parity = System.IO.Ports.Parity.None;
else if (comboBox3.SelectedIndex == 1)
serialPort1.Parity = System.IO.Ports.Parity.Odd;
else
serialPort1.Parity = System.IO.Ports.Parity.Even;
serialPort1.DataBits = int.Parse(comboBox4.Text.Trim());
if (comboBox5.SelectedIndex == 0)
serialPort1.StopBits = System.IO.Ports.StopBits.One;
else
serialPort1.StopBits = System.IO.Ports.StopBits.Two;
serialPort1.Handshake = System.IO.Ports.Handshake.None;
serialPort1.ReadBufferSize = 4096;
serialPort1.Open();
if (serialPort1.IsOpen)
{
MessageBox.Show("打开" + serialPort1.PortName + "成功!");
button3.Text = "关闭串口";
}
else
MessageBox.Show("打开" + serialPort1.PortName + "失败!");
}
private void button3_Click(object sender, EventArgs e)
{
if (button3.Text == "关闭串口")
{
serialPort1.Close();
button3.Text = "打开串口";
}
else
SetCom();
}
private string ConvertToHex(string asciiString)
{
string hex = "";
foreach (char c in asciiString)
{
int tmp = c;
hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
private int chartohex(char chr1,char chr2)
{
int cTemp1, cTemp2;
if (chr1 > '9' && chr1 <= 'F')
cTemp1 = chr1- 'A' + 10;
else if (chr1 >= '0' && chr1 <= '9')
cTemp1 = chr1 - '0';
else
cTemp1 =0;
if (chr2 > '9' && chr2 <= 'F')
cTemp2 = chr2- 'A' + 10;
else if(chr2 >= '0' && chr2 <= '9')
cTemp2 = chr2 - '0';
else
cTemp2=0;
cTemp2 += cTemp1 * 0x10;
return cTemp2;
}
private void sendHexData(string strData)
{
int i,iLen;
//string strtemp="";
Byte[] chrdata = new Byte[100];
strData=strData.ToUpper();
strData=strData.Replace(" ", "");
iLen = strData.Length;
iLen = iLen / 2;
for (i = 0; i < iLen; i++)
{
chrdata[i] = (Byte)chartohex(strData[2 * i], strData[2 * i + 1]);
}
//str`ing strtemp = ConvertToHex(strData);
serialPort1.Write(chrdata,0,i);
}
private void button2_Click(object sender, EventArgs e)
{
if (!checkBox2.Checked)
serialPort1.Write(textBox2.Text);
else
sendHexData(textBox2.Text.Trim());
}
public void RespondToMessage(int x, int y)
{
//this.Text = "X = " + x.ToString() + ", Y= " + y.ToString();
this.textBox1.Text+= str;
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
Byte[] chrdata = new Byte[200];
int i,iLen;
/* str="";
if (!bHexFlag)
str = serialPort1.ReadChar().ToString();
else
str = serialPort1.ReadByte().ToString("X");
*/
str = "";
iLen = serialPort1.BytesToRead;
serialPort1.Read(chrdata, 0, iLen);
if (!bHexFlag)
{
char[] cpara = System.Text.Encoding.Default.GetChars(chrdata);
str = new String(cpara);
}
// for (i = 0; i < iLen; i++)
// str += chrdata[i].ToString("C") + " ";
else
{
for (i = 0; i < iLen; i++)
str += chrdata[i].ToString("X2") + " ";
}
// str += "\r\n";
MessageWindow.SendMessage(ref msg);
}
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
bHexFlag = true;
else
bHexFlag = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (button3.Text == "关闭串口")
{
serialPort1.Close();
button3.Text = "打开串口";
}
// else
// button3.Text == "关闭串口";
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (button3.Text == "关闭串口")
{
serialPort1.Close();
button3.Text = "打开串口";
}
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (button3.Text == "关闭串口")
{
serialPort1.Close();
button3.Text = "打开串口";
}
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if (button3.Text == "关闭串口")
{
serialPort1.Close();
button3.Text = "打开串口";
}
}
private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
{
if (button3.Text == "关闭串口")
{
serialPort1.Close();
button3.Text = "打开串口";
}
}
private void checkBox3_CheckStateChanged(object sender, EventArgs e)
{
if(checkBox3.Checked)
timer1.Enabled = true;
else
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!checkBox2.Checked)
serialPort1.Write(textBox2.Text);
else
sendHexData(textBox2.Text.Trim());
}
}
// Derive MessageWindow to respond to
// Windows messages and to notify the
// form when they are received.
public class MsgWindow : MessageWindow
{
// Assign integers to messages.
// Note that custom Window messages start at WM_USER = 0x400.
public const int WM_CUSTOMMSG = 0x0400;
// Create an instance of the form.
private Form1 msgform;
// Save a reference to the form so it can
// be notified when messages are received.
public MsgWindow(Form1 msgform)
{
this.msgform = msgform;
}
// Override the default WndProc behavior to examine messages.
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
// If message is of interest, invoke the method on the form that
// functions as a callback to perform actions in response to the message.
case WM_CUSTOMMSG:
this.msgform.RespondToMessage((int)msg.WParam, (int)msg.LParam);
break;
}
// Call the base WndProc method
// to process any messages not handled.
base.WndProc(ref msg);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -