如何使用Arduino从C#读取数据 [英] How Do I Read Data Send From C# With Arduino

查看:383
本文介绍了如何使用Arduino从C#读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是C#和arduino的新手,我没能看到我在串口监视器上从c#发送的数据。以下是我的代码...:

Arduino:

  #include   <   EEPROM.h  >  
#include < SoftwareSerial.h >
SoftwareSerial xbee( 2 3 );
int 传入;
int id ;
int rea;
int comp = 0;
void setup(){
Serial.begin( 9600 );
xbee.begin( 9600 );
}

void loop(){
if (Serial.available())
{
Serial.print( hhhh);
id = Serial.read();
incoming = Serial.read();
if (id!= ' New'
{
xbee.println( id );
xbee.println(来电);
EEPROM.write( id ,incoming);
rea = EEPROM.read( id );
Serial.print( id );
}
else
{
EEPROM.write(comp,incoming);
rea = EEPROM.read( id );
Serial.print( id );
comp + = 1;
}
}
}





C#:

  protected   void  Button7_Click( object  sender,EventArgs e)
{
{
SerialPort comPort = new SerialPort( COM3 9600 ,Parity.None, 8 ,StopBits.One);
尝试
{
comPort.Open();
comPort.Write(TextBox1.Text);
comPort.Write( 删除);
comPort.Close();
}
catch (UnauthorizedAccessException ex)
{
Response.Write( 错误: + ex.ToString());
}
尝试
{
OleDbConnection conn = new OleDbConnection( Provider = Microsoft.Jet.OLEDB.4.0; Data Source = + Server.MapPath ( App_Data \\Stock.mdb));
conn.Open();
Int32 ash = Convert.ToInt32(TextBox1.Text);
string deleteQuery = 从Instock中删除ID =' + ash + ';
OleDbCommand com = new OleDbCommand(deleteQuery,conn);
com.ExecuteNonQuery();
Response.Write( 项目已成功删除);
Response.Redirect( UpdateDB.aspx);
conn.Close();
}
catch (例外情况)
{
Response.Write( 错误: + ex.ToString());
}
}
}

解决方案

本文提供了工作代码。节省一些时间。

Arduino,C#和串行接口 [ ^

am still new in C# and arduino, am failing to see the data i send from c# on serial monitor. below are my codes...:
Arduino:

#include <EEPROM.h>
#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3);
int incoming;
int id;
int rea;
int comp=0;
void setup() {
  Serial.begin(9600);
  xbee.begin( 9600 );
 }

void loop() {
  if(Serial.available())
   {
     Serial.print("hhhh");
     id = Serial.read();
     incoming = Serial.read();
     if(id!='New')
     {
     xbee.println(id);
     xbee.println(incoming);
     EEPROM.write(id, incoming);
     rea = EEPROM.read(id);
     Serial.print(id);
    }
    else
     {
      EEPROM.write(comp, incoming);
      rea = EEPROM.read(id);
      Serial.print(id);
      comp+=1;
     }
   }
}



C#:

protected void Button7_Click(object sender, EventArgs e)
        {
            {
                SerialPort comPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
                try
                {
                    comPort.Open();
                    comPort.Write(TextBox1.Text);
                    comPort.Write("Delete");
                    comPort.Close();
                }
                catch (UnauthorizedAccessException ex)
                {
                    Response.Write("Error:" + ex.ToString());
                }
                try
                {
                    OleDbConnection conn = new OleDbConnection("Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("App_Data\\Stock.mdb"));
                    conn.Open();
                   Int32 ash = Convert.ToInt32(TextBox1.Text);
                    string deleteQuery = "delete from Instock where ID= '" + ash + "'";
                    OleDbCommand com = new OleDbCommand(deleteQuery, conn);
                    com.ExecuteNonQuery();
                    Response.Write("Item deleted successfully");
                    Response.Redirect("UpdateDB.aspx");
                    conn.Close();
                }
                catch (Exception ex)
                {
                    Response.Write("Error:" + ex.ToString());
                }
            }
        }

解决方案

This article provides working code. Save yourself some time.
Arduino, C#, and Serial Interface[^]


这篇关于如何使用Arduino从C#读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