实例介绍
【实例截图】
【核心代码】
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using INIFILE;
using System.Text.RegularExpressions;
using System.Threading;
namespace SerialPortConnection
{
public partial class Form1 : Form
{
SerialPort sp1 = new SerialPort();
byte[] sendint_temp= new byte[8];
//sp1.ReceivedBytesThreshold = 1;//只要有1个字符送达端口时便触发DataReceived事件
public Form1()
{
InitializeComponent();
}
//加载
private void Form1_Load(object sender, EventArgs e)
{
INIFILE.Profile.LoadProfile();//加载所有
// 预置波特率
switch (Profile.G_BAUDRATE)
{
case "300":
cbBaudRate.SelectedIndex = 0;
break;
case "600":
cbBaudRate.SelectedIndex = 1;
break;
case "1200":
cbBaudRate.SelectedIndex = 2;
break;
case "2400":
cbBaudRate.SelectedIndex = 3;
break;
case "4800":
cbBaudRate.SelectedIndex = 4;
break;
case "9600":
cbBaudRate.SelectedIndex = 5;
break;
case "19200":
cbBaudRate.SelectedIndex = 6;
break;
case "38400":
cbBaudRate.SelectedIndex = 7;
break;
case "115200":
cbBaudRate.SelectedIndex = 8;
break;
default:
{
MessageBox.Show("波特率预置参数错误。");
return;
}
}
//预置波特率
switch (Profile.G_DATABITS)
{
case "5":
cbDataBits.SelectedIndex = 0;
break;
case "6":
cbDataBits.SelectedIndex = 1;
break;
case "7":
cbDataBits.SelectedIndex = 2;
break;
case "8":
cbDataBits.SelectedIndex = 3;
break;
default:
{
MessageBox.Show("数据位预置参数错误。");
return;
}
}
//预置停止位
switch (Profile.G_STOP)
{
case "1":
cbStop.SelectedIndex = 0;
break;
case "1.5":
cbStop.SelectedIndex = 1;
break;
case "2":
cbStop.SelectedIndex = 2;
break;
default:
{
MessageBox.Show("停止位预置参数错误。");
return;
}
}
//预置校验位
switch(Profile.G_PARITY)
{
case "NONE":
cbParity.SelectedIndex = 0;
break;
case "ODD":
cbParity.SelectedIndex = 1;
break;
case "EVEN":
cbParity.SelectedIndex = 2;
break;
default:
{
MessageBox.Show("校验位预置参数错误。");
return;
}
}
comboBox1.Text = Profile.G_COMbox1;
comboBox2.Text = Profile.G_COMbox2;
comboBox3.Text = Profile.G_COMbox3;
txCmd0Name.Text = Profile.G_CmdName[0];
txCmd1Name.Text = Profile.G_CmdName[1];
txCmd2Name.Text = Profile.G_CmdName[2];
txCmd3Name.Text = Profile.G_CmdName[3];
txCmd4Name.Text = Profile.G_CmdName[4];
txCmd5Name.Text = Profile.G_CmdName[5];
txCmd6Name.Text = Profile.G_CmdName[6];
txCmd7Name.Text = Profile.G_CmdName[7];
txCmd8Name.Text = Profile.G_CmdName[8];
txCmd9Name.Text = Profile.G_CmdName[9];
txCmd10Name.Text = Profile.G_CmdName[10];
txCmd11Name.Text = Profile.G_CmdName[11];
txCmd12Name.Text = Profile.G_CmdName[12];
txCmd13Name.Text = Profile.G_CmdName[13];
txCmd14Name.Text = Profile.G_CmdName[14];
txCmd0Adress.Text = Profile.G_CmdAdress[0];
txCmd1Adress.Text = Profile.G_CmdAdress[1];
txCmd2Adress.Text = Profile.G_CmdAdress[2];
txCmd3Adress.Text = Profile.G_CmdAdress[3];
txCmd4Adress.Text = Profile.G_CmdAdress[4];
txCmd5Adress.Text = Profile.G_CmdAdress[5];
txCmd6Adress.Text = Profile.G_CmdAdress[6];
txCmd7Adress.Text = Profile.G_CmdAdress[7];
txCmd8Adress.Text = Profile.G_CmdAdress[8];
txCmd9Adress.Text = Profile.G_CmdAdress[9];
txCmd10Adress.Text = Profile.G_CmdAdress[10];
txCmd11Adress.Text = Profile.G_CmdAdress[11];
txCmd12Adress.Text = Profile.G_CmdAdress[12];
txCmd13Adress.Text = Profile.G_CmdAdress[13];
txCmd14Adress.Text = Profile.G_CmdAdress[14];
txCmd0Num.Text = Profile.G_CmdNum[0];
txCmd1Num.Text = Profile.G_CmdNum[1];
txCmd2Num.Text = Profile.G_CmdNum[2];
txCmd3Num.Text = Profile.G_CmdNum[3];
txCmd4Num.Text = Profile.G_CmdNum[4];
txCmd5Num.Text = Profile.G_CmdNum[5];
txCmd6Num.Text = Profile.G_CmdNum[6];
txCmd7Num.Text = Profile.G_CmdNum[7];
txCmd8Num.Text = Profile.G_CmdNum[8];
txCmd9Num.Text = Profile.G_CmdNum[9];
txCmd10Num.Text = Profile.G_CmdNum[10];
txCmd11Num.Text = Profile.G_CmdNum[11];
txCmd12Num.Text = Profile.G_CmdNum[12];
txCmd13Num.Text = Profile.G_CmdNum[13];
txCmd14Num.Text = Profile.G_CmdNum[14];
txCmd0Dat.Text = Profile.G_CmdDat[0];
txCmd1Dat.Text = Profile.G_CmdDat[1];
txCmd2Dat.Text = Profile.G_CmdDat[2];
txCmd3Dat.Text = Profile.G_CmdDat[3];
txCmd4Dat.Text = Profile.G_CmdDat[4];
txCmd5Dat.Text = Profile.G_CmdDat[5];
txCmd6Dat.Text = Profile.G_CmdDat[6];
txCmd7Dat.Text = Profile.G_CmdDat[7];
txCmd8Dat.Text = Profile.G_CmdDat[8];
txCmd9Dat.Text = Profile.G_CmdDat[9];
txCmd10Dat.Text = Profile.G_CmdDat[10];
txCmd11Dat.Text = Profile.G_CmdDat[11];
txCmd12Dat.Text = Profile.G_CmdDat[12];
txCmd13Dat.Text = Profile.G_CmdDat[13];
txCmd14Dat.Text = Profile.G_CmdDat[14];
//检查是否含有串口
string[] str = SerialPort.GetPortNames();
if (str == null)
{
MessageBox.Show("本机没有串口!", "Error");
return;
}
//添加串口项目
foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
{//获取有多少个COM口
//System.Diagnostics.Debug.WriteLine(s);
cbSerial.Items.Add(s);
}
//串口设置默认选择项
cbSerial.SelectedIndex = 1; //note:获得COM9口,但别忘修改
//cbBaudRate.SelectedIndex = 5;
// cbDataBits.SelectedIndex = 3;
// cbStop.SelectedIndex = 0;
// cbParity.SelectedIndex = 0;
sp1.BaudRate = 9600;
Control.CheckForIllegalCrossThreadCalls = false; //这个类中我们不检查跨线程的调用是否合法(因为.net 2.0以后加强了安全机制,,不允许在winform中直接跨线程访问控件的属性)
sp1.DataReceived = new SerialDataReceivedEventHandler(sp1_DataReceived);
//sp1.ReceivedBytesThreshold = 1;
radio1.Checked = true; //单选按钮默认是选中的
rbRcvStr.Checked = true;
//准备就绪
sp1.DtrEnable = true;
sp1.RtsEnable = true;
//设置数据读取超时为1秒
sp1.ReadTimeout = 1000;
sp1.Close();
}
void sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (sp1.IsOpen) //此处可能没有必要判断是否打开串口,但为了严谨性,我还是加上了
{
Thread.Sleep(20);//延时20ms
//输出当前时间
//DateTime dt = DateTime.Now;
//txtReceive.Text = dt.GetDateTimeFormats('f')[0].ToString() "\r\n";
// txtReceive.SelectAll();
//txtReceive.SelectionColor = Color.Blue; //改变字体的颜色
byte[] byteRead = new byte[sp1.BytesToRead]; //BytesToRead:sp1接收的字符个数
if (rdSendStr.Checked) //'发送字符串'单选按钮
{
txtReceive.Text = sp1.ReadLine() "\r\n"; //注意:回车换行必须这样写,单独使用"\r"和"\n"都不会有效果
sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer
}
else //'发送16进制按钮'
{
try
{
Byte[] receivedData = new Byte[sp1.BytesToRead]; //创建接收字节数组
sp1.Read(receivedData, 0, receivedData.Length); //读取数据
sp1.DiscardInBuffer(); //清空SerialPort控件的Buffer //这是用以显示字符串
string strRcv = null;
for (int i = 0; i < receivedData.Length; i ) //窗体显示
{
strRcv = receivedData[i].ToString("X2") " "; //16进制显示
}
if (DisTimeCheckBox1.Checked)
{
// txtReceive.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") ":";
txtReceive.Text = System.DateTime.Now.ToString("HH:mm:ss:fff") ":";
}
txtReceive.Text = "R:" " " strRcv "\r\n";
txtReceive.SelectionStart = txtReceive.Text.Length;//选定文本的起点是文本框结尾
txtReceive.ScrollToCaret();//将滚动条滚到文本框结尾
if (DatAnalysisCheckBox.Checked)
{
int a = (int)receivedData[1] * 256 (int)receivedData[2];
if (sendint_temp[1] == 1)
{
txInPutVol.Text = a.ToString() "mV";
txCur.Text = "";
txOutPutVol.Text = "";
}
else if (sendint_temp[1] == 2)
{
txInPutVol.Text ="";
txCur.Text = a.ToString() "uA";
txOutPutVol.Text = "";
}
else if (sendint_temp[1] == 3)
{
txInPutVol.Text = "";
txCur.Text = "";
txOutPutVol.Text = a.ToString() "mV";
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "出错提示");
txtSend.Text = "";
}
}
}
else
{
MessageBox.Show("请打开某个串口", "错误提示");
}
}
//发送按钮
private void btnSend_Click(object sender, EventArgs e)
{
if (cbTimeSend.Checked)
{
tmSend.Enabled = true;
}
else
{
tmSend.Enabled = false;
}
if (!sp1.IsOpen) //如果没打开
{
MessageBox.Show("请先打开串口!", "Error");
return;
}
String strSend = txtSend.Text;
if (radio1.Checked == true) //“HEX发送” 按钮
{
//处理数字转换
string sendBuf = strSend;
string sendnoNull = sendBuf.Trim();
string sendNOComma = sendnoNull.Replace(',', ' '); //去掉英文逗号
string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号
string strSendNoComma2 = sendNOComma1.Replace("0x", ""); //去掉0x
strSendNoComma2.Replace("0X", ""); //去掉0X
string[] strArray = strSendNoComma2.Split(' ');
int byteBufferLength = strArray.Length;
for (int i = 0; i < strArray.Length; i )
{
if (strArray[i]=="")
{
byteBufferLength--;
}
}
// int temp = 0;
byte[] byteBuffer = new byte[byteBufferLength];
int ii = 0;
for (int i = 0; i < strArray.Length; i ) //对获取的字符做相加运算
{
// Byte[] bytesOfStr = Encoding.Default.GetBytes(strArray[i]);
int decNum = 0;
if (strArray[i] == "")
{
//ii--; //加上此句是错误的,下面的continue以延缓了一个ii,不与i同步
continue;
}
else
{
decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18
}
try //防止输错,使其只能输入一个字节的字符
{
byteBuffer[ii] = Convert.ToByte(decNum);
}
catch (System.Exception ex)
{
MessageBox.Show("字节越界,请逐个字节输入!", "Error");
tmSend.Enabled = false;
return;
}
ii ;
}
sendint_temp= byteBuffer;//用于对接收数据的解析
if (DisTimeCheckBox1.Checked )
{
// txtReceive.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") ":";
txtReceive.Text = System.DateTime.Now.ToString("HH:mm:ss:fff") ":";
}
if (DisSendcheckBox.Checked)
{
txtReceive.Text = "T:" strSend "\r\n";
txtReceive.SelectionStart = txtReceive.Text.Length;//选定文本的起点是文本框结尾
txtReceive.ScrollToCaret();//将滚动条滚到文本框结尾
// txtReceive.Select(txtReceive.Text.Length-2, txtReceive.Text.Length);
// txtReceive.SelectionColor = Color.Red; //改变字体的颜色
}
sp1.Write(byteBuffer, 0, byteBuffer.Length);
}
else //以字符串形式发送时
{
sp1.WriteLine(txtSend.Text); //写入数据
}
}
//开关按钮
private void btnSwitch_Click(object sender, EventArgs e)
{
//serialPort1.IsOpen
if (!sp1.IsOpen)
{
try
{
//设置串口号
string serialName = cbSerial.SelectedItem.ToString();
sp1.PortName = serialName;
//设置各“串口设置”
string strBaudRate = cbBaudRate.Text;
string strDateBits = cbDataBits.Text;
string strStopBits = cbStop.Text;
Int32 iBaudRate = Convert.ToInt32(strBaudRate);
Int32 iDateBits = Convert.ToInt32(strDateBits);
sp1.BaudRate = iBaudRate; //波特率
sp1.DataBits = iDateBits; //数据位
switch (cbStop.Text) //停止位
{
case "1":
sp1.StopBits = StopBits.One;
break;
case "1.5":
sp1.StopBits = StopBits.OnePointFive;
break;
case "2":
sp1.StopBits = StopBits.Two;
break;
default:
MessageBox.Show("Error:参数不正确!", "Error");
break;
}
switch (cbParity.Text) //校验位
{
case "无":
sp1.Parity = Parity.None;
break;
case "奇校验":
sp1.Parity = Parity.Odd;
break;
case "偶校验":
sp1.Parity = Parity.Even;
break;
default:
MessageBox.Show("Error:参数不正确!", "Error");
break;
}
if (sp1.IsOpen == true)//如果打开状态,则先关闭一下
{
sp1.Close();
}
//状态栏设置
tsSpNum.Text = "串口号:" sp1.PortName "|";
tsBaudRate.Text = "波特率:" sp1.BaudRate "|";
tsDataBits.Text = "数据位:" sp1.DataBits "|";
tsStopBits.Text = "停止位:" sp1.StopBits "|";
tsParity.Text = "校验位:" sp1.Parity "|";
//设置必要控件不可用
cbSerial.Enabled = false;
cbBaudRate.Enabled = false;
cbDataBits.Enabled = false;
cbStop.Enabled = false;
cbParity.Enabled = false;
sp1.Open(); //打开串口
btnSwitch.Text = "关闭串口";
}
catch (System.Exception ex)
{
MessageBox.Show("Error:" ex.Message, "Error");
tmSend.Enabled = false;
return;
}
}
else
{
//状态栏设置
tsSpNum.Text = "串口号:未指定|";
tsBaudRate.Text = "波特率:未指定|";
tsDataBits.Text = "数据位:未指定|";
tsStopBits.Text = "停止位:未指定|";
tsParity.Text = "校验位:未指定|";
//恢复控件功能
//设置必要控件不可用
cbSerial.Enabled = true;
cbBaudRate.Enabled = true;
cbDataBits.Enabled = true;
cbStop.Enabled = true;
cbParity.Enabled = true;
sp1.Close(); //关闭串口
btnSwitch.Text = "打开串口";
tmSend.Enabled = false; //关闭计时器
}
}
//清空按钮
private void btnClear_Click(object sender, EventArgs e)
{
txtReceive.Text = ""; //清空文本
}
//退出按钮
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
//关闭时事件
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
INIFILE.Profile.SaveProfile();
sp1.Close();
}
private void txtSend_KeyPress(object sender, KeyPressEventArgs e)
{
if (radio1.Checked== true)
{
//正则匹配
string patten = "[0-9a-fA-F]|\b|0x|0X| "; //“\b”:退格键
Regex r = new Regex(patten);
Match m = r.Match(e.KeyChar.ToString());
if (m.Success )//&&(txtSend.Text.LastIndexOf(" ") != txtSend.Text.Length-1))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}//end of radio1
else
{
e.Handled = false;
}
}
private void txtSend_TextChanged(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e)
{
//设置各“串口设置”
string strBaudRate = cbBaudRate.Text;
string strDateBits = cbDataBits.Text;
string strStopBits = cbStop.Text;
Int32 iBaudRate = Convert.ToInt32(strBaudRate);
Int32 iDateBits = Convert.ToInt32(strDateBits);
Profile.G_BAUDRATE = iBaudRate ""; //波特率
Profile.G_DATABITS = iDateBits ""; //数据位
switch (cbStop.Text) //停止位
{
case "1":
Profile.G_STOP = "1";
break;
case "1.5":
Profile.G_STOP = "1.5";
break;
case "2":
Profile.G_STOP ="2";
break;
default:
MessageBox.Show("Error:参数不正确!", "Error");
break;
}
switch (cbParity.Text) //校验位
{
case "无":
Profile.G_PARITY = "NONE";
break;
case "奇校验":
Profile.G_PARITY = "ODD";
break;
case "偶校验":
Profile.G_PARITY = "EVEN";
break;
default:
MessageBox.Show("Error:参数不正确!", "Error");
break;
}
//保存设置
// public static string G_BAUDRATE = "1200";//给ini文件赋新值,并且影响界面下拉框的显示
//public static string G_DATABITS = "8";
//public static string G_STOP = "1";
//public static string G_PARITY = "NONE";
Profile.G_COMbox1=comboBox1.Text ;
Profile.G_COMbox2 = comboBox2.Text;
Profile.G_COMbox3 = comboBox3.Text;
Profile.G_CmdName[0] = txCmd0Name.Text;
Profile.G_CmdName[1] = txCmd1Name.Text;
Profile.G_CmdName[2] = txCmd2Name.Text;
Profile.G_CmdName[3] = txCmd3Name.Text;
Profile.G_CmdName[4] = txCmd4Name.Text;
Profile.G_CmdName[5] = txCmd5Name.Text;
Profile.G_CmdName[6] = txCmd6Name.Text;
Profile.G_CmdName[7] = txCmd7Name.Text;
Profile.G_CmdName[8] = txCmd8Name.Text;
Profile.G_CmdName[9] = txCmd9Name.Text;
Profile.G_CmdName[10] = txCmd10Name.Text;
Profile.G_CmdName[11] = txCmd11Name.Text;
Profile.G_CmdName[12] = txCmd12Name.Text;
Profile.G_CmdName[13] = txCmd13Name.Text;
Profile.G_CmdName[14] = txCmd14Name.Text;
Profile.G_CmdAdress[0] = txCmd0Adress.Text.Replace(" ", "");
Profile.G_CmdAdress[1] = txCmd1Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[2] = txCmd2Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[3] = txCmd3Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[4] = txCmd4Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[5] = txCmd5Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[6] = txCmd6Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[7] = txCmd7Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[8] = txCmd8Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[9] = txCmd9Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[10] = txCmd10Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[11] = txCmd11Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[12] = txCmd12Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[13] = txCmd13Adress.Text.Replace(" ", ""); ;
Profile.G_CmdAdress[14] = txCmd14Adress.Text.Replace(" ", ""); ;
Profile.G_CmdNum[0] = txCmd0Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[1] = txCmd1Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[2] = txCmd2Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[3] = txCmd3Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[4] = txCmd4Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[5] = txCmd5Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[6] = txCmd6Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[7] = txCmd7Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[8] = txCmd8Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[9] = txCmd9Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[10] = txCmd10Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[11] = txCmd11Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[12] = txCmd12Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[13] = txCmd13Num.Text.Replace(" ", ""); ;
Profile.G_CmdNum[14] = txCmd14Num.Text.Replace(" ", ""); ;
Profile.G_CmdDat[0] = txCmd0Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[1] = txCmd1Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[2] = txCmd2Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[3] = txCmd3Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[4] = txCmd4Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[5] = txCmd5Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[6] = txCmd6Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[7] = txCmd7Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[8] = txCmd8Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[9] = txCmd9Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[10] = txCmd10Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[11] = txCmd11Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[12] = txCmd12Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[13] = txCmd13Dat.Text.Replace(" ", ""); ;
Profile.G_CmdDat[14] = txCmd14Dat.Text.Replace(" ", ""); ;
Profile.SaveProfile();
}
//定时器
private void tmSend_Tick(object sender, EventArgs e)
{
//转换时间间隔
string strSecond = txtSecond.Text;
try
{
int isecond = int.Parse(strSecond) * 1000;//Interval以微秒为单位
tmSend.Interval = isecond;
if (tmSend.Enabled == true)
{
btnSend.PerformClick();//模拟一个按键按键事件
}
}
catch (System.Exception ex)
{
tmSend.Enabled = false;
MessageBox.Show("错误的定时输入!", "Error");
}
}
private void txtSecond_KeyPress(object sender, KeyPressEventArgs e)
{
string patten = "[0-9]|\b"; //“\b”:退格键
Regex r = new Regex(patten);
Match m = r.Match(e.KeyChar.ToString());
if (m.Success)
{
e.Handled = false; //没操作“过”,系统会处理事件
}
else
{
e.Handled = true;
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
btnSave.PerformClick();//模拟一个按键按键事件
Button sen = (Button)(sender);
int[] int_buf = new int[4];
try
{
for (int i = 0; i < 16; i )
{
if (sen.Name == "bt" i)
{
// string str_temp = Profile.G_CmdAdress[i - 1];
if (comboBox1.Text == "十进制")
{
int_buf[0] = Convert.ToInt32(Profile.G_CmdAdress[i]) & 0xff;
}
else if (comboBox1.Text == "二进制")
{
int_buf[0] = Convert.ToInt32(Profile.G_CmdAdress[i], 2);
}
else if (comboBox1.Text == "十六进制")
{
int_buf[0] = Convert.ToInt32(Profile.G_CmdAdress[i], 16);
}
if (comboBox2.Text == "十进制")
{
int_buf[1] = Convert.ToInt32(Profile.G_CmdNum[i ]) & 0xff;
}
else if (comboBox2.Text == "二进制")
{
int_buf[1] = Convert.ToInt32(Profile.G_CmdNum[i ], 2);
}
else if (comboBox2.Text == "十六进制")
{
int_buf[1] = Convert.ToInt32(Profile.G_CmdNum[i], 16);
}
if (comboBox3.Text == "十进制")
{
int_buf[2] = Convert.ToInt32(Profile.G_CmdDat[i]) / 256 & 0xff;
int_buf[3] = Convert.ToInt32(Profile.G_CmdDat[i]) % 256 & 0xff;
}
else if (comboBox3.Text == "二进制")
{
int_buf[2] = Convert.ToInt32(Profile.G_CmdDat[i], 2) / 256 & 0xff;
int_buf[3] = Convert.ToInt32(Profile.G_CmdDat[i], 2) % 256 & 0xff; ;
}
else if (comboBox3.Text == "十六进制")
{
int_buf[2] = Convert.ToInt32(Profile.G_CmdDat[i], 16) / 256 & 0xff; ;
int_buf[3] = Convert.ToInt32(Profile.G_CmdDat[i], 16) % 256 & 0xff; ;
}
txtSend.Text = " "
int_buf[0].ToString("X2") " "
int_buf[1].ToString("X2") " "
int_buf[2].ToString("X2") " "
int_buf[3].ToString("X2") " "
;
btnSend.PerformClick();//模拟一个按键按键事件
}
}
}
catch
{
MessageBox.Show("请确认输入格式是否正确", "错误提示");
}
}
private void label17_Click(object sender, EventArgs e)
{
}
private void comCellAdress_SelectedIndexChanged(object sender, EventArgs e)
{
txCmd0Adress.Text= comCellAdress.SelectedItem.ToString();
txCmd1Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd2Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd3Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd4Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd5Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd6Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd7Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd8Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd9Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd10Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd11Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd12Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd13Adress.Text = comCellAdress.SelectedItem.ToString();
txCmd14Adress.Text = comCellAdress.SelectedItem.ToString();
}
private void radioButton16_CheckedChanged(object sender, EventArgs e)
{
}
}
}
标签: c#上位机 测试设备
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论