PALMisLIFE 討論區

搜索
鹹魚爸魅力四射舞蹈教室
查看: 1910|回復: 5
打印 上一主題 下一主題

想問一下C#的問題

[複製鏈接]

9

主題

1

好友

23

積分

該用戶從未簽到

文章
40
跳轉到指定樓層
1#
發表於 2011-5-29 23:58 |只看該作者 |倒序瀏覽
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace ConsoleApplication10
{
    class Program
    {


      




        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            int data;

            //開啟rs232 Port (開啟COM1,鮑率為19200,同為檢查為元為沒有,位元率為8,停止位元數為1)
            SerialPort rs232 = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);

            
            //超過5秒鐘(5000毫秒)沒讀到東西,則丟出一個例外(TimeoutException)
            rs232.ReadTimeout = 5000;
            

            //開啟(建立)連線
            rs232.Open();

            while (true)
           {               
                try
                {
                    //由rs232讀入一個byte的資料
                    data = rs232.ReadByte();

                    //將讀到的東西顯示出來
                    Console.WriteLine(data.ToString());
                }
            
            }

            //關閉rs232
            rs232.Close();
            
        


        }


   
    }
}
    namespace mysql
    {
        class Program
        {
            static void Main1(string[] args)
            {
                Insert();
            }
            
//程式片段大概是這樣  
        public int Insert(string Ohm, string Ampere, string volt)
        {
            //設定連線資訊  
            string connStr = String.Format("server=localhost;user id=root; password=123; database=test");

            MySqlConnection conn = new MySqlConnection(connStr);
            //sql字串  
            string sqlStr = "INSERT INTO tbl_SRBack (Ohm,Ampere,volt) VALUES (?Ohm,?Ampere,?volt)";

            try
            {
                conn.Open();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        Console.WriteLine("Can't Connect to test.");
                        break;
                    case 1045:
                        Console.WriteLine("Account or Password wrong.");
                        break;
                }
                return ex.Number;
            }

            MySqlCommand mySqlCmd = new MySqlCommand(sqlStr, conn);
            //加入參數  
            MySqlParameter[] parameters = new MySqlParameter[3];

            parameters[0] = new MySqlParameter("?Ohm", MySqlDbType.VarChar, 99);
            parameters[0].Value = Ohm;

            parameters[1] = new MySqlParameter("?Ampere", MySqlDbType.VarChar, 99);
            parameters[1].Value = Ampere;

            parameters[2] = new MySqlParameter("?volt", MySqlDbType.VarChar, 99);
            parameters[2].Value = volt;

            mySqlCmd.Parameters.AddRange(parameters);

            try
            {
                mySqlCmd.ExecuteNonQuery();
                return 0;
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                return ex.Number;
            }

            }
        }
    }


想問一下我的程式碼是這樣   那我想要把我RS232收到的資料透過下面的程式傳到資料庫  但是我執行的時候有些錯誤

不知道是否有高人可以指點我>"<
分享淘帖0 分享分享0 收藏收藏0 頂0 踩0

181

主題

5

好友

2509

積分

