完美冰蓝

为心爱的人敲十万行代码!

POS机硬件实现

 1         /// <summary>
 2         /// POS机弹出钱箱
 3         /// </summary>
 4         public static void MoneyOut()
 5         {
 6             try
 7             {
 8                 IntPtr iHandle = CreateFile("LPT1"0x4000000000300);//"LPT1"
 9                 if (iHandle.ToInt32() == -1)
10                 {
11                     MessageBox.Show("没有连接打印机或者打印机端口未打开!""错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
12                 }
13                 else
14                 {
15                     Microsoft.Win32.SafeHandles.SafeFileHandle handle = new Microsoft.Win32.SafeHandles.SafeFileHandle(iHandle, true);
16                     FileStream fs = new FileStream(handle, FileAccess.ReadWrite);
17                     StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
18                     sw.Write(((char)27).ToString() + "p" + ((char)0).ToString() + ((char)60).ToString() + ((char)255).ToString());
19 
20                     sw.Close();
21 
22                     fs.Close();
23                 }
24             }
25             catch (Exception e)
26             {
27                 throw;
28             }
29         }
30 
31         //Win32 API:打开端口
32         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
33         private static extern IntPtr CreateFile(string lpFileName,
34         int dwDesiredAccess,
35         int dwShareMode,
36         int lpSecurityAttributes,
37         int dwCreationDisposition,
38         int dwFlagsAndAttributes,
39         int hTemplateFile);
40 
41         private static void Clear()
42         {
43             SerialPort serialPort1 = new SerialPort();
44             serialPort1.PortName = "COM1";
45             serialPort1.BaudRate = 2400;
46             serialPort1.Open();
47             serialPort1.WriteLine("\f");
48             serialPort1.Close();
49         }
50 
51         private static void Init()
52         {
53             SerialPort serialPort1 = new SerialPort();
54             serialPort1.PortName = "COM1";
55             serialPort1.BaudRate = 2400;
56             serialPort1.Open();
57             serialPort1.WriteLine("esc@");
58             serialPort1.Close();
59         }
60 
61         private static void 单价(string data)
62         {
63             SerialPort serialPort1 = new SerialPort();
64             serialPort1.PortName = "COM1";
65             serialPort1.BaudRate = 2400;
66             serialPort1.Open();
67             serialPort1.WriteLine(@"s1 Q A" + Char.ConvertFromUtf32(27) + Char.ConvertFromUtf32(81) + Char.ConvertFromUtf32(65) + data + Char.ConvertFromUtf32(13));
68             serialPort1.Close();
69         }
70 
71         private static void 总计(string data)
72         {
73             SerialPort serialPort1 = new SerialPort();
74             serialPort1.PortName = "COM1";
75             serialPort1.BaudRate = 2400;
76             serialPort1.Open();
77             serialPort1.WriteLine(@"s2 Q A" + Char.ConvertFromUtf32(27) + Char.ConvertFromUtf32(81) + Char.ConvertFromUtf32(65) + data + Char.ConvertFromUtf32(13));
78             serialPort1.Close();
79         }
80 
81         private static void 收款(string data)
82         {
83             SerialPort serialPort1 = new SerialPort();
84             serialPort1.PortName = "COM1";
85             serialPort1.BaudRate = 2400;
86             serialPort1.Open();
87             serialPort1.WriteLine(@"s3 Q A " + Char.ConvertFromUtf32(27) + Char.ConvertFromUtf32(81) + Char.ConvertFromUtf32(65) + data + Char.ConvertFromUtf32(13));
88             serialPort1.Close();
89         }
90 
91         private static void 找零(string data)
92         {
93             SerialPort serialPort1 = new SerialPort();
94             serialPort1.PortName = "COM1";
95             serialPort1.BaudRate = 2400;
96             serialPort1.Open();
97             serialPort1.WriteLine(@"s4 Q A " + Char.ConvertFromUtf32(27) + Char.ConvertFromUtf32(81) + Char.ConvertFromUtf32(65) + data + Char.ConvertFromUtf32(13));
98             serialPort1.Close();
99         }
 

posted on 2011-09-25 15:17  完美冰蓝  阅读(445)  评论(0编辑  收藏  举报

导航