- 註冊時間
- 2011-5-27
- 線上時間
- 20 小時
- 閱讀權限
- 10
- 積分
- 23
- 主題
- 9
- 精華
- 0
- 文章
- 40
該用戶從未簽到 - 文章
- 40
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.IO.Ports;
namespace ConsoleApplication21
{
class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM1");
mySerialPort.BaudRate = 19200;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);
mySerialPort.Open();
Console.WriteLine("Press any key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceviedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
//class aaa
//{
public static void insert(object sender,
SerialDataReceivedEventArgs e)
{
//設定連線資訊
string connStr = String.Format("server=localhost;user id=root; password=123; database=test");
MySqlConnection conn = new MySqlConnection(connStr);
//sql字串
string sqlStr = "INSERT INTO test (date,time,Ohm,Ampere,volt) VALUES (now(), now('hh:mm:00'),?Ohm,?Ampere,?volt)";
conn.Open();
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
MySqlCommand mySqlCmd = new MySqlCommand(sqlStr, conn);
//加入參數
MySqlParameter[] parameters = new MySqlParameter[3];
parameters[0] = new MySqlParameter("?Ohm", MySqlDbType.VarChar, 50);
parameters[0].Value = indata;
parameters[1] = new MySqlParameter("?Ampere", MySqlDbType.VarChar, 50);
parameters[1].Value = 555;
parameters[2] = new MySqlParameter("?volt", MySqlDbType.VarChar, 50);
parameters[2].Value = 123;
mySqlCmd.Parameters.AddRange(parameters);
mySqlCmd.ExecuteNonQuery();
conn.Close();
}
}
}
想問一下我的indata為什麼收不到值我有呼叫Console.Write(indata);所收到的值....是我程式有錯嗎....
parameters[0].Value = indata;這行是這樣呼叫嗎....
|
|