|
 |
|
ช่วยเขียนโค๊ด C# การทำงานของปุ่มเพื่อมาเปรียบเทียบกับค่าที่รับมาจากเครื่องหยอดเหรียญหน่อยคับ |
|
 |
|
|
 |
 |
|
ติดตรงไหนครับ ?
|
 |
 |
 |
 |
Date :
2013-05-02 06:53:37 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือ มันไม่ได้ทำงานในลูปของปุ่มอ่ะคับ
มันไม่รอเช็คเงื่อนไข ว่ารับค่าเข้ามาแล้วเท่าไหร่
พอกดปุ่ม มันโชว์ข้อความ "กรุณาหยอดเหรียญ 15 บาท" กับ MessageBox พร้อมกันเลยคับ
มันไม่ได้รอว่าให้ได้ค่าครบ15 ก่อนแล้วค่อยโซว์ MessageBox พอค่าครบ 15 แล้วมันก็ยังไม่โชว์ MessageBox คับ
พอหยอดเหรียญมันก็เพิ่มไปเรื่อยๆๆ ไม่หยุดที่ 15 คับ
ยังไงก็ขอความกรุณาช่วยหน่อย นะคับ
|
ประวัติการแก้ไข 2013-05-02 08:25:27
 |
 |
 |
 |
Date :
2013-05-02 08:23:29 |
By :
goingmary |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

