arduino控制舵机,上位机C# 或winform控制舵机
1、第一步,烧录arduino程序。源程序如下:
#include <Servo.h> //引入lib
Servo myservo; // 创建一个伺服电机对象
char inByte = 0; //串口接收的数据
int angle = 0; //角度值
String temp = "";//临时字符变量,又或者说是缓存用的吧
void setup()
{
myservo.attach(9); //定义舵机的引脚为9,舵机只能是10,或者9引脚
Serial.begin(9600); //设置波特率
}
void loop()
{
while (Serial.available() > 0) //判断串口是否有数据
{
inByte = Serial.read();//读取数据,串口一次只能读1个字符
temp += inByte;//把读到的字符存进临时变量里面缓存,
//再继续判断串口还有没有数据,知道把所有数据都读取出来
}
if(temp != "") //判断临时变量是否为空
{
angle = temp.toInt(); //把变量字符串类型转成整型
Serial.println(angle); //输出数据到串口上,以便观察
}
temp = "";//请看临时变量
myservo.write(angle); //控制舵机转动相应的角度。
delay(50);//延时100毫秒
}
大家有什么问题还可以问我。

2、第二步,arduino与舵机连线,如下图所示。

3、第三步,VS2010新建项目,打开vs2010,新建项目——其他语言——选择C#


4、第四部,写C#上位机程序
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;
using System.Threading;
using System.IO.Ports;
using System.Diagnostics;
using System.Globalization;
using System.Text.RegularExpressions;
namespace 遥控器
{
public partial class Form1 : Form
{
Stopwatch sw = new Stopwatch();
bool drawornot = false;
SerialPort serialPort1 = new SerialPort();
public Form1()
{
InitializeComponent();
sw.Start();
Control.CheckForIllegalCrossThreadCalls = false; //防止跨线程出错
//定时器中断时间
comboBox1.Items.Add("1200");
comboBox1.Items.Add("2400");
comboBox1.Items.Add("4800");
comboBox1.Items.Add("9600");
comboBox1.Items.Add("14400");
comboBox1.Items.Add("19200");
comboBox1.Items.Add("28800");
comboBox1.Items.Add("38400");//常用的波特率
try
{
string[] ports = SerialPort.GetPortNames();//得到接口名字
//将端口列表添加到comboBox
this.comboBox2.Items.AddRange(ports);
///设置波特率
serialPort1.BaudRate = Convert.ToInt32(comboBox1.Text);
}
catch (Exception ex)
{
}
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.A)
{
button3.PerformClick();
}
if (keyData == Keys.D)
{
button2.PerformClick();
}
return true;
}
private void button5_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)////(更新)如果按下按钮之前串口是开的,就断开//如果按下按钮之前 flag的内容是false 按下之后 内容改成true 然后打开串口
{
serialPort1.Close();
button5.Text = "连 接";
}
else
{
//要打开串口要看波特率 串口等有没有设置对
bool no_error_flag = true;
try
{
serialPort1.BaudRate = Convert.ToInt32(comboBox1.SelectedItem);
}
catch (ArgumentException e1)
{
this.errorProvider1.SetError(this.comboBox1, "不能为空");
no_error_flag = false;
}
try
{
serialPort1.PortName = Convert.ToString(comboBox2.SelectedItem);
}
catch (ArgumentException e2)
{
this.errorProvider1.SetError(this.comboBox2, "不能为空");
no_error_flag = false;
}
try
{
serialPort1.Open();
}
catch
{
MessageBox.Show("端口错误", "警告");
no_error_flag = false;
}
if (no_error_flag)
{
button5.Text = "断开连接";
}
}
}
int i=0;
int angle=0 ;
private void button3_Click(object sender, EventArgs e)
{
angle=angle+i;
if (angle >180)
{ angle = 180; }
string stm = Convert.ToString(angle);
serialPort1.Write(stm);
}
private void button2_Click(object sender, EventArgs e)
{
angle = angle - i;
if(angle<0)
{ angle = 0; }
string stm = Convert.ToString(angle);
serialPort1.Write(stm);
}
private void button6_Click_2(object sender, EventArgs e)
{
string message = textBox1.Text;
serialPort1.Write(message);
}
private void button7_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void button1_Click_1(object sender, EventArgs e)
{
i = Convert.ToInt16(textBox2.Text);
angle = Convert.ToInt16(textBox3.Text);
}
private void button4_Click(object sender, EventArgs e)
{
textBox2.Clear();
}
private void button8_Click(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
this.comboBox2.Items.Clear();
this.comboBox2.Items.AddRange(ports);
}
private void button10_Click(object sender, EventArgs e)
{
textBox3.Clear();
}
}
}
这是程序啦,下面我们看一下效果
