■ SerialPort 클래스에서 유진시스텍 YJ-730T 키오스크 프린터를 사용하는 방법을 보여준다.
▶ DataReceivedEventArgs.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; namespace TestProject { /// <summary> /// 데이터 수신시 이벤트 인자 /// </summary> public class DataReceivedEventArgs : EventArgs { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 데이터 - Data /// <summary> /// 데이터 /// </summary> public string Data { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DataReceivedEventArgs(data) /// <summary> /// 생성자 /// </summary> /// <param name="data">데이터</param> public DataReceivedEventArgs(string data) { Data = data; } #endregion } } |
▶ PrinterHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
using System; using System.Diagnostics; using System.IO.Ports; using System.Management; using System.Text; using System.Text.RegularExpressions; namespace TestProject { /// <summary> /// 프린터 헬퍼 /// </summary> /// <remarks> /// 유진시스텍 YJ-730T 키오스크 프린터 /// </remarks> public static class PrinterHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 데이터 수신시 - DataReceived /// <summary> /// 데이터 수신시 /// </summary> public static event EventHandler<DataReceivedEventArgs> DataReceived; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 직렬 포트 /// </summary> private static SerialPort _serialPort = new SerialPort(); /// <summary> /// 프린터 COM 포트 /// </summary> private static readonly string _printerCOMPort = "2"; /// <summary> /// 매장명 /// </summary> private static readonly string _storeName = "수원역점"; /// <summary> /// 주문 번호 /// </summary> private static readonly string _orderNumber = "주문번호"; /// <summary> /// 영수증 제목 /// </summary> private static readonly string _receiptTItle = "영수증 (고객용)"; /// <summary> /// 감사말 /// </summary> private static readonly string _thankYou = "감사합니다."; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public ////////////////////////////////////////////////////////////////////// Function #region 연결 여부 구하기 - IsConnected() /// <summary> /// 연결 여부 구하기 /// </summary> /// <returns>연결 여부</returns> public static bool IsConnected() { bool isConnected = false; string query = "SELECT * FROM Win32_PnPEntity WHERE ClassGuid = '{4d36e978-e325-11ce-bfc1-08002be10318}'"; try { using(ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("root\\CIMV2", query)) { foreach(ManagementObject managementObject in managementObjectSearcher.Get()) { string port = Regex.Replace(managementObject["Name"].ToString(), "[^0-9]", ""); if(port.Equals(_printerCOMPort)) { isConnected = true; break; } } } } catch(Exception exception) { Debug.WriteLine(exception.ToString()); } return isConnected; } #endregion #region 열기 - Open() /// <summary> /// 열기 /// </summary> /// <returns>처리 결과</returns> public static bool Open() { try { if(_serialPort != null) { _serialPort.Close(); } _serialPort = new SerialPort(); _serialPort.PortName = $"COM{_printerCOMPort}"; _serialPort.BaudRate = 9600; _serialPort.Parity = Parity.None; _serialPort.DataBits = 8; _serialPort.StopBits = StopBits.One; _serialPort.Handshake = Handshake.XOnXOff; _serialPort.DataReceived += serialPort_DataReceived; _serialPort.Open(); Debug.WriteLine($"OPEN : {_serialPort.PortName} {_serialPort.IsOpen}"); } catch(Exception exception) { Debug.WriteLine(exception.ToString()); } return _serialPort.IsOpen; } #endregion #region 상태 요청하기 - RequestStatus() /// <summary> /// 상태 요청하기 /// </summary> public static void RequestStatus() { try { Debug.WriteLine("REQUEST STATUS"); byte[] commandByteArray = new byte[2] { 0x1B, 0x07 }; SendCommand(commandByteArray); } catch(Exception exception) { Debug.WriteLine(exception.ToString()); } } #endregion #region 닫기 - Close() /// <summary> /// 닫기 /// </summary> public static void Close() { _serialPort.Close(); Debug.WriteLine($"CLOSE : {_serialPort.PortName} {_serialPort.IsOpen}"); } #endregion #region 주문서 인쇄하기 - PrintOrderSheet(orderNumber) /// <summary> /// 주문서 인쇄하기 /// </summary> /// <param name="orderNumber">주문 번호</param> public static void PrintOrderSheet(string orderNumber) { try { Debug.WriteLine($"PRINT ORDER SHEET : ORDER NUMBER = {orderNumber}"); // Center Alignment SendCommand(new byte[] { 0x1B, 0x61, 0x01 }); byte fontSize1 = ((byte)0xF0 & ((byte)0x00 << 4) | ((byte)0x0F & (byte)(0x00))); byte fontSize2 = ((byte)0xF0 & ((byte)0x01 << 4) | ((byte)0x0F & (byte)(0x01))); SendCommand(new byte[] { 0x1D, 0x21, fontSize1 }); byte[] commandByteArray = Encoding.Default.GetBytes($"{_storeName}\n{DateTime.Now:yyyy-MM-dd HH:mm:ss}"); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x02 }); // Print & n Line Feed SendCommand(new byte[] { 0x1D, 0x21, fontSize2 }); commandByteArray = Encoding.Default.GetBytes($"[{_orderNumber} : {orderNumber}]"); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x02 }); // Print & n Line Feed SendCommand(new byte[] { 0x1D, 0x21, fontSize1 }); commandByteArray = Encoding.Default.GetBytes($"{_thankYou}\n"); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x06 }); // Print & n Line Feed //SendCommand(new byte[] { 0x1B, 0x69 }); // Partial Cut SendCommand(new byte[] { 0x1B, 0x6D }); // Full Cut } catch(Exception exception) { Debug.WriteLine(exception.ToString()); } } #endregion #region 영수증 인쇄하기 - PrintReceipt(storeInformation, orderNumberInformation, orderItemInformation, paymentInformation) /// <summary> /// 영수증 인쇄하기 /// </summary> /// <param name="storeInformation">매장 정보</param> /// <param name="orderNumberInformation">주문 번호 정보</param> /// <param name="orderItemInformation">주문 항목 정보</param> /// <param name="paymentInformation">결제 정보</param> public static void PrintReceipt(string storeInformation, string orderNumberInformation, string orderItemInformation, string paymentInformation) { try { Debug.WriteLine($"PRINT RECEIPT : {orderNumberInformation}"); byte fontSize1 = ((byte)0xF0 & ((byte)0x00 << 4) | ((byte)0x0F & (byte)(0x00))); byte fontSize2 = ((byte)0xF0 & ((byte)0x01 << 4) | ((byte)0x0F & (byte)(0x01))); SendCommand(new byte[] { 0x1B, 0x61, 0x00 }); // Left Alignment SendCommand(new byte[] { 0x1D, 0x21, fontSize1 }); byte[] commandByteArray = Encoding.Default.GetBytes(storeInformation); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x01 }); // Print & n Line Feed SendCommand(new byte[] { 0x1B, 0x61, 0x01 }); // Center Alignment SendCommand(new byte[] { 0x1D, 0x21, fontSize2 }); commandByteArray = Encoding.Default.GetBytes(orderNumberInformation); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x01 }); // Print & n Line Feed SendCommand(new byte[] { 0x1B, 0x61, 0x00 }); // Left Alignment SendCommand(new byte[] { 0x1D, 0x21, fontSize1 }); commandByteArray = Encoding.Default.GetBytes(orderItemInformation); SendCommand(commandByteArray); // Write Buffer commandByteArray = Encoding.Default.GetBytes(_receiptTItle.PadLeft(26, ' ')); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x00 }); // Print & n Line Feed SendCommand(new byte[] { 0x1B, 0x61, 0x00 }); // Left Alignment commandByteArray = Encoding.Default.GetBytes(paymentInformation); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x01 }); // Print & n Line Feed SendCommand(new byte[] { 0x1B, 0x61, 0x01 }); // Center Alignment commandByteArray = Encoding.Default.GetBytes(_thankYou); SendCommand(commandByteArray); // Write Buffer SendCommand(new byte[] { 0x1B, 0x64, 0x06 }); // Print & n Line Feed //SendCommand(new byte[] { 0x1B, 0x69 }); // Partial Cut SendCommand(new byte[] { 0x1B, 0x6D }); // Full Cut } catch(Exception exception) { Debug.WriteLine(exception.ToString()); } } #endregion //////////////////////////////////////////////////////////////////////////////// Private ////////////////////////////////////////////////////////////////////// Event #region 직렬 포트 데이터 수신시 처리하기 - serialPort_DataReceived(sender, e) /// <summary> /// 직렬 포트 데이터 수신시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { int byteCountToRead = _serialPort.BytesToRead; if(byteCountToRead > 0) { byte[] bufferByteArray = new byte[byteCountToRead]; _serialPort.Read(bufferByteArray, 0, byteCountToRead); if(DataReceived != null) { Debug.WriteLine($"SERIAL PORT DATA RECEIVED : [{BitConverter.ToString(bufferByteArray)}]"); string argument = string.Empty; byte flagByte = byteCountToRead == 2 ? bufferByteArray[1] : bufferByteArray[0]; switch(flagByte) { case 0x41 : // Paper Empty argument = "Paper Empty"; break; case 0x42 : // Caver Open argument = "Caver Open"; break; case 0x50 : // Roll Paper Empty case 0x21 : // Roll Paper Empty argument = "Roll Paper Empty"; break; case 0x44 : // Head TM Over argument = "Head TM Over"; break; case 0x40 : // OK break; } if(!string.IsNullOrEmpty(argument)) { DataReceived(sender, new DataReceivedEventArgs(argument)); } } } } catch(Exception exception) { Debug.WriteLine(exception.ToString()); } } #endregion ////////////////////////////////////////////////////////////////////// Function #region 명령 보내기 - SendCommand(commandByteArray) /// <summary> /// 명령 보내기 /// </summary> /// <param name="commandByteArray">명령 바이트 배열</param> private static void SendCommand(byte[] commandByteArray) { try { if(!_serialPort.IsOpen) { PrinterHelper.Open(); } if(_serialPort.IsOpen) { _serialPort.Write(commandByteArray, 0, commandByteArray.Length); Debug.WriteLine($"SEND COMMAND : [{BitConverter.ToString(commandByteArray)}]"); } } catch(Exception exception) { Debug.WriteLine(exception.ToString()); } } #endregion } } |