2012年11月3日 星期六

How to use aboutBox in C sharp

1.Add aboutBox1
2.set parameter
    this.Text = String.Format("About {0} {0}", AssemblyTitle);
    this.labelProductName.Text = AssemblyProduct;
    this.labelVersion.Text = String.Format("Version {0} {0}", AssemblyVersion);
    this.labelCopyright.Text = AssemblyCopyright;
    this.labelCompanyName.Text = AssemblyCompany;
    this.textBoxDescription.Text = AssemblyDescription;

3. active it
    AboutBox1 abc=new AboutBox1() ;
    abc.ShowDialog();

2012年10月29日 星期一

Recieve Data from serial port by C sharp


        void AddRecieve(object s, EventArgs e)
        {
            textBox2.AppendText("進入!Received" + "\n");
            int count;
            int i = 0;

                 do
                {
                     count = serialPort1.BytesToRead;
                     System.Threading.Thread.Sleep(200);
                     i += 1;
                 }

                 while (i < 19);
       
            byte[] rxBuff = new byte[count];
            int[] wordss = new int[18];
            serialPort1.Read(rxBuff, 0, rxBuff.Length);

            string str = BitConverter.ToString(rxBuff);
            textBox2.AppendText(str + " i=" + i + "\n");
            System.IO.File.AppendAllText(@"WriteLines.txt", str + " i=" +i + "\n");
            string[] words = str.Split('-');
            i = 0;
            foreach (string word in words)
            {
                textBox2.AppendText(word + " ");
                wordss[i] = Convert.ToInt16(word, 16);
                i++;
            }

            // int decValue;
            textBox1.Text = wordss[1].ToString();           
                textBox3.Text = (Convert.ToSingle(wordss[3]) + (Convert.ToSingle(wordss[4])) / 10).ToString();
                textBox4.Text = (Convert.ToSingle(wordss[5]) + (Convert.ToSingle(wordss[6])) / 10).ToString();

                double xValue = (Convert.ToDouble(wordss[7]) + (Convert.ToDouble(wordss[8])) / 10.0);
                if (xValue >= 128.0)
                {
                    xValue = 128.0 - xValue;
                }
                double yValue = (Convert.ToDouble(wordss[9]) + (Convert.ToDouble(wordss[10])) / 10.0);
                if (yValue >= 128.0)
                {
                    yValue = 128.0 - yValue;
                }
                double zValue = (Convert.ToDouble(wordss[11]) + (Convert.ToDouble(wordss[12])) / 10.0);
                if (zValue >= 128.0)
                {
                    zValue = 128.0 - zValue;
                }
                textBox5.Text = String.Format("{0:0.0}", xValue);
                textBox6.Text = String.Format("{0:0.0}", yValue);
                textBox7.Text = String.Format("{0:0.0}", zValue);
                //wordss[11].ToString() + wordss[12].ToString();
                string NowTime = String.Format("{0:yyyy/M/d HH:mm:ss}", DateTime.Now);
                writeLog(NowTime, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text);
            textBox2.AppendText("\n");
        }

C sharp read data from serial port

  serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(SerialPort1_DataReceived);
  serialPort1.ReceivedBytesThreshold = 1;

   int i = 0;
                 do
                {
                     count = serialPort1.BytesToRead;       // binary data
                     System.Threading.Thread.Sleep(200);
                     i += 1;
                 }

                 while (i < 13);

VB.NET Sleep Lib

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Int32)

Sleep(10000) ' 10秒鐘, 1秒鐘=1000

什麼是零息債券?


零息債券 CATS。Streaker。Strip。Zero coupon bond。
一種債務憑證,指債券以折價方式發行,不定期付息,以低於面值價格出售的債券。投資者持有此類債券主要在於獲得資本增值,即是贖回面值高於購入折扣價的差額;對發行機構而言,可免除需要定期付息所引致的資金周轉問題。
2012/10/29  的大盤趨勢圖







寫入資料成 CSV 檔使用 VB.NET

Imports System.IO

Imports System.Text

Public Class Form1
Dim FilePath As String = "C:\csv.CSV"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Using fs As IO.FileStream = New IO.FileStream(FilePath, FileMode.Append)
Using sw As StreamWriter = New StreamWriter(fs, Encoding.Default)
Dim str As String = TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & "," _
                              & TextBox4.Text & "," & TextBox5.Text
sw.Write(str) '第一行
sw.WriteLine(str) '插入一整行
sw.Flush() '執行寫入動作
End Using
End Using
Catch ex As Exception
End Try

End Sub
End Class

如何讀取與寫入 ini 檔用 VB.NET

Imports System.Text


Imports System.Runtime.InteropServices



Public Class Form1



'2.宣告方法:

Public Declare Function GetPrivateProfileString Lib "kernel32" _

