我的目标是创建一个项目,该项目能够使用C#中的另一个简单Windows窗体应用程序使用NodeMcu esp 8266控制一个简单系统。
但是我的代码有一些错误,我不知道为什么。所以我想有人帮我做这件事。
错误:
// Programação do controle de dispositivos via Serial com ESP8266
#include <Servo.h>
// #include <Process.h>
Servo servo;
const int buzzer = D2;
const int lamp1 = D4;
const int lamp2 = D0;
const int pinServo = D3;
String serialData;
// String comando;
bool estadoBuzzer = LOW;
bool estadoLam1 = LOW;
bool estadoLam2 = LOW;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//SerialUSB.begin(115200);
pinMode(buzzer, OUTPUT);
pinMode(lamp1, OUTPUT);
pinMode(lamp2, OUTPUT);
servo.attach(pinServo);
Serial.setTimeout(10);
//Bridge.begin();
//while (!SerialUSB);
}
void loop() {
// put your main code here, to run repeatedly:
serialEvent(Serial.readString());
}
void serialEvent(String data){
/*Process p;
comando = "echo " + data + " > teste_arduino_serial_comandos.txt";
p.runShellCommand(comando);
*/
if(data.charAt(0) == 'B'){
if(estadoBuzzer == HIGH){
digitalWrite(buzzer, LOW);
estadoBuzzer = LOW;
} else {
digitalWrite(buzzer, HIGH);
estadoBuzzer = HIGH;
}
} else if(data.charAt(0) == 'L'){
data.remove(0);
if(data.toInt() == 1){
lampada1();
} else {
lampada2();
}
} else if(data.charAt(0) == 'A'){
data.remove(0);
int angulo = data.toInt();
Serial.println(angulo);
if(angulo >= 0 && angulo <= 180){
servo.write(angulo);
}
}
}
void lampada1(){
if(estadoLam1 == HIGH){
digitalWrite(lamp1, LOW);
estadoLam1 = LOW;
} else {
digitalWrite(lamp1, HIGH);
estadoLam1 = HIGH;
}
}
void lampada2(){
if(estadoLam2 == HIGH){
digitalWrite(lamp2, LOW);
estadoLam2 = LOW;
}
else {
digitalWrite(lamp2, HIGH);
estadoLam2 = HIGH;
}
}
这是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.Diagnostics;
namespace ControleDeDispositivosViaSerialNodeMcuEsp8266
{
public partial class Form1 : Form
{
public Stopwatch watch { get; set; }
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
watch = Stopwatch.StartNew();
port.Open();
}
public void writeToPort(String indicador, String valor="0")
{
if (watch.ElapsedMilliseconds > 15)
{
watch = Stopwatch.StartNew();
port.Write(String.Format(indicador+valor));
System.Windows.Forms.MessageBox.Show(indicador + valor);
}
// System.Windows.Forms.MessageBox.Show(port.ReadExisting());
}
private void anguloBtn_Click(object sender, EventArgs e)
{
writeToPort("A", angulos.Text);
angulos.Refresh();
}
private void onoff1_Click(object sender, EventArgs e)
{
writeToPort("L", "1");
}
private void onoff2_Click(object sender, EventArgs e)
{
writeToPort("L", "2");
}
private void buzzerBtn_Click(object sender, EventArgs e)
{
writeToPort("B");
}
}
}
答案 0 :(得分:0)
您的data.toInt()无法正常工作,这就是为什么它在伺服器上总是只出现一盏灯并且位置相同的原因。
data.Remove(0)将在第一个字符之后删除所有内容,因此将为空。
使用数据=数据。子串(1)
我通常做为检查反馈的内容是这样写的:Serial.println(data)etcto检查我是否真的按照我的想法工作。