简体   繁体   中英

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. 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. And the lovely Arduino UNO.

C# side

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. I wanted to send the whole name at once then, but Serial.Read() seems to be reading one at a time.

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

#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.

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); line to LiquidCrystal lcd(8,9,4,5,6,7); I can see the desired output now and there are no white cells on startup too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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