Alias "GetPrivateProfileStringA" ( _

ByVal lpApplicationName As String, _

ByVal lpKeyName As String, _

ByVal lpDefault As String, _

ByVal lpReturnedString As StringBuilder, _

ByVal nSize As UInt32, _

ByVal lpFileName As String) As UInt32

Public Declare Function WritePrivateProfileString Lib "kernel32" _

Alias "WritePrivateProfileStringA" ( _

ByVal lpApplicationName As String, _

ByVal lpKeyName As String, _

ByVal lpReturnedString As StringBuilder, _

ByVal lpFileName As String) As UInt32





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'3.使用方式:

'讀出ini檔,區段裡的值:

Dim lReturnLen1 As UInt64

Dim lReturnLen2 As UInt64

Dim lReturnLen3 As UInt64



Dim sSection1 As String = "City" '區段

Dim sKey1 As String = "City_1" '區段中的值

Dim sKey2 As String = "City_2"

Dim sKey3 As String = "City_3"



Dim sSection2 As String = "food" '區段

Dim sKey4 As String = "food_1" '區段中的值

Dim sKey5 As String = "food_2"

Dim sKey6 As String = "food_3"



Dim sSection3 As String = "goods" '區段

Dim sKey7 As String = "goods_1" '區段中的值

Dim sKey8 As String = "goods_2"

Dim sKey9 As String = "goods_3"



Dim sKeyValue As New StringBuilder(1024)

Dim nSize As UInt32 = Convert.ToUInt32(1024)

Dim sinifilename As String = Application.StartupPath & "\\test.ini"

lReturnLen1 = GetPrivateProfileString(sSection1, sKey1, "", sKeyValue, nSize, sinifilename)

Label1.Text = sKeyValue.ToString



lReturnLen1 = GetPrivateProfileString(sSection1, sKey2, "", sKeyValue, nSize, sinifilename)

Label2.Text = sKeyValue.ToString

lReturnLen1 = GetPrivateProfileString(sSection1, sKey3, "", sKeyValue, nSize, sinifilename)

Label3.Text = sKeyValue.ToString



lReturnLen2 = GetPrivateProfileString(sSection2, sKey4, "", sKeyValue, nSize, sinifilename)

Label4.Text = sKeyValue.ToString

lReturnLen2 = GetPrivateProfileString(sSection2, sKey5, "", sKeyValue, nSize, sinifilename)

Label5.Text = sKeyValue.ToString

lReturnLen2 = GetPrivateProfileString(sSection2, sKey6, "", sKeyValue, nSize, sinifilename)

Label6.Text = sKeyValue.ToString



lReturnLen3 = GetPrivateProfileString(sSection3, sKey7, "", sKeyValue, nSize, sinifilename)

Label7.Text = sKeyValue.ToString

lReturnLen3 = GetPrivateProfileString(sSection3, sKey8, "", sKeyValue, nSize, sinifilename)

Label8.Text = sKeyValue.ToString

lReturnLen3 = GetPrivateProfileString(sSection3, sKey9, "", sKeyValue, nSize, sinifilename)

Label9.Text = sKeyValue.ToString



End Sub

End Class

VB.NET Log 如何的寫入

而app.config中有定義一個FileLog,內定會把log產生到檔案中,如果我們unmark EventLog的部份,那麼它


還會產生log於Windows的EventLog之中,所以非好用。而 app.config中DefaultSwitch 的value內定是Information

更改它之後(像我改它成Error),可以讓程式變成只記錄Error與Critial。

With My.Application.Log.DefaultFileLogWriter

.BaseFileName = "cwwT" '設log的檔名開頭

.CustomLocation = "c:\" '設log存放之dir

.AutoFlush = True

.IncludeHostName = True

.LogFileCreationSchedule = Logging.LogFileCreationScheduleOption.Daily '設定一天一個檔

MsgBox(My.Application.Log.DefaultFileLogWriter.FullLogFileName) 'c:\cwwT-2009-09-25.log

My.Application.Log.WriteEntry("TestInfor", TraceEventType.Information, 10) '不會記錄

My.Application.Log.WriteEntry("TestError", TraceEventType.Error, 20)

End With

以下是app.config

type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"

initializeData="FileLogWriter"/>

此表會顯示「記錄檔」在特定的 DefaultSwitch 設定下,將訊息寫入接聽程式所需的嚴重性層級


成員名稱 說明

Off 不允許任何事件通過。

Critical 只允許 Critical 事件通過。

Error 允許 Critical 和 Error 事件通過。

Warning 允許 Critical、Error 和 Warning 事件通過。

Information 允許 Critical、Error、Warning 和 Information 事件通過。

Verbose 允許 Critical、Error、Warning、Information 和 Verbose 事件通過。

ActivityTracing 允許 Stop、Start、Suspend、Transfer 和 Resume 事件通過。

All 允許所有事件通過。