久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2099|回復: 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, 下載次數: 25)

1.png

2.png (439.16 KB, 下載次數: 25)

2.png
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:1 發表于 2018-12-24 02:07 | 只看該作者
補全工程包 原理圖或者詳細說明一下電路連接即可獲得100+黑幣
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 一区二区三区高清 | 99精品视频免费观看 | 欧美在线一二三 | 国产影音先锋 | 伊人免费网 | 国产精品久久久久久婷婷天堂 | 国产一区二区在线免费观看 | 大久 | 精品乱码久久久久 | 亚洲精品一区二区久 | 久操av在线 | aaaa日韩| 91精品一区二区三区久久久久久 | 中文在线a在线 | 国产精品www| 成年人黄色免费视频 | 日本视频一区二区 | 免费精品 | 五月综合色啪 | 亚洲+变态+欧美+另类+精品 | 日韩视频 中文字幕 | 国产日产欧产精品精品推荐蛮挑 | 免费成人高清在线视频 | 一区二区精品 | 国产精品海角社区在线观看 | 中日字幕大片在线播放 | 亚洲狠狠爱 | 欧美色999 | 色妞av| 特级丰满少妇一级aaaa爱毛片 | 日日夜夜天天久久 | 久草新视频 | 亚洲网站免费看 | 日韩在线第一 | 国产视频一区二区三区四区五区 | 日韩视频一区在线观看 | 91麻豆产精品久久久久久 | 久久小视频 | 欧美a区 | h在线免费观看 | 午夜视频一区二区三区 |