如何从Arduino nano打印两个不同的vaule到c#程序 [英] How to print two different vaules from Arduino nano to a c# program
问题描述
我最近为了个人目的买了一个Arduino nano。我还开发了一个草图,可以从纳米读取电压,可以打开/关闭引脚D2,并获得引脚d2的当前状态(开或关)(使用变量i的状态)。我的目的是用相同的C#程序手动测量电压和开/关引脚d2,以便同时了解引脚的状态。我现在开发了一个程序,我将结果从nano打印到标签上,看看引脚是打开还是关闭(那时草图只是开/关引脚d2的一点点)。
现在我在草图上添加了代码来测量电压。但我不知道如何从这个草图中获得2个不同的用于在c#中完成2个不同的工作。请帮忙。
=======
sketch
===== ==
I recently bought a Arduino nano for my personal purpose. I also developed a sketch that can read voltage from nano and can on/off pin D2 and to get the current status of the pin d2(on or off)( status using the variable i). My purpose is to measure voltage and on/off pin d2 manually with same C# programand to know the status of pin at same time . i developed a program now i am printing the result from nano to a label to see whether the pin is on or off(at that time sketch was little bit only for on/off pin d2).
now i added codes to the sketch to measure the voltage. but i dont know how to get 2 different out from this sketch for doing 2 different work in c#. any one help please.
=======
sketch
=======
int LED= 2;
char data;
char x='f';
int analogpin=3;
void setup() {
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
pinMode(analogpin,INPUT);
Serial.begin(9600);
}
}
void loop() {
////////////////
int value = analogRead(analogpin);
Serial.println(value);
////////////////////////////////
data= Serial.read();
if (data=='o')
{
digitalWrite(LED,HIGH);
x = data;
}
else if (data=='f')
{
digitalWrite(LED,LOW);
x = data;
}
///////////////////////////////////
if (data !='o'||data !='f')
{
data=x;
}
//////////////////////////////////////
char i;
if (data == 'o')
{
i='O';
}
else if (data == 'f')
{
i= 'F';
}
else if (data == ' ')
{
i='F';
}
Serial.println(i);
delay(1000);
}
============================
c#开/关引脚D2的代码
============================
============================
c# code for on/off pin D2
============================
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public SerialPort myport;
public Form1()
{
InitializeComponent();
init();
}
private void button1_Click(object sender, EventArgs e)
{
myport.WriteLine("o");
on.Enabled = false;
off.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = "COM5";
myport.Open();
}
catch (Exception)
{
MessageBox.Show("Error");
}
}
private void init()
{
on.Enabled = true;
off.Enabled = false;
}
private void off_Click(object sender, EventArgs e)
{
myport.WriteLine("f");
on.Enabled = true;
off.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text =myport.ReadLine();
}
}
}
推荐答案
简单地添加<出了什么问题? code>'v'命令你的{'o','f'}
命令设置只是为了从arduino获得电压?
[更新]
类似
What's wrong in simply adding a'v'
command to your{'o', 'f'}
command set just to get the voltage from the arduino?
[update]
Something like
if (data=='o')
{
digitalWrite(LED,HIGH);
}
else if (data=='f')
{
digitalWrite(LED,LOW);
}
else if ( data=='v')
{
// write voltage measured value on the serial port
}
else
{// handle error (unrecognized command)
}
当然你还要适当地适应 C#
方。
[/ update]
Of course you have also to adapt properly the C#
side.
[/update]
这篇关于如何从Arduino nano打印两个不同的vaule到c#程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!