using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Xml.Serialization; using System.Windows.Threading; using System.Diagnostics; namespace SafeBagDE70 { /// /// Interaction logic for MainWindow.xaml /// /// //public class CountSession //{ // public int[] QuantityDenomi { get; set; } // public int[] SumDenomi { get; set; } //} public partial class MainWindow : HandyControl.Controls.Window { SerialPort serialPort = new SerialPort(); private int[] denominations = { 500000, 200000, 100000, 50000, 20000, 10000, 5000, 2000, 1000, 500 }; List sessions = new List(); public MainWindow() { InitializeComponent(); serialPort.PortName = "COM3"; serialPort.BaudRate = 9600; serialPort.Parity = Parity.Even; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; //Subscrible to the DataReveied event serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived); //serialPort.Open(); } public long maxQuantitySheet = 10000; public int totalAllDenominations = 0; public int flag = 0; public string hexReceivedString = ""; public void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { int bytesToRead = serialPort.BytesToRead; byte[] receivedBytes = new byte[bytesToRead]; serialPort.Read(receivedBytes, 0, bytesToRead); //Convert to hex string string newHexString = BitConverter.ToString(receivedBytes).Replace("-", " "); hexReceivedString = newHexString; } public async void btnCountStart_Click(object sender, RoutedEventArgs e) { sessions.Add(new CountSession()); string deSenseExpectedString0 = "10 02 30 38 34 60 40 07 41 00 01 00 00 00 00 00 00 10 10 00 00 00 00 00 F0 12"; string deSenseExpectedString1 = "10 02 30 38 34 60 40 07 41 00 01 00 00 00 00 00 00 10 10 00 00 01 00 01 F0 12"; string deSenseExpectedString2 = "10 02 30 38 34 60 40 07 41 00 01 00 00 00 00 00 00 10 10 00 00 02 00 02 F0 12"; string deSenseExpectedString3 = "10 02 30 38 34 60 40 07 41 00 01 00 00 00 00 00 00 10 10 00 00 03 00 03 F0 12"; string deSenseExpectedString4 = "10 02 30 38 34 60 40 07 41 00 01 00 00 00 00 00 00 10 10 00 00 04 00 04 F0 12"; string deSenseExpectedString5 = "10 02 30 38 34 60 40 07 41 00 01 00 00 00 00 00 00 10 10 00 00 05 00 05 F0 12"; var countStart = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x31, 0x73, 0x10, 0x03, 0x41 }; serialPort.Write(countStart, 0, countStart.Length); await Task.Delay(2500); //int[] quantityValues = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //int[] sessions[sessions.Count - 1].SumDenomi = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; var deSense = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x31, 0x60, 0x10, 0x03, 0x52 }; serialPort.Write(deSense, 0, deSense.Length); await Task.Delay(300); if (hexReceivedString.Contains(deSenseExpectedString0) || hexReceivedString.Contains(deSenseExpectedString1) || hexReceivedString.Contains(deSenseExpectedString2) || hexReceivedString.Contains(deSenseExpectedString3) || hexReceivedString.Contains(deSenseExpectedString4) || hexReceivedString.Contains(deSenseExpectedString5)) { //Hexa string data received process string[] hexValues = hexReceivedString.Split(' '); int[] values = new int[hexValues.Length]; for (int i = 0; i < hexValues.Length; i++) { values[i] = Convert.ToInt32(hexValues[i], 16); } //Take amount of each denomination sessions[0].QuantityDenomi = values.Skip(26).Take(10).ToArray(); //Total money of each denomination. for (int i = 0; i < sessions[0].QuantityDenomi.Length; i++) { sessions[sessions.Count - 1].SumDenomi[i] = sessions[sessions.Count - 1].QuantityDenomi[i] * denominations[i]; } //Total amount of each denomination // Total money of all denominations totalAllDenominations = sessions[sessions.Count - 1].SumDenomi.Sum(); } Dispatcher.Invoke(() => { //Update UI txtTotalMoney.Text = totalAllDenominations.ToString(); quantityDenomi0.Text = sessions[sessions.Count - 1].QuantityDenomi[9].ToString(); quantityDenomi1.Text = sessions[sessions.Count - 1].QuantityDenomi[8].ToString(); quantityDenomi2.Text = sessions[sessions.Count - 1].QuantityDenomi[7].ToString(); quantityDenomi3.Text = sessions[sessions.Count - 1].QuantityDenomi[6].ToString(); quantityDenomi4.Text = sessions[sessions.Count - 1].QuantityDenomi[5].ToString(); quantityDenomi5.Text = sessions[sessions.Count - 1].QuantityDenomi[4].ToString(); quantityDenomi6.Text = sessions[sessions.Count - 1].QuantityDenomi[3].ToString(); quantityDenomi7.Text = sessions[sessions.Count - 1].QuantityDenomi[2].ToString(); quantityDenomi8.Text = sessions[sessions.Count - 1].QuantityDenomi[1].ToString(); quantityDenomi9.Text = sessions[sessions.Count - 1].QuantityDenomi[0].ToString(); totalAmountQuantityDenomi0.Text = sessions[sessions.Count - 1].SumDenomi[9].ToString(); totalAmountQuantityDenomi1.Text = sessions[sessions.Count - 1].SumDenomi[8].ToString(); totalAmountQuantityDenomi2.Text = sessions[sessions.Count - 1].SumDenomi[7].ToString(); totalAmountQuantityDenomi3.Text = sessions[sessions.Count - 1].SumDenomi[6].ToString(); totalAmountQuantityDenomi4.Text = sessions[sessions.Count - 1].SumDenomi[5].ToString(); totalAmountQuantityDenomi5.Text = sessions[sessions.Count - 1].SumDenomi[4].ToString(); totalAmountQuantityDenomi6.Text = sessions[sessions.Count - 1].SumDenomi[3].ToString(); totalAmountQuantityDenomi7.Text = sessions[sessions.Count - 1].SumDenomi[2].ToString(); totalAmountQuantityDenomi8.Text = sessions[sessions.Count - 1].SumDenomi[1].ToString(); totalAmountQuantityDenomi9.Text = sessions[sessions.Count - 1].SumDenomi[0].ToString(); }); } private void btnCheck_Click(object sender, RoutedEventArgs e) { //Xóa tiền đã đếm trong lần trước, cập nhật lại giao diện var check = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x31, 0x76, 0x10, 0x03, 0x44 }; serialPort.Write(check, 0, check.Length); } private void btnStoreStart_Click(object sender, RoutedEventArgs e) { var storeStart = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x31, 0x75, 0x10, 0x03, 0x47 }; serialPort.Write(storeStart, 0, storeStart.Length); } private void btnStart_Click(object sender, RoutedEventArgs e) { string deSenseDataRev01 = "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 00 00 01 70 00 10 03 35"; string deSenseDataRev02 = "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 01 00 02 70 00 10 03 37"; string deSenseDataRev03 = "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 02 00 03 70 00 10 03 35"; string deSenseDataRev04 = "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 03 00 04 70 00 10 03 33"; string deSenseDataRev05 = "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 04 00 05 70 00 10 03 35"; string deSenseDataRev00 = "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 05 00 00 70 00 10 03 31"; string[] deSenseDataRevSatisfy = { "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 05 00 05 70 00 10 03 34", "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 04 00 04 70 00 10 03 34", "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 03 00 03 70 00 10 03 34", "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 02 00 02 70 00 10 03 34", "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 01 00 01 70 00 10 03 34", "10 02 30 32 30 60 40 04 41 00 00 00 00 00 00 00 00 10 10 00 00 00 00 00 70 00 10 03 34" }; var deSense = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x31, 0x60, 0x10, 0x03, 0x52 }; serialPort.Write(deSense, 0, deSense.Length); Thread.Sleep(100); if (hexReceivedString == deSenseDataRev01) { flag = 1; var eventDataFixed_1 = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x33, 0x65, 0x00, 0x01, 0x10, 0x03, 0x54 }; serialPort.Write(eventDataFixed_1, 0, eventDataFixed_1.Length); } else if (hexReceivedString == deSenseDataRev02) { flag = 1; var eventDataFixed_2 = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x33, 0x65, 0x00, 0x02, 0x10, 0x03, 0x57 }; serialPort.Write(eventDataFixed_2, 0, eventDataFixed_2.Length); } else if (hexReceivedString == deSenseDataRev03) { flag = 1; var eventDataFixed_3 = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x33, 0x65, 0x00, 0x03, 0x10, 0x03, 0x56 }; serialPort.Write(eventDataFixed_3, 0, eventDataFixed_3.Length); } else if (hexReceivedString == deSenseDataRev04) { flag = 1; var eventDataFixed_4 = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x33, 0x65, 0x00, 0x04, 0x10, 0x03, 0x51 }; serialPort.Write(eventDataFixed_4, 0, eventDataFixed_4.Length); } else if (hexReceivedString == deSenseDataRev05) { flag = 1; var eventDataFixed_5 = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x33, 0x65, 0x00, 0x05, 0x10, 0x03, 0x50 }; serialPort.Write(eventDataFixed_5, 0, eventDataFixed_5.Length); } else if (hexReceivedString == deSenseDataRev00) { flag = 1; var eventDataFixed_0 = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x33, 0x65, 0x00, 0x00, 0x10, 0x03, 0x55 }; serialPort.Write(eventDataFixed_0, 0, eventDataFixed_0.Length); } else if (deSenseDataRevSatisfy.Contains(hexReceivedString)) { flag = 1; }; if(flag == 1) { var start = new byte[] { 0x10, 0x02, 0x30, 0x31, 0x33, 0x70, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x00, 0x10, 0x03, 0x48 }; //lock serialPort.Write(start, 0, start.Length); } } private void btnEnd_Click(object sender, RoutedEventArgs e) { //Đặt lại tất cả , kết thúc phiên và sẵn sàng vào phiên mới //int[] numbers = new int[] {1,2,3,4,5,6}; //Array.Clear(numbers, 0, numbers.Length); var end = new byte[] { 0x10, 0x02, 0x30, 0x30, 0x31, 0x71, 0x10, 0x03, 0x43 }; serialPort.Write(end, 0, end.Length); flag = 0; maxQuantitySheet = 0; } } }