C#串口控制舵機、arduino源碼 及C#源碼及界面

原文 C#串口控制舵機、arduino源碼 及C#源碼及界面c#

1.舵機原理簡介oop

       wKiom1X83d_A7UpBAAEyGBJFunE380.jpg

控制信號由接收機的通道進入信號調製芯片,得到直流偏置電壓。它內部有一個基準電路,產生週期爲20ms,寬度爲1.5ms的基準信號,將得到的直流偏置電壓與電位器的電壓比較,得到電壓差輸出。最後,電壓差的正負輸出到電機驅動芯片決定電機的正反轉。當電機轉速必定時,經過級聯減速齒輪帶動電位器旋轉,使得電壓差爲0,電機中止轉動。 ui

舵機的控制通常須要一個20ms左右的時基脈衝,該脈衝的高電平部分通常爲0.5ms-2.5ms範圍內的角度控制脈衝部分,總間隔爲2ms。以180度角度伺服爲例,那麼對應的控制關係是這樣的:   this

0.5ms--------------0度;    spa

1.0ms------------45度;    3d

1.5ms------------90度;    code

2.0ms-----------135度;    orm

2.5ms-----------180度;blog

 

2.arduino下位機源碼事件

相關源碼已壓縮上傳,須要請下載對應附件

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<Servo.h>  //庫文件
Servo servo1;     
static  int  v=0;
String mycommand= "" ;
void  setup()
   {
     Serial.begin(9600); //此處爲串口設置的波特率 ,能夠修改 必須同上位機設置的波特路一致。
     servo1.attach(3);   // 控制的端口是~3號
     servo1.write(90);
   }
void  loop() 
  {
     while (Serial.available()>0)
     {
       mycommand+= char (Serial.read());
       delay(2);
     }
     for ( int  m=0;m<mycommand.length();m++)  // 
        {
           char  ch = mycommand[m];    //讀取串口數據
           switch (ch)
              {
                  case  '0' ... '9' :
                        v = v*10 + ch -  '0' ;    //字符轉換成十進制
                         break ;
                   case  'a' :    //若是數據後帶a,則表示是一號舵機的數據,好比串口發送85a
                         //if(v >= 5 || v <= 175 )   
                          servo1.write(v);  // 讓A從66度旋轉到9度 (可修改角度)                     
                          //用於設定舵機旋轉角度的語句,可設定的角度範圍是0°到180°,「V」獲得所輸入的值而改變角度,好比85a爲85度角
                          Serial.println(v+ "°" ); //舵機角度改變後 發送改變的角度到上位機。
                          v = 0;
                           break ;
                      }
    
           }  
    mycommand= "" ;
}



    wKiom1X85Y7CkQbwAD6l1QJr6rA555.jpg

     

我選擇個人是arduino Uno,舵機的接線方法是紅色(VCC)端接控制板的5V處,棕色端接板子的GND,舵機的橙色線爲信號線,接板子上的3號口;

 

 

 

 

 

 

3.C#上位機源碼及界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
using  System.Windows.Forms;
namespace  舵機上位機源碼
{
     public  partial  class  Form1 : Form
     {
         bool  open =  false ;
         public  delegate  void  HandleInterfaceUpdataDelegate( string  text);
         private  HandleInterfaceUpdataDelegate interfaceUpdataHandle;
         int  a;
         public  Form1()
         {
             InitializeComponent();
         }
         private  void  Form1_Load( object  sender, EventArgs e)
         {
             comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames()); //從系統獲取已有串口
             if  (comboBox1.Items.Count > 0)
             {
                 comboBox1.SelectedIndex = 0; //串口變量初始化
                 serialPort1.RtsEnable =  true ; //DataReceived事件委託
                 serialPort1.ReceivedBytesThreshold = 1; //設置 DataReceived 事件發生前內部輸入緩衝區中的字節數
                 serialPort1.DataReceived +=  new  System.IO.Ports.SerialDataReceivedEventHandler( this .serialPort1_DataReceived);
                 comboBox2.SelectedIndex = 6;
             }
             else
             {
                 MessageBox.Show( "未檢測到設備\r\n" );
             }
         }
         //監聽串口
         private  void  serialPort1_DataReceived( object  sender, System.IO.Ports.SerialDataReceivedEventArgs e)
         {
             try
             {
                 string  text =  string .Empty;
                 byte [] result =  new  byte [serialPort1.BytesToRead];
                 serialPort1.Read(result, 0, serialPort1.BytesToRead);
                 text = Encoding.UTF8.GetString(result);
             }
             catch 
             {
                
             }
         }
         //串口刷新按鈕
         private  void  button2_Click( object  sender, EventArgs e)
         {
             comboBox1.Items.Clear();
             comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
             if  (comboBox1.Items.Count > 0)
             {
                 comboBox1.SelectedIndex = 0;
             }
             else
             {
                 MessageBox.Show( "未檢測到串口\r\n" );
             }
         }
         
