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;
// user include----------------------------
using System.IO.Ports;
namespace rs232v2
{
    public partial class Form1 : Form
    {
        public SerialPort serialPort = new SerialPort();
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // button 設定
            this.button1.Text = "Open COM";
            this.button2.Text = "Send";
            this.button2.Enabled = false;
            // 事件初始化
            this.button1.Click += new EventHandler(Rs232PortDoor);
            this.button2.Click += new EventHandler(SendPhone);
            // rs232 door key
            rs232Key = true;
        }
        bool rs232Key;
        // button1 事件處理
        // 初始PORT & 打開 button2
        private void Rs232PortDoor(object sender, EventArgs e)
        {
            if (rs232Key)
            {
                this.button1.Text = "Close Port";
                // 打開 button2
                this.button2.Enabled = true;
                // 設定使用的 PORT
                this.serialPort.PortName = "COM4";
                // 檢查 PORT 是否關閉
                if (!serialPort.IsOpen)
                    this.serialPort.Close();
                // 初始化 PORT
                this.serialPort.BaudRate = 9600;            // baud rate = 9600
                this.serialPort.Parity = Parity.None;       // Parity = none
                this.serialPort.StopBits = StopBits.One;    // stop bits = one
                this.serialPort.DataBits = 8;               // data bits = 8
                // 設定 PORT 接收事件
                serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
                // 打開 PORT
                serialPort.Open();
                // 清空 serial port 的緩存
                serialPort.DiscardInBuffer();       // RX
                serialPort.DiscardOutBuffer();      // TX
                rs232Key = false;
            }
            else
            {
                this.button1.Text = "Open Port";
                // 清空 serial port 的緩存
                serialPort.DiscardInBuffer();       // RX
                serialPort.DiscardOutBuffer();      // TX
                // 關閉 PORT
                this.serialPort.Close();
                // 關閉 button2
                this.button2.Enabled = false;
                rs232Key = true;
            }
        }
        // button2 事件處理
        // 傳送三組電話過去
        private void SendPhone(object sender, EventArgs e)
        {
            char[] textBuf;
            textBuf = this.textBox1.Text.ToCharArray();
            rs232Output(textBuf);
            textBuf = this.textBox2.Text.ToCharArray();
            rs232Output(textBuf);
            textBuf = this.textBox3.Text.ToCharArray();
            rs232Output(textBuf);
        }
        // 經由RS232 字串傳送
        private void rs232Output(char[] phoneNum)
        {
            for (int i = 0; i < phoneNum.Length; i++)
            {
                serialPort.Write(phoneNum, i, 1);
                //serialPort.Write("A");
            }
            // 傳送 Enter 的 ascii code
            byte[] commEnter = new byte[] {0x0D, 0x0A};
            for (int i = 0; i < 2; i++)
            {
                serialPort.Write(commEnter, i, 1);
            }
            Console.WriteLine("output Phone Number" + phoneNum.ToString());
        }
        // PORT 接收事件
        void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte RB = Byte.Parse(serialPort.ReadByte().ToString());
            //SetText(RB.ToString());
            SetText(textBox4.Text + RB.ToString() + " ");
        }
        // TextBox2 跨執行續執行
        delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            if (this.textBox2.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox4.Text = text;
            }
        }
    }
}

ffyy99 發表在 痞客邦 留言(3) 人氣()

出處: http://winnercow.pixnet.net/blog/post/23920505
 
C#的String.Format舉例
stringstr1 =string.Format("{0:N1}",56789);
//result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789);
//result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789);
//result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789);
//result: 56789.0
stringstr9 =string.Format("{0:F2}",56789);
//result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##");
//result: 567.89
stringstr12 =(56789 / 100).ToString("#.##");
//result: 567
C 或 c
貨幣
Console.Write("{0:C}", 2.5); //$2.50
Console.Write("{0:C}",
-2.5); //($2.50)
D 或 d
十進位數字
Console.Write("{0:D5}", 25);
//00025
E 或 e
科學型
Console.Write("{0:E}", 250000);
//2.500000E+005
F 或 f
固定點
Console.Write("{0:F2}", 25);
//25.00
Console.Write("{0:F0}", 25); //25
G 或 g
常規
Console.Write("{0:G}",
2.5); //2.5
N 或 n
數字
Console.Write("{0:N}", 2500000);
//2,500,000.00
X 或 x
十六進位
Console.Write("{0:X}", 250);
/******************************************************************************/
ASP.NET
設置資料格式與String.Format使用總結(引)
{0:d} YY-MM-DD
{0:p} 百分比00.00%
{0:N2}
12.68
{0:N0} 13
{0:c2} $12.68
{0:d} 3/23/2003
{0:T}
12:00:00 AM
{0:男;;女}
DataGrid-資料格式設置運算式
資料格式設置運算