Code (C#)
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;
using System.IO.Ports;
namespace TestC_Sharp
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string iData;
// สำหรับเก็บรูปแบบการทำงาน
private enum WorkTypeID { IsPerday = 0, IsPerWeek = 1, IsPerMonth = 3, IsUnknown = 4 };
private WorkTypeID _cWorkType;
// สำหรับเก็บจำนวนเงินที่หยอด ณ ปัจจุบัน
private int _cMoney;
private void Form2_Load(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
_cMoney = 0;
_cWorkType = WorkTypeID.IsUnknown;
this.txtMonitor.Text = "กรุณาเลือกรูปแบบ"; // แสดงที่จอให้เลือกรูปแบบ (กดปุ่ม)
}
}
private void Form1_close(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
serialPort1.Close();
}
public static byte[] ConvertStringToByteArray(string stringToConvert)
{
return (new UnicodeEncoding()).GetBytes(stringToConvert);
}
private void ReceiveData(object s, EventArgs e)
{
byte[] myBytes = ConvertStringToByteArray(iData);
string hexString = BitConverter.ToString(myBytes);
// Replace the - seperator for an whitespace
hexString = hexString.Replace("-", " ");
if (hexString == "3F 00 06 00 12 00 01 00 03 00 3F 00")
_cMoney += 1;
else if (hexString == "3F 00 06 00 12 00 06 00 03 00 3F 00")
_cMoney += 1;
else if (hexString == "3F 00 06 00 12 00 02 00 03 00 3F 00")
_cMoney += 2;
else if (hexString == "3F 00 06 00 12 00 05 00 03 00 3F 00")
_cMoney += 2;
else if (hexString == "3F 00 06 00 12 00 03 00 03 00 3F 00")
_cMoney += 5;
else if (hexString == "3F 00 06 00 12 00 04 00 03 00 3F 00")
_cMoney += 10;
switch (_cWorkType)
{
case WorkTypeID.IsPerday:
if (_cMoney >= 15)
{
this.txtMonitor.Text = "พิมพ์";
}
else
{
this.txtMonitor.Text = "หยอดแล้ว " + _cMoney.ToString() + " บาท";
}
break;
case WorkTypeID.IsPerWeek:
if (_cMoney >= 20)
{
this.txtMonitor.Text = "พิมพ์";
}
else
{
this.txtMonitor.Text = "หยอดแล้ว " + _cMoney.ToString() + " บาท";
}
break;
case WorkTypeID.IsPerMonth:
if (_cMoney >= 25)
{
this.txtMonitor.Text = "พิมพ์";
}
else
{
this.txtMonitor.Text = "หยอดแล้ว " + _cMoney.ToString() + " บาท";
}
break;
};
}
private void ReceiveData2(String hexString)
{
if (hexString == "3F 00 06 00 12 00 01 00 03 00 3F 00")
_cMoney += 1;
else if (hexString == "3F 00 06 00 12 00 06 00 03 00 3F 00")
_cMoney += 1;
else if (hexString == "3F 00 06 00 12 00 02 00 03 00 3F 00")
_cMoney += 2;
else if (hexString == "3F 00 06 00 12 00 05 00 03 00 3F 00")
_cMoney += 2;
else if (hexString == "3F 00 06 00 12 00 03 00 03 00 3F 00")
_cMoney += 5;
else if (hexString == "3F 00 06 00 12 00 04 00 03 00 3F 00")
_cMoney += 10;
switch (_cWorkType)
{
case WorkTypeID.IsPerday:
if (_cMoney >= 15)
{
this.txtMonitor.Text = "พิมพ์";
}
else
{
this.txtMonitor.Text = "หยอดแล้ว " + _cMoney.ToString() + " บาท";
}
break;
case WorkTypeID.IsPerWeek:
if (_cMoney >= 20)
{
this.txtMonitor.Text = "พิมพ์";
}
else
{
this.txtMonitor.Text = "หยอดแล้ว " + _cMoney.ToString() + " บาท";
}
break;
case WorkTypeID.IsPerMonth:
if (_cMoney >= 25)
{
this.txtMonitor.Text = "พิมพ์";
}
else
{
this.txtMonitor.Text = "หยอดแล้ว " + _cMoney.ToString() + " บาท";
}
break;
};
}
private void serRf_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (_cWorkType == WorkTypeID.IsUnknown)
{
MessageBox.Show("กรุณาเลือกรูปแบบการทำงาน", "คำอธิบาย", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// ตรงนี้ที่จริงเครื่องต่องคืนเหรียญด้วย ทำอย่างไร จัดการเอาเองนะครับ
}
else
{
iData = serialPort1.ReadExisting();
this.Invoke(new EventHandler(ReceiveData));
}
}
private void btnPerDay_Click(object sender, EventArgs e)
{
// หากมีการเปลี่ยนรูปแบบใหม่ แสดงว่าเงินต้องเป็น 0
_cMoney = 0;
_cWorkType = WorkTypeID.IsPerday;
this.txtMonitor.Text = "กรุณาหยอดเหรียญทั้งสิ้น 15 บาท";
}
private void btnPerWeek_Click(object sender, EventArgs e)
{
// หากมีการเปลี่ยนรูปแบบใหม่ แสดงว่าเงินต้องเป็น 0
_cMoney = 0;
_cWorkType = WorkTypeID.IsPerWeek;
this.txtMonitor.Text = "กรุณาหยอดเหรียญทั้งสิ้น 20 บาท";
}
private void btnPerMonth_Click(object sender, EventArgs e)
{
// หากมีการเปลี่ยนรูปแบบใหม่ แสดงว่าเงินต้องเป็น 0
_cMoney = 0;
_cWorkType = WorkTypeID.IsPerMonth;
this.txtMonitor.Text = "กรุณาหยอดเหรียญทั้งสิ้น 25 บาท";
}
// ถ้าคุณมีปุ่มพิมพ์ อย่าลืมถ้ากดพิมพ์แล้ว
// _cWorkType = WorkTypeID.IsUnknown;
// _cMoney = 0;
}
}
|
 |
 |
 |
 |
Date :
2013-05-02 10:04:12 |
By :
คนงานตัดอ้อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
20 บาทครับ 555
PM มาเหมือนเดิมครับ
|
 |
 |
 |
 |
Date :
2013-05-02 16:24:46 |
By :
คนงานตัดอ้อย |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|