找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1740|回复: 1
打印 上一主题 下一主题
收起左侧

STM32步进电机调试上位机

[复制链接]
跳转到指定楼层
楼主
代码示例:
  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 步进电机调试
  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;            //顺时针
  21.         static Int16 direction_data_anticlockwise = 0x12;        //逆时针


  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 == "打开串口")
  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 = "关闭串口";
  34.                     //label3.Text = serialPort1.ReadExisting();

  35.                 }
  36.                 else
  37.                 {
  38.                     button1.Text = "打开串口";
  39.                     serialPort1.Close();
  40.                 }
  41.             }
  42.             else
  43.             { MessageBox.Show("请填入相关参数"); }
  44.         }



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

  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);//将方向数据放入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();      //滚动条的值转为字符串显示于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 = "转动";
  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);//将方向数据放入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();      //滚动条的值转为字符串显示于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("出错,检查串口"); }*/
  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);//将方向数据放入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();      //滚动条的值转为字符串显示于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);//将方向数据放入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();      //滚动条的值转为字符串显示于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 = "转动";
  170.             }
  171.             catch
  172.             {
  173.                 MessageBox.Show("请检查错误");
  174.             }
  175.         }

  176.     }
  177. }
复制代码



1.png (12.12 KB, 下载次数: 19)

1.png

2.png (439.16 KB, 下载次数: 17)

2.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2018-12-24 02:07 | 只看该作者
补全工程包 原理图或者详细说明一下电路连接即可获得100+黑币
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表