public void ReceiveDataThread() { LogWriter.WriteLog(new string[] { "[Test]ReceiveData begin" }, true); string strRipFileName = "RipPrintData.prt"; //SystemCall.isPmPrint = false; byte[] chRequest = new byte[CachePrinter.BUFSIZE]; int cbBytesRead = 0; bool fSuccess = false; bool isSetWhiteFormPrt = false; bool isUseJobModeSetting = false; SPrinterSetting oldPrinterSetting = m_IPrinterChange.GetAllParam().PrinterSetting; //UVData oldUVData = new UVData(); //oldUVData.Load(); //启动发送打印数据线程 cachePrinter.Run(); Mutex mutex = new Mutex(true, MUTEX_PORT_OPEN); int sendBytes = 0; if (isRipPrintData) { File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strRipFileName)); } // The thread's parameter is a handle to a pipe instance. while (true) { // Read client requests from the pipe. if (m_hPipeHandle != 0) { cbBytesRead = 0; fSuccess = CNamedPipe.ReadFile( m_hPipeHandle, // handle to pipe chRequest, // buffer to receive data CachePrinter.BUFSIZE, // size of buffer ref cbBytesRead, // number of bytes read 0); // not overlapped I/O } if (fSuccess) { MyStruct data = new MyStruct(); data.buflen = cbBytesRead; data.buf = new byte[CachePrinter.BUFSIZE]; Buffer.BlockCopy(chRequest, 0, data.buf, 0, cbBytesRead); cachePrinter.AddPrintData(data); } if (!fSuccess || cbBytesRead == 0) { cachePrinter.ReceiveDataFinishedFlag(true); break; } } cachePrinter.WaitSendPrintDataFinish(); if (isSetWhiteFormPrt || isUseJobModeSetting) { m_IPrinterChange.GetAllParam().PrinterSetting = oldPrinterSetting; CoreInterface.SetPrinterSetting(ref m_IPrinterChange.GetAllParam().PrinterSetting); LogWriter.WriteLog(new string[] { "[RIP]SetOldPrinterSetting" }, true); } if (isUseJobModeSetting) { //oldUVData.Save(); } LogWriter.WriteLog(new string[] { "[RIP]Printer close" }, true); mutex.Close(); // Flush the pipe to allow the client to read the pipe's contents // before disconnecting. Then disconnect the pipe, and close the // handle to this pipe instance. //FlushFileBuffers(hPipe); CNamedPipe.DisconnectNamedPipe(m_hPipeHandle); ///CloseHandle(hPipe); fConnected = false; }
public void SendPrintData() { int hPrinterPort = 0; try { bool isFirstRIP = false; _bSendExited = _bReadExited = false; bool bFileHeader = true; while (_cachList.Count < 2048 && !_bReadExited) //缓存一定数量才开始打印16M { Thread.Sleep(100); } while ((hPrinterPort = CoreInterface.Printer_Open()) == 0) { Thread.Sleep(100); } SPrinterSetting ssNew = this.m_IPrinterChange.GetAllParam().PrinterSetting; SJobSetting sjobseting = new SJobSetting(); if (UIFunctionOnOff.SupportPrintMode) { //todo } if (UIFunctionOnOff.SupportMediaMode) { //todo } LayoutSetting curLayoutSetting = new LayoutSetting(); int layoutIdx = 0; if (CoreInterface.LayoutIndex >= 0) { layoutIdx = CoreInterface.LayoutIndex; } if (NewLayoutFun.GetLayoutSetting(layoutIdx, ref curLayoutSetting)) { ssNew.layoutSetting = curLayoutSetting; } if (ssNew.sExtensionSetting.AutoRunAfterPrint) { ssNew.sBaseSetting.fYAddDistance = ssNew.sExtensionSetting.fRunDistanceAfterPrint; ssNew.sExtensionSetting.bEnableAnotherUvLight = ssNew.sExtensionSetting.fRunDistanceAfterPrint > 0; CoreInterface.SetPrinterSetting(ref ssNew, false); //打印结束后继续扫描一段距离生效 } //打印前设置JobSetting sjobseting.bReversePrint = ssNew.sBaseSetting.bReversePrint; CoreInterface.SetSJobSetting(ref sjobseting); LogWriter.WriteLog(new string[] { "[RIP]Printer open" }, true); int cbBytesRead = 0; isFirstRIP = true; while (true) { if (_cachList.Count > 0) { MyStruct data = new MyStruct(); if (!_cachList.TryDequeue(out data)) { continue; } byte[] chRequest = data.buf; cbBytesRead = data.buflen; #if ADD_HARDKEY { int subsize = 32; byte[] lastValue = chRequest; if (bFileHeader) { bFileHeader = false; lastValue = new byte[BUFSIZE + 8]; byte[] mjobhead = new byte[subsize]; byte[] retValue = new byte[subsize + Marshal.SizeOf(typeof(int))]; for (int j = 0; j < BYHXSoftLock.JOBHEADERSIZE / subsize; j++) { Buffer.BlockCopy(chRequest, j * mjobhead.Length, mjobhead, 0, mjobhead.Length); BYHX_SL_RetValue ret = BYHXSoftLock.CheckValidDateWithData(mjobhead, ref retValue); Buffer.BlockCopy(retValue, 0, lastValue, j * retValue.Length, retValue.Length); } Buffer.BlockCopy(chRequest, BYHXSoftLock.JOBHEADERSIZE, lastValue, BYHXSoftLock.JOBHEADERSIZE + 8, chRequest.Length - BYHXSoftLock.JOBHEADERSIZE); int sendBytes = CoreInterface.Printer_Send(hPrinterPort, lastValue, cbBytesRead + 8); Debug.Assert(sendBytes == cbBytesRead + 8); } else { CoreInterface.Printer_Send(hPrinterPort, chRequest, cbBytesRead); } } #else { if (isFirstRIP) { string strLog = ""; int nVersion = 0; byte bReversePrint = 0; byte nPrintLayerNum = 0; int printmodePerLayer = 0; isFirstRIP = false; if (cbBytesRead >= 84) { for (int i = 0; i < 84; i++) { strLog += Convert.ToString(chRequest[i], 2) + ","; } LogWriter.WriteLog(new string[] { "[RIP]" + strLog }, true); nVersion = BitConverter.ToInt32(chRequest, 4); if (nVersion == 4) { bReversePrint = chRequest[55]; //nPrintLayerNum = chRequest[56]; //printmodePerLayer = BitConverter.ToInt32(chRequest, 57); //PrinterSettingHelper.SetPropertyWhiteInkLayer( // ref m_IPrinterChange.GetAllParam().PrinterSetting, bReversePrint, // nPrintLayerNum, // (uint) printmodePerLayer); CoreInterface.SetPrinterSetting(ref m_IPrinterChange.GetAllParam() .PrinterSetting); //isSetWhiteFormPrt = true; sjobseting = new SJobSetting(); sjobseting.bReversePrint = bReversePrint == 1; CoreInterface.SetSJobSetting(ref sjobseting); } else if (nVersion == 6) { bReversePrint = chRequest[71]; sjobseting = new SJobSetting(); sjobseting.bReversePrint = bReversePrint == 1; CoreInterface.SetSJobSetting(ref sjobseting); } } } //#else CoreInterface.Printer_Send(hPrinterPort, chRequest, cbBytesRead); } #endif } if (_cachList.Count == 0 && _bReadExited) { LogWriter.WriteLog( new string[] { string.Format("[SendPrintData] CachList.Count = {0};bReadExited={1}", _cachList.Count, _bReadExited) }, true); break; } if (_cachList.Count == 0) { LogWriter.WriteLog( new string[] { string.Format("[SendPrintData] CachList.Count = {0};waittime={1}", _cachList.Count, 10) }, true); Thread.Sleep(10); } } CoreInterface.Printer_Close(hPrinterPort); _bSendExited = true; SExtensionSetting extensionSetting = ssNew.sExtensionSetting; if (extensionSetting.fRunDistanceAfterPrint > 0 && extensionSetting.BackBeforePrint) { while (true) { JetStatusEnum status = CoreInterface.GetBoardStatus(); if (status == JetStatusEnum.Ready) { int speed = ssNew.sMoveSetting.nYMoveSpeed; MoveDirectionEnum dir = MoveDirectionEnum.Up; int len = Convert.ToInt32(extensionSetting.fRunDistanceAfterPrint * m_IPrinterChange.GetAllParam().PrinterProperty.fPulsePerInchY); CoreInterface.MoveCmd((int)dir, len, speed); break; } Thread.Sleep(100); } } } catch (Exception e) { LogWriter.WriteLog( new string[] { string.Format("[SendPrintData] CachList.Count = {0};Exception={1}", _cachList.Count, e.Message) }, true); if (hPrinterPort != 0) { CoreInterface.Printer_Close(hPrinterPort); _bSendExited = true; } } }