超敗家的白爛長笛手

  • TA的每日心情
    郁悶
    2013-4-6 01:41
  • 簽到天數: 4 天

    連續簽到: 1 天

    [LV.2]偶爾看看I

    文章
    2201
    2#
    發表於 2011-5-30 09:38 |只看該作者
    程式裡有兩個 void Main(){},這當然不成。
    先一步一步來,先暫時取下 namespace mysql{} 這個 block,比較好確認 RS232 能不能正確讀取。
    int data; 改成 string data = string.Empty;
    data = rs232.ReadByte(); 改成 data = rs232.ReadLine();
    這是把 RS232 讀進來的資料流轉成字串。而這字串必須經過適當解析之後,才能新增到資料庫裡。

    點評

    kc100639  可以讀取只是讀取不是讀取到正確的是字我打一會變成 49 10  發表於 2011-5-30 10:42
    kc100639  可以讀取 只是讀取不是讀取到正確的是字 我打一會變成 49  發表於 2011-5-30 10:42
    回復

    使用道具 舉報

    54

    主題

    2

    好友

    977

    積分

  • TA的每日心情
    慵懶
    2011-4-15 01:33
  • 簽到天數: 2 天

    連續簽到: 1 天

    [LV.1]初來乍到

    文章
    1023
    3#
    發表於 2011-5-30 10:43 |只看該作者
    要不要貼出錯誤訊息的內容呢?

    我看你的程式,應該compiler都不會過阿~
    例如:
    static void Main1(string[] args)
    {
        Insert();  <--沒有這個Function
    }

    點評

    kc100639  我目前程式是改成這樣~~我收的值已經是正確的了 但是在連接MYSQL這方面 還是連不過去 正在努力尋找問題...  發表於 2011-6-6 17:41
    zgenius右手定則:當你逆著時間回憶過去,心情是向上的;當你順著時間思考未來,心情是向下的。
    回復

    使用道具 舉報

    68

    主題

    1

    好友

    416

    積分

    該用戶從未簽到

    文章
    410
    4#
    發表於 2011-5-30 14:19 |只看該作者
    這是因為你用readbyte,會把原本讀取的1 (ASCII值為 49),然後你又做了ToString(),當然就是49
    可以讀取 只是讀取不是讀取到正確的是字 我打一會變成 49

    點評

    kc100639  恩恩!!瞭解 謝謝你的解答^^ 有你的解答幫了我不少忙  發表於 2011-6-6 17:42
    實力,決定排名的順序...
    回復

    使用道具 舉報

    9

    主題

    1

    好友

    23

    積分

    該用戶從未簽到

    文章
    40
    5#
    發表於 2011-5-30 18:47 |只看該作者
    謝謝各位~我已經把收到的值跟傳出的值更改為相同~把問題解決了
    回復

    使用道具 舉報

    9

    主題

    1

    好友

    23

    積分

    該用戶從未簽到

    文章
    40
    6#
    發表於 2011-6-6 17:40 |只看該作者
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO.Ports;
    using MySql.Data.MySqlClient;

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                string data = string.Empty;

                //開啟rs232 Port (開啟COM1,鮑率為19200,同為檢查為元為沒有,位元率為8,停止位元數為1)
                SerialPort rs232 = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);

                
                //超過5秒鐘(5000毫秒)沒讀到東西,則丟出一個例外(TimeoutException)
                rs232.ReadTimeout = 5000;
                

                //開啟(建立)連線
                rs232.Open();

                while (true)
                {               
                    try
                    {
                        //由rs232讀入一個byte的資料
                        data = rs232.ReadLine();


                        Console.WriteLine(data.ToString());
                        string[] parameters = data.Split("-");  //假設你讀出來的資料是用"-"做分隔
                        Insert(parameters[0], parameters[1], parameters[2]); //這邊要帶入你前面所設定的參數

                        //將讀到的東西顯示出來
                        Console.WriteLine(data.ToString());
                    }
                

                    //如果讀到'q'則停止
                  
                }

                //關閉rs232
                rs232.Close();
                
            }


            public int Insert(string Ohm, string Ampere, string volt)
            {
                //設定連線資訊  
                string connStr = String.Format("server=localhost;user id=root; password=123; database=test");

                MySqlConnection conn = new MySqlConnection(connStr);
                //sql字串  
                string sqlStr = "INSERT INTO tbl_test_copy (Ohm,Ampere,volt) VALUES (?Ohm,?Ampere,?volt)";

                try
                {
                    conn.Open();
                }
                catch (MySql.Data.MySqlClient.MySqlException ex)
                {
                    switch (ex.Number)
                    {
                        case 0:
                            Console.WriteLine("Can't Connect to test.");
                            break;
                        case 1045:
                            Console.WriteLine("Account or Password wrong.");
                            break;
                    }
                    return ex.Number;
                }

                MySqlCommand mySqlCmd = new MySqlCommand(sqlStr, conn);
                //加入參數  
                MySqlParameter[] parameters = new MySqlParameter[3];

                parameters[0] = new MySqlParameter("?Ohm", MySqlDbType.VarChar, 50);
                parameters[0].Value = Ohm;

                parameters[1] = new MySqlParameter("?Ampere", MySqlDbType.VarChar, 50);
                parameters[1].Value = Ampere;

                parameters[2] = new MySqlParameter("?volt", MySqlDbType.VarChar, 50);
                parameters[2].Value = volt;

                mySqlCmd.Parameters.AddRange(parameters);

                try
                {
                    mySqlCmd.ExecuteNonQuery();

                    return 0;
                }
                catch (MySql.Data.MySqlClient.MySqlException ex)
                {
                    return ex.Number;
                }
            }
       





        }
    }



    回復

    使用道具 舉報

    您需要登錄後才可以回帖 登錄 | 免費註冊

    與站長聯繫| PALMisLIFE 掌上生活      下載:更快、更棒、更好玩

    GMT+8, 2024-4-24 15:39 , Processed in 0.046200 second(s), 31 queries , Gzip On.

    Powered by Discuz!

    © 2001-2012 Comsenz Inc. style by eisdl

    回頂部