         //打開串口
         private  void  btnOpen_Click( object  sender, EventArgs e)
         {
             if  (open ==  false )
             {
                 
                 if  (serialPort1.IsOpen)
                 {
                     MessageBox.Show( "串口已經打開" "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information);
                     return ;
                 }
                 //串口
                 if  (comboBox1.Text ==  string .Empty)
                 {
                     MessageBox.Show( "請選擇串口" "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information);
                     return ;
                 }
                 //波特率
                 if  (comboBox2.Text ==  string .Empty)
                 {
                     MessageBox.Show( "請選擇波特率" "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information);
                     return ;
                 }
                 serialPort1.BaudRate =  int .Parse(comboBox2.Text);
                 try
                 {
                     serialPort1.PortName = comboBox1.SelectedItem.ToString();
                     serialPort1.Open();
                 }
                 catch
                 {
                     try
                     {
                         comboBox1.SelectedIndex = comboBox1.SelectedIndex + 1;
                     }
                     catch
                     {
                         comboBox1.SelectedIndex = 0;
                     }
                     serialPort1.Close();
                 }
                 btnOpen.Text =  "關閉" ;
                 comboBox1.Enabled =  false ;
                 comboBox2.Enabled =  false ;
                 open =  true ;
                 trackBarSend_Scroll( this null );
                 btnReserch.Enabled =  false ;
                 btnsend.Enabled =  true ;
                 tbxSend.Enabled =  true ;
                 trackBarSend.Enabled =  true ;
                 pictureBox1.BackColor = Color.Lime;
             }
             else
             {
                 try
                 {
                     serialPort1.Close();
                     btnOpen.Text =  "打開" ;
                     open =  false ;
                     comboBox1.Enabled =  true ;
                     comboBox2.Enabled =  true ;
                     btnReserch.Enabled =  true ;
                     btnsend.Enabled =  false ;
                     tbxSend.Enabled =  false ;
                     trackBarSend.Enabled =  false ;
                     pictureBox1.BackColor = Color.Silver;
                 }
                 catch
                 {
                 }
             }
         }
         private  void  trackBarSend_Scroll( object  sender, EventArgs e)
         {
             if  (serialPort1.IsOpen)
             {
                 a = trackBarSend.Value;
                 string  duojiA = trackBarSend.Value.ToString() +  "a" ;
                 try
                 {
                     serialPort1.WriteLine(duojiA);
                     tbxSend.Text = a.ToString(); ; ;
                 }
                 catch
                 {
                 }
             }
         }
         private  void  btnsend_Click( object  sender, EventArgs e)
         {
             try
             {
                 byte [] SendBuf =  new  byte [100000];
                 SendBuf = System.Text.Encoding.Default.GetBytes(tbxSend.Text+ "a" );
                 serialPort1.Write(SendBuf, 0, SendBuf.Length);
             }
             catch  (Exception err)
             {
                 if  (serialPort1.IsOpen)
                     serialPort1.Close(); //若是是寫數據時出錯,此時窗口狀態爲開,就應關閉串口,防止下次不能使用,串口是不能重複打開和關閉的
                 MessageBox.Show(err.ToString(),  "錯誤" );
             }
         }
         private  void  tbxSend_ValueChanged( object  sender, EventArgs e)
         {
             trackBarSend.Value = ( int )tbxSend.Value;
         }
     }
}

 

   

wKioL1X89RzTSBEvAAC-pgj9rOE503.jpg

wKiom1X88uLiaNuNAACSH9kDILg122.jpg

                                                            未鏈接設備狀態

wKioL1X89R2TOm6tAACW11E8wdI166.jpg

                                                                鏈接設備後

wKiom1X883uRwFneAAGPDChUgtI158.jpg

                                            這是本身作的機械臂控制軟件 相關源碼也有

wKioL1X89bahD3yCAAW1gGFplBI493.jpg

                                                            這是wifi智能小車控制軟件

 

 

第一次寫博文,有很差的地方還請多多包涵。

相關文章
相關標籤/搜索