using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Management; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { internal class Program { static string RandomString(int length) { var random = new Random(); const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; var str = new char[length]; for (int i = 0; i < length; i++) { str[i] = chars[random.Next(chars.Length)]; } return new string(str); } static string RandomNumber(int length) { var random = new Random(); string number = ""; for (int i = 0; i < length; i++) number += random.Next(0, 10).ToString(); return number; } private static string GetVendorId(string portName) { try { using (var searcher = new ManagementObjectSearcher( "SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%(" + portName + ")%'")) { foreach (var obj in searcher.Get()) { string deviceId = obj["PNPDeviceID"]?.ToString(); if (deviceId != null) { var vidIndex = deviceId.IndexOf("VID_"); if (vidIndex >= 0) { return deviceId.Substring(vidIndex + 4, 4); } } } } } catch (Exception ex) { Console.WriteLine("Failed to get vendor ID: " + ex.Message); } return null; } static void Main(string[] args) { //Console.WriteLine(GetVendorId("COM3")); string[] portNames = SerialPort.GetPortNames(); string selectedPort = null; foreach (string port in portNames) { string vendorId = GetVendorId(port); var knownVendors = new Dictionary { { "0483", "STM32" }, { "0403", "FTDI" }, { "10C4", "Silicon Labs CP210x" }, { "067B", "Prolific" }, { "1A86", "QinHeng CH340" }, { "16C0", "VOTI" } }; if (vendorId != null && knownVendors.ContainsKey(vendorId.ToUpper())) { selectedPort = port; break; } } if (selectedPort == null && portNames.Length > 0) { selectedPort = portNames[0]; } int baudRate = 921600; // Replace with appropriate baud rate using (SerialPort serialPort = new SerialPort(selectedPort, baudRate, Parity.None, 8, StopBits.One)) { serialPort.Open(); // Initialize commands dictionary var commands = new Dictionary { { "1", "welcome" }, { "2", "final" }, { "3", "to_pay" }, { "4", "success" }, { "5", "fail" }, { "6", "cancel" }, { "7", "billjson" }, { "8", "newbill" }, { "0", "exit" } }; Console.WriteLine("Press '0' to Exit."); Console.WriteLine("Press '1' for Welcome Message."); Console.WriteLine("Press '2' for Final Invoice Message."); Console.WriteLine("Press '3' for To Pay QR."); Console.WriteLine("Press '4' for Payment Success Message."); Console.WriteLine("Press '5' for Payment Fail Message."); Console.WriteLine("Press '6' for Payment Cancel Message."); Console.WriteLine("Press '7' for Display Bill."); Console.WriteLine("Press '8' for Display Single Item."); while (true) { Console.Write("Enter command number (0-8): "); string choice = Console.ReadLine()?.Trim(); if (!commands.ContainsKey(choice)) { Console.WriteLine("Invalid choice, try again."); continue; } string commandKey = commands[choice]; if (commandKey == "exit") { Console.WriteLine("Exiting..."); break; } string command = ""; switch (commandKey) { case "welcome": Console.Write("Enter company name: "); string company = Console.ReadLine(); command = $"WelcomeScreen**{company}"; break; case "final": Console.Write("Enter total: "); string total = Console.ReadLine(); Console.Write("Enter discount: "); string discount = Console.ReadLine(); Console.Write("Enter tax: "); string tax = Console.ReadLine(); Console.Write("Enter grand total: "); string grandTotal = Console.ReadLine(); command = $"DisplayTotalScreen**{total}**{discount}**{tax}**{grandTotal}"; break; case "to_pay": Console.Write("Enter UPI string: "); string upi = Console.ReadLine(); Console.Write("Enter amount: "); string amt = Console.ReadLine(); Console.Write("Enter VPA: "); string vpa = Console.ReadLine(); command = $"DisplayQRCodeScreen**{upi}**{amt}**{vpa}"; break; case "success": case "fail": case "cancel": Console.Write("Enter bank RRN (leave empty to generate): "); string bankRrn = Console.ReadLine(); if (string.IsNullOrEmpty(bankRrn)) bankRrn = RandomNumber(10); Console.Write("Enter Order ID (leave empty to generate): "); string orderId = Console.ReadLine(); if (string.IsNullOrEmpty(orderId)) orderId = "ORD" + RandomNumber(6); Console.Write("Enter Date (dd-MM-yyyy, leave empty for today): "); string date = Console.ReadLine(); if (string.IsNullOrEmpty(date)) date = DateTime.Now.ToString("dd-MM-yyyy"); Console.Write("Enter Amount: "); string amt2 = Console.ReadLine(); if (commandKey == "success") command = $"DisplaySuccessQRCodeScreen**{bankRrn}**{orderId}**{date}**{amt2}"; else if (commandKey == "fail") command = $"DisplayFailQRCodeScreen**{bankRrn}**{orderId}**{date}**{amt2}"; else command = $"DisplayCancelQRCodeScreen**{bankRrn}**{orderId}**{date}**{amt2}"; break; case "billjson": string json = "{\"storeInfo\": {\"storeName\": \"Test Store\",\"address\": \"123 Test Street\",\"phone\": \"123-456-7890\",\"gstin\": \"TEST123456\"},\"billInfo\": {\"billNo\": \"1\",\"date\": \"2025-02-17\",\"time\": \"12:00\",\"cashier\": \"Test Cashier\"},\"customerInfo\": {\"customerName\": \"John Doe\",\"customerPhone\": \"987-654-3210\"},\"items\": [{\"itemName\": \"Item 1\",\"quantity\": 2,\"pricePerUnit\": 10.0,\"total\": 20.0},{\"itemName\": \"Item 2\",\"quantity\": 1,\"pricePerUnit\": 15.0,\"total\": 15.0},{\"itemName\": \"Item 3\",\"quantity\": 1,\"pricePerUnit\": 15.0,\"total\": 15.0}],\"totals\": {\"subTotal\": 50.0,\"tax\": 5.0,\"discount\": 0.0,\"totalAmount\": 55.0,\"amountPaid\": 55.0,\"paymentMethod\": \"Cash\"},\"footer\": {\"message\": \"Thank you for testing!\",\"website\": \"www.teststore.com\"}}"; command = $"billjson**{json}"; break; case "newbill": Console.Write("Enter item name: "); string itemName = Console.ReadLine(); Console.Write("Enter item code: "); string itemCode = Console.ReadLine(); Console.Write("Enter price: "); string price = Console.ReadLine(); Console.Write("Enter quantity: "); string qty = Console.ReadLine(); Console.Write("Enter unit: "); string unit = Console.ReadLine(); Console.Write("Enter subtotal: "); string subtotal = Console.ReadLine(); Console.Write("Enter grand total: "); string grandTotal2 = Console.ReadLine(); command = $"newbill**{itemName}**{itemCode}**{price}**{qty}**{unit}**{subtotal}**{grandTotal2}"; break; } serialPort.WriteLine(command); Console.WriteLine($"Sent: {command}"); } serialPort.Close(); } } } }