简体   繁体   English

如何通过串口将数据从 ASP.Net C# 发送到 Arduino 并在 LCD 上打印?

[英]How to send data from ASP.Net C# to Arduino through Serial and print it on LCD?

I have an ASP.Net page for login/register operations.我有一个用于登录/注册操作的 ASP.Net 页面。 What I'm trying to do is show the user name on LCD when the user logs in. The hardware I'm using is LCD Keypad Shield not just LCD if that matters.我想要做的是在用户登录时在 LCD 上显示用户名。我使用的硬件是 LCD Keypad Shield,而不仅仅是 LCD,如果这很重要的话。 And the lovely Arduino UNO.还有可爱的 Arduino UNO。

C# side C#端

I tried storing the username in a char array and send to arduino one by one but the Serial.Write() gives error if I don't give a string to it.我尝试将用户名存储在一个字符数组中并一个一个地发送到 arduino,但是如果我不给它一个字符串,Serial.Write() 会给出错误。 I wanted to send the whole name at once then, but Serial.Read() seems to be reading one at a time.我当时想立即发送全名,但 Serial.Read() 似乎一次读取一个。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.IO.Ports;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace EComm
{
    public partial class Login : System.Web.UI.Page
    {
        SerialPort sp;
        protected void Page_Load(object sender, EventArgs e)
        {
            sp = new SerialPort();
            sp.PortName = "COM13";
            sp.BaudRate = 9600;
            txtPass.TextMode = TextBoxMode.Password;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DBDataContext db = new DBDataContext();
            db.Connection.Open();
            var users = from allusers in db.Users select allusers;
            foreach (User _user in users)
            {
                if (txtUser.Text == _user.UserName.Trim())
                    if (txtPass.Text == _user.UserPassword.Trim())
                    {
                        Session["User"] = _user.UserName;
                        String outgoing = Session["User"].ToString();
                        for (int i = 0; i < outgoing.Length; i++)
                        {
                            sp.Open();
                            sp.Write(outgoing[i].ToString());
                            sp.Close();
                        }
                        Response.Redirect("Default.aspx");
                    }
            }
        }

Arduino side Arduino端

#include <LiquidCrystal.h>
char usrName[10];
char incomingChar;
byte index=0;

LiquidCrystal lcd(4,5,6,7,8,9);
int baud = 9600; 
int x=0;
int y=0;

void setup()
{
 lcd.begin(16,2);
 lcd.setCursor(x,y); 
 Serial.begin(baud);
}

void loop()
{
    while(Serial.available()>0){
    if(index<10){
      y=1;
      incomingChar=Serial.read();
      usrName[index]=incomingChar;
      lcd.setCursor(x,y);
      lcd.print(usrName[index]);
      index++;
      x++;
    }
}
}

Both codes do not give any errors or warnings.两个代码都没有给出任何错误或警告。 When I upload the program to Arduino and run the login page, I am succesfully redirected to the page stated but nothing shows up on the LCD.当我将程序上传到 Arduino 并运行登录页面时,我成功重定向到指定的页面,但 LCD 上没有显示任何内容。

Actually this is what I see after I login to the site.实际上这是我登录网站后看到的。 I have no idea why there are white cells, they just show up when I plug in the board.我不知道为什么会有白细胞,它们只是在我插入电路板时出现。 But when I upload an example program for keypad shield those cells just turn to normal.但是当我上传键盘屏蔽的示例程序时,这些单元格就会恢复正常。

在此处输入图片说明

在此处输入图片说明

I found out that the order of the pin numbers mattered.我发现引脚号的顺序很重要。 Changed the LiquidCrystal lcd(4,5,6,7,8,9);更改了LiquidCrystal lcd(4,5,6,7,8,9); line to LiquidCrystal lcd(8,9,4,5,6,7);线到LiquidCrystal lcd(8,9,4,5,6,7); I can see the desired output now and there are no white cells on startup too.我现在可以看到所需的输出,并且启动时也没有白色单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM