}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//程序开启,串口初始化
private void Form1_Load(object sender, System.EventArgs e)
{
mycom1.PortNum=iPort;
mycom1.BaudRate=iRate;
mycom1.ByteSize=bSize;
mycom1.Parity=bParity;
mycom1.StopBits=bStopBits;
mycom1.ReadTimeout=iTimeout;
if(this.OpenCom())
msg.AppendText("串口初始化成功……\r\n");
else
msg.AppendText("串口初始化失败!\r\n");
}
//显示包信息
public string dis_package(byte[] reb)
{
string temp="";
foreach(byte b in reb)
temp =b.ToString("X2") " ";
return temp;
}
//开串口
public bool OpenCom()
{
try
{
if (mycom1.Opened)
{
mycom1.Close();
mycom1.Open(); //打开串口
}
else
{
mycom1.Open();//打开串口
}
return true;
}
catch(Exception e)
{
MessageBox.Show("错误:" e.Message);
return false;
}
}
//发送按钮
private void button1_Click(object sender, System.EventArgs e)
{
if(t_send.Text=="")
{MessageBox.Show("发送数据为空!");return;}
byte[] temp1=mysendb();
int sendnumb=0;
try
{
sendnumb=mycom1.Write(temp1);
msg.AppendText("\r\n发送数据(" sendnumb "):" dis_package(temp1));
评论加载中…
![]() |