我试着用arduino uno和C#控制伺服,但是伺服不动。
以下是Arduino代码:
#include <Servo.h>
Servo servoT1;
void setup()
{
Serial.begin(9600);
servoT1.attach(9);
}
int i=0;
void loop()
{
if (Serial.available() > 0)
{
i=Serial.read();
i=map(i, 0, 100, 0, 179);
servoT1.write(i);
}
}下面是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.IO.Ports;
namespace arduino_throttle
{
public partial class Form1 : Form
{
SerialPort uno1 = new SerialPort("COM3", 9600);
public Form1()
{
InitializeComponent();
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
uno1.Open();
uno1.Write(HScroll.ToString());
uno1.Close();
}
}
}我想通过在程序中滚动hScrollBar来设置伺服角度。
发布于 2014-06-15 22:06:01
serial.read ()方法返回传入串行数据的第一个字节可用http://arduino.cc/en/Serial/Read,但您正在发送字符串(字符数组)尝试仅从C#发送一个字节或字符为此,您需要检查最大值是否为256
另外,您需要发送scrollbar的值(以字符串的形式发送整个对象)
尝尝这个
uno1.Write(Convert.ToByte(HScroll.value));
https://stackoverflow.com/questions/24230131
复制相似问题