我用温湿度传感器显示在1602上,温度是26,相对湿度是56%,但是我用串口传输显示在c#界面上就成了温度100+,湿度100+,我把c#程序粘出来,希望得到大家的指点。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Data.OleDb;//用OLEDB连接数据库的命名空间namespace XSwenshidu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SerialPort sp = new SerialPort();
static int buffersize = 18;
byte[] buffer = new Byte[buffersize];
string tb1, tb2, tb3, tb4, tb5, nb1, nb2, nb3, nb4, nb5; private void button1_Click(object sender, EventArgs e)
{
sp.PortName = "COM4";//串口号
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.Parity = System.IO.Ports.Parity.None;//设置串口属性
sp.StopBits = System.IO.Ports.StopBits.None;
sp.ReadBufferSize = 1024;//接收缓冲区大小
sp.Encoding = Encoding.BigEndianUnicode;
sp.Open();
}
//字节数组转16进制字符串
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
} private void button2_Click(object sender, EventArgs e)
{
while (true)
{
sp.WriteLine(textBox3.Text);//串口写数据
sp.Read(buffer, 0, buffersize);
string shiliu, shi, tb1, tb2, tb3, tb4, tb5;
shiliu = byteToHexStr(buffer);//字节转换16进制字符串 textBox3.Text = shiliu;
tb1 = textBox3.Text.Substring(0, 2);//截取16进制字符串
//nb1 = ConvertShi(tb1);
tb2 = textBox3.Text.Substring(2, 2);
//nb2 = ConvertShi(tb2);
tb3 = textBox3.Text.Substring(4, 2);
//nb3 = ConvertShi(tb3);
tb4 = textBox3.Text.Substring(6, 2);
//nb4 = ConvertShi(tb4);
tb5 = textBox3.Text.Substring(8, 2);
//连接、输入以及提取数据库数据
string strConnect = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=wenshidu.mdb ";//创建数据库连接"; OleDbConnection aConnection = new OleDbConnection(strConnect);//表示到数据源的唯一一个连接
aConnection.Open(); //打开数据库连接 OleDbDataAdapter da = new OleDbDataAdapter(@"select * from table1", aConnection);//表示一组数据命令和一个数据库连接,它们用于填充 DataSet 和更新数据源 DataSet ds = new DataSet();//数据在内存中的缓存
da.Fill(ds);//在指定范围内刷新行 OleDbCommandBuilder cb = new OleDbCommandBuilder(da);//自动生成用于协调对 DataSet 的更改与关联数据库的单表命令
da.UpdateCommand = cb.GetUpdateCommand();//用于更新数据源中与 DataSet 中已修改的行相对应的记录
DataRow newData = ds.Tables[0].NewRow();//表示 DataTable 中的一行数据,创建与该表具有相同架构的新 DataRow,Tables[0]表示第一个表的数据
newData["湿度整数"] = tb1;
newData["湿度小数"] = tb2;
newData["温度整数"] = tb3;
newData["温度小数"] = tb4;
newData["校验位"] = tb5;
DateTime.Now.Date.ToString(); //获取当前时间
ds.Tables[0].Rows.Add(newData);//添加对象 da.Update(ds);//更新数据源 string[] znb1 = new string[ds.Tables[0].Rows.Count];
string[] znb2 = new string[ds.Tables[0].Rows.Count];
string[] znb3 = new string[ds.Tables[0].Rows.Count];
string[] znb4 = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
znb1[i] = ds.Tables[0].Rows[i]["湿度整数"].ToString();
nb1 = ConvertShi(znb1[i]);
znb2[i] = ds.Tables[0].Rows[i]["湿度小数"].ToString();
nb2 = ConvertShi(znb2[i]);
textBox1.Text = nb1 + "." + nb2 + "%";
znb3[i] = ds.Tables[0].Rows[i]["温度整数"].ToString();
nb3 = ConvertShi(znb3[i]);
znb4[i] = ds.Tables[0].Rows[i]["温度小数"].ToString();
nb4 = ConvertShi(znb4[i]);
textBox2.Text = nb3 + "." + nb4 + "摄氏度";
}
}
}
public String ConvertShi( string Str16)
{
int a;
string Str10;
a = Convert.ToInt32(Str16, 16);
Str10 = Convert.ToString(a);
return Str10; }
}
}数据库串口
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Data.OleDb;//用OLEDB连接数据库的命名空间namespace XSwenshidu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SerialPort sp = new SerialPort();
static int buffersize = 18;
byte[] buffer = new Byte[buffersize];
string tb1, tb2, tb3, tb4, tb5, nb1, nb2, nb3, nb4, nb5; private void button1_Click(object sender, EventArgs e)
{
sp.PortName = "COM4";//串口号
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.Parity = System.IO.Ports.Parity.None;//设置串口属性
sp.StopBits = System.IO.Ports.StopBits.None;
sp.ReadBufferSize = 1024;//接收缓冲区大小
sp.Encoding = Encoding.BigEndianUnicode;
sp.Open();
}
//字节数组转16进制字符串
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
} private void button2_Click(object sender, EventArgs e)
{
while (true)
{
sp.WriteLine(textBox3.Text);//串口写数据
sp.Read(buffer, 0, buffersize);
string shiliu, shi, tb1, tb2, tb3, tb4, tb5;
shiliu = byteToHexStr(buffer);//字节转换16进制字符串 textBox3.Text = shiliu;
tb1 = textBox3.Text.Substring(0, 2);//截取16进制字符串
//nb1 = ConvertShi(tb1);
tb2 = textBox3.Text.Substring(2, 2);
//nb2 = ConvertShi(tb2);
tb3 = textBox3.Text.Substring(4, 2);
//nb3 = ConvertShi(tb3);
tb4 = textBox3.Text.Substring(6, 2);
//nb4 = ConvertShi(tb4);
tb5 = textBox3.Text.Substring(8, 2);
//连接、输入以及提取数据库数据
string strConnect = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=wenshidu.mdb ";//创建数据库连接"; OleDbConnection aConnection = new OleDbConnection(strConnect);//表示到数据源的唯一一个连接
aConnection.Open(); //打开数据库连接 OleDbDataAdapter da = new OleDbDataAdapter(@"select * from table1", aConnection);//表示一组数据命令和一个数据库连接,它们用于填充 DataSet 和更新数据源 DataSet ds = new DataSet();//数据在内存中的缓存
da.Fill(ds);//在指定范围内刷新行 OleDbCommandBuilder cb = new OleDbCommandBuilder(da);//自动生成用于协调对 DataSet 的更改与关联数据库的单表命令
da.UpdateCommand = cb.GetUpdateCommand();//用于更新数据源中与 DataSet 中已修改的行相对应的记录
DataRow newData = ds.Tables[0].NewRow();//表示 DataTable 中的一行数据,创建与该表具有相同架构的新 DataRow,Tables[0]表示第一个表的数据
newData["湿度整数"] = tb1;
newData["湿度小数"] = tb2;
newData["温度整数"] = tb3;
newData["温度小数"] = tb4;
newData["校验位"] = tb5;
DateTime.Now.Date.ToString(); //获取当前时间
ds.Tables[0].Rows.Add(newData);//添加对象 da.Update(ds);//更新数据源 string[] znb1 = new string[ds.Tables[0].Rows.Count];
string[] znb2 = new string[ds.Tables[0].Rows.Count];
string[] znb3 = new string[ds.Tables[0].Rows.Count];
string[] znb4 = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
znb1[i] = ds.Tables[0].Rows[i]["湿度整数"].ToString();
nb1 = ConvertShi(znb1[i]);
znb2[i] = ds.Tables[0].Rows[i]["湿度小数"].ToString();
nb2 = ConvertShi(znb2[i]);
textBox1.Text = nb1 + "." + nb2 + "%";
znb3[i] = ds.Tables[0].Rows[i]["温度整数"].ToString();
nb3 = ConvertShi(znb3[i]);
znb4[i] = ds.Tables[0].Rows[i]["温度小数"].ToString();
nb4 = ConvertShi(znb4[i]);
textBox2.Text = nb3 + "." + nb4 + "摄氏度";
}
}
}
public String ConvertShi( string Str16)
{
int a;
string Str10;
a = Convert.ToInt32(Str16, 16);
Str10 = Convert.ToString(a);
return Str10; }
}
}数据库串口
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货