2010年4月5日 星期一

[c#] 事件1Car 應用介紹-[Event 1Car]

事件 應用介紹-[Event]

//Car 事件類別
namespace even_car
{
//事件委派
delegate void DangerEvent(int Speed);

class Car
{
private int m_speed;

//宣告事件
public event DangerEvent Danger;

public int Speed
{
get
{
return m_speed;
}
set
{
if (value >200)
if (Danger != null)
{
Danger(value);
}

m_speed = value;
}
}
}
}

//測試程式
namespace even_car
{
class Program
{
static void Main(string[] args)
{
test事件();
Console.ReadLine();
}

static void test事件()
{
Car Benz = new Car();
//指定Danger事件由ToolFast方法來處理
Benz.Danger += new DangerEvent(ToolFast);
//車子速度屬性指定
Benz.Speed = 300;
}


static void ToolFast(int vSpeed)
{
Console.WriteLine("目前的速度{0},高過200,請減速!!", vSpeed);
}
}
}

2010年3月12日 星期五

SIEMENS-PC-Base MicroBox-[ Motion VS Vision 影片 ]

SIEMENS-PC-Base MicroBox-[ Motion VS Vision 應用例 ]
應用介紹:

系統利用西門子高速,極高運算力的PC-Base 系統,精簡配線通訊總線IO系統,搭配使用通訊總線運動控制器,與影像辨識1000fps 高速分色辨識自由落體,並使用同步非同步,winAC-RTX windows 即時核心技術,做完美的結合.

點選下方play鍵播放收看影片,並且注意分色轉盤的快慢速度變化.




(其中軟硬體精隨另行備載)

2010年1月26日 星期二

[c#] 如何處理例外錯誤狀況 應用介紹-[try-catch-finally]

如何處理例外錯誤狀況 應用介紹-[try-catch-finally]

1.) NG程式
//錯誤程式,程式直接當掉(原因被除數不得維0)

private void button1_Click(object sender, EventArgs e)
{
int a = 0;
int b =100/a;
Console.WriteLine("b is {0}", b);

Console.WriteLine("End of job ");
}

2.) 攔截NG程式

//try-catch-finally語法(程式不會當掉,但提報錯誤)
private void button2_Click(object sender, EventArgs e)
{
int a = 0;
try
{
int b = 100 / a;
Console.WriteLine("b is {0}", b);
}
//錯誤攔截訊息
catch (Exception err1)
{
Console.WriteLine("Divided by zero ");
Console.WriteLine("Divided by zero {0}",err1 );
MessageBox.Show(err1.ToString ());
}
//最後一定會執行
finally
{
Console.WriteLine("End of job");
}

3.) 自行判斷NG錯誤跳轉攔截錯誤訊息(throw 使用)

//自行判斷錯誤跳轉攔截錯誤訊息(程式不會當掉,但提報錯誤)
private void button3_Click(object sender, EventArgs e)
{
int a = 100, b = 0;
try
{
if (b == 0)
{
//跳轉呼叫丟出錯誤
throw new ArithmeticException("Div by zero");
}
else
{
int c = a / b;
Console.WriteLine("Resoult is " + c);
}
Console.WriteLine("After divide2");
}
//
catch (ArithmeticException error)
{
Console.WriteLine("Caught!{0}",error );
}


===========================================
自訂錯誤類別-待續

2010年1月19日 星期二

[c#] String字串處理Split 應用介紹-[Split]

String字串處理Split 應用介紹-[Split]

//split
private void button_Click(object sender, EventArgs e)
{

char[] del = new char[] { ',','/' }; //多符號分離
//char del = ','; //單符號分離

string line = "datalog_mo1_0,2010/1/19下午 01:47:52,691,1,40197574905.7755";

string[] split = line.Split(del);

foreach (string s in split)
{
listBox1.Items.Add(s); //送到目標 listbox檢視
}

}

2010年1月2日 星期六

[SIEMENS SCL FC ] 由函式FC回傳演算計算 應用介紹-[SCL-FC ][IEC 61131-3]


由函式FC回傳演算計算 應用介紹-[SCL-FC][IEC 61131-3]


FUNCTION SQUARE : INT //帶回傳值


(*********************************************************

This function returns as its function value the square of the

input value or if there is overflow, the maximum value that

can be represented as an integer.

***********************************************************)


//know_how_protect //程式保護


//變數宣告in類型
VAR_INPUT

value : INT;

END_VAR


//程式開始
BEGIN


IF value <= 181 THEN

SQUARE := value * value; //計算含式

ELSE

SQUARE := 32_767; // overflow, 設定最大值並回傳當成錯誤碼

END_IF;


END_FUNCTION

2009年12月27日 星期日

[c#] 單一執行緒與委派 應用介紹-[Thread and Delegate]

單一執行緒與委派 應用介紹-[Thread and Delegate]

//產生紅色球球
bool flg1, flg2,flg3;

private void button1_Click(object sender, EventArgs e)
{

flg1 = true;
Thread th1 = new Thread(readtst);
th1.Start();

}

//執行緒目標
public void readtst()
{
while (flg1 == true)
{
textBox1.BeginInvoke(new creatball(rdball ));
Thread.Sleep(10);
}
}

//委派目標 (執行緒委派目標值傳回到form的物件)
private delegate void creatball( );
private void tst()
{
textBox1.AppendText("read ball" +DateTime .Now .ToLongTimeString ()+ "\n");
}

//委派目標 - 產生紅色球球
public void rdball()
{
Ball aBall;
aBall.pt = new Point(rd.Next(20, this.ClientSize.Width - 20), rd.Next(40, this.ClientSize.Height - 20));
aBall.color = Color.Red;

lock (this)
{
ballList.Add(aBall);
textBox1.AppendText("Read"+DateTime .Now .ToLongTimeString ()+ "\n");
}

this.Invalidate();
}

[c#] 由函式反向傳遞直給飲用者 應用介紹-[public void mystr(out string x)]

由函式反向傳遞值給引用者 應用介紹-[public void mystr(out string x)]

//函式引用者取值
private void button1_Click(object sender, EventArgs e)
{
string x;
mystr(out x);
textBox1.Text = x;
}

//反向送出函式
public void mystr(out string x)
{
x = "read";
Console.WriteLine(x);
}