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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4370|回復: 1
收起左側

STM32步進電機調試上位機C#源碼

[復制鏈接]
ID:452818 發表于 2018-12-23 20:26 | 顯示全部樓層 |閱讀模式
上位機運行界面:
1.png 2.png

代碼示例:
  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. }
復制代碼

全部資料51hei下載地址:
步進電機調試.zip (87.01 KB, 下載次數: 67)

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

ID:478453 發表于 2019-2-24 23:56 | 顯示全部樓層
樓主,下位機的代碼能貼上不
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产精品乱码一区二三区小蝌蚪 | 日日操天天射 | 九九精品视频在线 | 久久33| 成人在线一级片 | 日本特黄a级高清免费大片 国产精品久久性 | 成人精品在线观看 | 国产偷录视频叫床高潮对白 | 午夜国产一区 | av网站免费看 | 亚洲欧美日韩电影 | 精品国产乱码久久久久久闺蜜 | 精品国产99 | 亚洲视频免费在线播放 | 中文字幕一级毛片 | 91精品国产乱码久久久久久久 | 亚洲一区二区不卡在线观看 | 午夜免费网站 | 操久久 | 中文字幕乱码一区二区三区 | 久久神马| 欧美一级视频在线观看 | 99在线免费视频 | 日韩精品一区在线观看 | 欧美午夜精品 | 国产在线观看免费 | 九九精品在线 | 久精品视频 | 亚洲一区不卡 | 91久久国产综合久久 | 7777在线视频免费播放 | aa级毛片毛片免费观看久 | 国产视频福利一区 | 免费国产一区二区 | 欧美精品首页 | 亚洲一区二区中文字幕 | 精品亚洲一区二区三区四区五区 | 午夜影院在线观看 | 中文字幕视频免费 | 日本人麻豆 | 国产一在线 |