.NET Framework
格式設置運算式,它在資料顯示在列中之前先應用於資料。此運算式由可選靜態文本和用以下格式表示的格式說明符組成:
{0:format
specifier}
零 是參數索引,它指示列中要格式化的資料元素;因此,通常用零來指示第一個(且唯一的)元素。format
specifier 前面有一個冒號
(:),它由一個或多個字母組成,指示如何格式化資料。可以使用的格式說明符取決於要格式化的資料類型:日期、數位或其他類型。下表顯示了不同資料類型的
格式設置運算式的示例。有關格式設置運算式的更多資訊,請參見格式化類型。
格式設置運算式
應用於此資料類型

說明
Price: {0:C}
numeric/decimal

示“Price:”,後跟以貨幣格式表示的數字。貨幣格式取決於通過 Page 指令或 Web.config 檔中的區域性屬性指定的區域性設置。
{0:D4}
integer(不
能和小數一起使用。)
在由零填充的四個字元寬的欄位中顯示整數。
{0:N2}%
numeric

示精確到小數點後兩位元的數字,後跟“%”。
{0:000.0}
numeric/decimal

捨五入到小數點後一位元的數字。不到三位元的數字用零填充。
{0:D}
date/datetime

日期格式(“Thursday, August 06, 1996”)。日期格式取決於頁或 Web.config 檔的區域性設置。
{0:d}
date/datetime

日期格式(“12/31/99”)。
{0:yy-MM-dd}
date/datetime

數字的年-月-日表示的日期(96-08-06)。
唯讀
當此列處於編輯模式時,該列中的資料是否顯示在可編輯的控制
項中。
2006-02-22 | asp.net資料格式的Format-- DataFormatString

們在呈現資料的時候,不要將未經修飾過的資料呈現給使用者。例如金額一萬元,如果我們直接顯示「10000」,可能會導致使用者看成一千或十萬,造成使用
者閱讀資料上的困擾。若我們將一萬元潤飾後輸出為「NT$10,000」,不但讓使比較好閱讀,也會讓使用者減少犯錯的機會。
下列畫面為潤飾過
的結果:
上述資料除了將DataGrid Web
控制項以顏色來區隔記錄外,最主要將日期、單價以及小計這三個計欄位的資料修飾的更容易閱讀。要修飾欄位的輸出,只要設定欄位的
DataFormatString 屬性即可;其使用語法如下:
DataFormatString="{0:格式字串}"

們知道在DataFormatString 中的 {0}
表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;另外在指定的格式符號後可以指定小數所要顯示的位元數。例如原來的資料為
「12.34」,若格式設定為 {0:N1},則輸出為「12.3」。其常用的數值格式如下表所示:
格式字串 資料 結果
"{0:C}"
12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}"
12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789
1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}"
12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789
12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789
12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}"
12345.6789 Total: $12345.68
其常用的日期格式如下表所示:
格式 說明 輸出格式
d
精簡日期格式 MM/dd/yyyy
D 詳細日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date +
short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date +
long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short
time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy
HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t
精簡時間格式 HH:mm
T 詳細時間格式 HH:mm:ss
string.format格式結果
String.Format
(C)
Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . .
-123
(E) Scientific: . . . . . . . -1.234500E+002
(F)
Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . .
-123
(N) Number: . . . . . . . . . -123.00
(P) Percent:. .
. . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X)
Hexadecimal:. . . . . . . FFFFFF85
(d) Short date: . . . . . . .
6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t)
Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . .
8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004
8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004
8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G)
General date/long time: . 6/26/2004 8:11:04 PM
(M) Month:. . . .
. . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun
2004 20:11:04 GMT
(s) Sortable: . . . . . . . .
2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26
20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June
27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
(G)
General:. . . . . . . . . Green
(F) Flags:. . . . . . . . . .
Green (flags or integer)
(D) Decimal number: . . . . . 3
(X)
Hexadecimal:. . . . . . . 00000003
說明:
String.Format
將指定
的 String 中的每個格式項替換為相應物件的值的文本等效項。
例子:
int iVisit = 100;
string
szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了
{1} 次.", szName, iVisit));

ffyy99 發表在 痞客邦 留言(0) 人氣()

1
Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。