找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4279|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

STM32步進(jìn)電機(jī)調(diào)試上位機(jī)C#源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:452818 發(fā)表于 2018-12-23 20:26 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
上位機(jī)運(yùn)行界面:


代碼示例:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;

  10. namespace 步進(jìn)電機(jī)調(diào)試
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.             System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
  18.         }

  19.         static Int16 speed_add = 0x01;
  20.         static Int16 direction_data_clockwise = 0x11;            //順時(shí)針
  21.         static Int16 direction_data_anticlockwise = 0x12;        //逆時(shí)針


  22.         private void button1_Click(object sender, EventArgs e)
  23.         {
  24.             if (textBox2.Text != "" & textBox3.Text != "" & (radioButton1.Checked | radioButton2.Checked) & comboBox1.Text != "" & comboBox2.Text != "")
  25.             {
  26.                 if (button1.Text == "打開(kāi)串口")
  27.                 {
  28.                     serialPort1.PortName = comboBox1.Text;
  29.                     serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
  30.                     try
  31.                     { serialPort1.Open(); }
  32.                     catch { MessageBox.Show("串口不存在"); }
  33.                     button1.Text = "關(guān)閉串口";
  34.                     //label3.Text = serialPort1.ReadExisting();

  35.                 }
  36.                 else
  37.                 {
  38.                     button1.Text = "打開(kāi)串口";
  39.                     serialPort1.Close();
  40.                 }
  41.             }
  42.             else
  43.             { MessageBox.Show("請(qǐng)?zhí)钊胂嚓P(guān)參數(shù)"); }
  44.         }



  45.         byte[] data = new byte[5];//方向+(速度1+速度2)+圈數(shù)+路程

  46.         private void trackBar1_Scroll(object sender, EventArgs e)
  47.         {
  48.             ushort y, x;
  49.             if (radioButton1.Checked)
  50.             {
  51.                 data[0] = Convert.ToByte(direction_data_clockwise);//將方向數(shù)據(jù)放入data中
  52.             }
  53.             else
  54.             {
  55.                 data[0] = Convert.ToByte(direction_data_anticlockwise);
  56.             }
  57.             serialPort1.Write(data, 0, 1);
  58.             //速度1
  59.             textBox1.Text = trackBar1.Value.ToString();      //滾動(dòng)條的值轉(zhuǎn)為字符串顯示于Textbox1
  60.             y = Convert.ToUInt16(trackBar1.Value);
  61.             data[1] = Convert.ToByte((y & 0xff00) >> 8);
  62.             serialPort1.Write(data, 1, 1);
  63.             //速度2
  64.             data[2] = Convert.ToByte(y & 0x00ff);
  65.             serialPort1.Write(data, 2, 1);
  66.             //圈速
  67.             data[3] = Convert.ToByte(textBox2.Text);
  68.             serialPort1.Write(data, 3, 1);
  69.             //角度
  70.             data[4] = Convert.ToByte(textBox3.Text);
  71.             serialPort1.Write(data, 4, 1);
  72.             label10.Text = "轉(zhuǎn)動(dòng)";
  73.         }

  74.         private void textBox1_TextChanged(object sender, EventArgs e)
  75.         {

  76.             try
  77.             {
  78.                 trackBar1.Value = Convert.ToInt32(textBox1.Text);
  79.             }
  80.             catch
  81.             {

  82.             }
  83.         }

  84.         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
  85.         {
  86.             Int16 a;
  87.             a = Convert.ToInt16(serialPort1.ReadExisting());
  88.             if (a == 0x88)
  89.             {
  90.                 label10.Text = "停止";
  91.             }
  92.         }

  93.         private void Form1_Load(object sender, EventArgs e)
  94.         {

  95.         }

  96.         private void radioButton2_CheckedChanged(object sender, EventArgs e)
  97.         {


  98.         }

  99.         private void radioButton2_Click(object sender, EventArgs e)
  100.         {
  101.             /* try
  102.              {
  103.                  ushort y;
  104.                  if (radioButton1.Checked)
  105.                  {
  106.                      data[0] = Convert.ToByte(direction_data_clockwise);//將方向數(shù)據(jù)放入data中
  107.                  }
  108.                  else
  109.                  {
  110.                      data[0] = Convert.ToByte(direction_data_anticlockwise);
  111.                  }
  112.                  serialPort1.Write(data, 0, 1);
  113.                  textBox1.Text = trackBar1.Value.ToString();      //滾動(dòng)條的值轉(zhuǎn)為字符串顯示于Textbox1

  114.                  y = Convert.ToUInt16(trackBar1.Value);
  115.                  data[1] = Convert.ToByte((y & 0xff00) >> 8);
  116.                  serialPort1.Write(data, 1, 1);
  117.                  data[2] = Convert.ToByte(y & 0x00ff);
  118.                  serialPort1.Write(data, 2, 1);
  119.              }
  120.              catch { MessageBox.Show("出錯(cuò),檢查串口"); }*/
  121.         }

  122.         private void radioButton1_Click(object sender, EventArgs e)
  123.         {
  124.             /* ushort y;
  125.              if (radioButton1.Checked)
  126.              {
  127.                  data[0] = Convert.ToByte(direction_data_clockwise);//將方向數(shù)據(jù)放入data中
  128.              }
  129.              else
  130.              {
  131.                  data[0] = Convert.ToByte(direction_data_anticlockwise);
  132.              }
  133.              serialPort1.Write(data, 0, 1);
  134.              textBox1.Text = trackBar1.Value.ToString();      //滾動(dòng)條的值轉(zhuǎn)為字符串顯示于Textbox1

  135.              y = Convert.ToUInt16(trackBar1.Value);
  136.              data[1] = Convert.ToByte((y & 0xff00) >> 8);
  137.              serialPort1.Write(data, 1, 1);
  138.              data[2] = Convert.ToByte(y & 0x00ff);
  139.              serialPort1.Write(data, 2, 1);*/
  140.         }

  141.         private void button2_Click(object sender, EventArgs e)
  142.         {
  143.             try
  144.             {
  145.                 ushort y, x;
  146.                 if (radioButton1.Checked)
  147.                 {
  148.                     data[0] = Convert.ToByte(direction_data_clockwise);//將方向數(shù)據(jù)放入data中
  149.                 }
  150.                 else
  151.                 {
  152.                     data[0] = Convert.ToByte(direction_data_anticlockwise);
  153.                 }

  154.                 serialPort1.Write(data, 0, 1);
  155.                 //速度1
  156.                 textBox1.Text = trackBar1.Value.ToString();      //滾動(dòng)條的值轉(zhuǎn)為字符串顯示于Textbox1
  157.                 y = Convert.ToUInt16(trackBar1.Value);
  158.                 data[1] = Convert.ToByte((y & 0xff00) >> 8);
  159.                 serialPort1.Write(data, 1, 1);
  160.                 //速度2
  161.                 data[2] = Convert.ToByte(y & 0x00ff);
  162.                 serialPort1.Write(data, 2, 1);
  163.                 //圈速
  164.                 data[3] = Convert.ToByte(textBox2.Text);
  165.                 serialPort1.Write(data, 3, 1);
  166.                 //角度
  167.                 data[4] = Convert.ToByte(textBox3.Text);
  168.                 serialPort1.Write(data, 4, 1);

  169.                 label10.Text = "轉(zhuǎn)動(dòng)";
  170.             }
  171.             catch
  172.             {
  173.                 MessageBox.Show("請(qǐng)檢查錯(cuò)誤");
  174.             }
  175.         }

  176.     }
  177. }
復(fù)制代碼

全部資料51hei下載地址:
步進(jìn)電機(jī)調(diào)試.zip (87.01 KB, 下載次數(shù): 67)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:478453 發(fā)表于 2019-2-24 23:56 | 只看該作者
樓主,下位機(jī)的代碼能貼上不
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表