[英]Send data from Arduino to C# through the serial port, accumulated sensor data
[英]How to send data from ASP.Net C# to Arduino through Serial and print it on LCD?
我有一個用於登錄/注冊操作的 ASP.Net 頁面。 我想要做的是在用戶登錄時在 LCD 上顯示用戶名。我使用的硬件是 LCD Keypad Shield,而不僅僅是 LCD,如果這很重要的話。 還有可愛的 Arduino UNO。
C#端
我嘗試將用戶名存儲在一個字符數組中並一個一個地發送到 arduino,但是如果我不給它一個字符串,Serial.Write() 會給出錯誤。 我當時想立即發送全名,但 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端
#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++;
}
}
}
兩個代碼都沒有給出任何錯誤或警告。 當我將程序上傳到 Arduino 並運行登錄頁面時,我成功重定向到指定的頁面,但 LCD 上沒有顯示任何內容。
實際上這是我登錄網站后看到的。 我不知道為什么會有白細胞,它們只是在我插入電路板時出現。 但是當我上傳鍵盤屏蔽的示例程序時,這些單元格就會恢復正常。
我發現引腳號的順序很重要。 更改了LiquidCrystal lcd(4,5,6,7,8,9);
線到LiquidCrystal lcd(8,9,4,5,6,7);
我現在可以看到所需的輸出,並且啟動時也沒有白色單元格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.