public void TestToleranceBand()
        {
            ToleranceBand band = new ToleranceBand();

            band.Color = ResistorTolerance.Gold;

            Assert.AreEqual(5, band.Value);
        }
        public void TestFourBands()
        {
            Band           firstBand  = new Band();
            Band           secondBand = new Band();
            MultiplierBand thirdBand  = new MultiplierBand();
            ToleranceBand  fourthBand = new ToleranceBand();

            //25000 +-25%
            firstBand.Color  = ResistorColor.Red;
            secondBand.Color = ResistorColor.Green;
            thirdBand.Color  = ResistorMultiplier.Orange;
            fourthBand.Color = ResistorTolerance.Silver;

            float value = ((10 * firstBand.Value) + secondBand.Value) * thirdBand.Value;

            Assert.AreEqual(25000, value);
        }
        public Resistor()
        {
            _firstBand = new ValueBand {
                BandNumber = 1
            };
            _secondBand = new ValueBand {
                BandNumber = 2
            };
            _thirdBand = new ValueBand {
                BandNumber = 3
            };
            _toleranceBand = new ToleranceBand();

            Bands = new List <AbstractBand> {
                _firstBand, _secondBand, _thirdBand, _toleranceBand
            };
        }
Beispiel #4
0
        private void HandleProtocol(string protocol, int i)
        {
            try
            {
                lock (this.tbandMonitor)
                {
                    if (curStatus != JetStatusEnum.Pause && !bManualTrigger)
                    {
                        return;
                    }
                    string[] temp = protocol.Split(new char[] { ';' });
                    //float X_OFFSET = 0;
                    float lXDistance = 0, rXDistance = 0, lYDistance = 0, rYDistance = 0, lWidth = 0, lHeight = 0;
                    float lXCellCount = 0, rXCellCount = 0, lYCellCount = 0, rYCellCount = 0, rWidth = 0, rHeight = 0;
                    #region 解析并处理返回的数据
                    switch (i)
                    {
                    case 0:
                        #region 左侧相机
                        Y_StepL = 0;
                        if (!this.tbandMonitor.bLeftUpdated)
                        {
                            lYDistance  = Convert.ToSingle(temp[6]);
                            lYCellCount = Convert.ToInt32(temp[5]);
                            lXDistance  = Convert.ToSingle(temp[8]);
                            lXCellCount = Convert.ToInt32(temp[7]);
                            lHeight     = Convert.ToSingle(temp[9]);
                            lWidth      = Convert.ToSingle(temp[10]);

                            // 如果x方向识别成功
                            if (Convert.ToInt32(temp[4]) == 1)
                            {
                                this.tBand.LeftTol_X = this.GetLeftTol_X(Convert.ToSingle(temp[2]));
                            }
                            else
                            {
                                tBand.LeftTol_X = PreBand.LeftTol_X;
                            }
                            // 如果y方向识别成功
                            if (Convert.ToInt32(temp[3]) == 1)
                            {
                                Y_StepL = this.GetY_Step(Convert.ToSingle(temp[1]), bandCount);
                            }
                            else
                            {
                                tBand.LeftTol_Y = PreBand.LeftTol_Y;
                            }
                        }
                        else
                        {
                            worker.ReportProgress(0, "Error:连续两次Left");
                        }
                        if (bManualTrigger)
                        {
                            bManualTrigger = false;
//								if(mTcpipRespondTimer.Enabled)
//								{
//									mTcpipRespondTimer.Stop();
//								}
                            worker.ReportProgress(1, this.tBand);
                        }
                        else
                        {
                            this.tbandMonitor.bLeftUpdated = true;
                        }
                        Debug.WriteLine(string.Format("bLeftUpdated = {0}: {1};bandCount={2}", tBand.LeftTol_X, protocol, bandCount));
                        if (worker != null)
                        {
                            worker.ReportProgress(0, string.Format("Left:{0};bandCount={1}", protocol, bandCount));
                            worker.ReportProgress(0, string.Format("Left offset point:{0};bandCount={1}", this.tBand.LeftTol_X, bandCount));
                        }
                        else
                        {
                            MessageBox.Show("worker is null");
                        }
                        #endregion
                        break;

                    case 1:
                        #region 右侧相机
                        Y_StepR     = 0;
                        rYDistance  = Convert.ToSingle(temp[6]);
                        rYCellCount = Convert.ToInt32(temp[5]);
                        rXDistance  = Convert.ToSingle(temp[8]);
                        rXCellCount = Convert.ToInt32(temp[7]);
                        rHeight     = Convert.ToSingle(temp[9]);
                        rWidth      = Convert.ToSingle(temp[10]);
                        if (!this.tbandMonitor.bRightUpdated)
                        {
                            this.tbandMonitor.bRightUpdated = true;
                            // 如果x方向识别成功
                            if (Convert.ToInt32(temp[4]) == 1)
                            {
                                this.tBand.RightTol_X = this.GetLeftTol_X(Convert.ToSingle(temp[2]));
                            }
                            else
                            {
                                tBand.RightTol_X = PreBand.RightTol_X;
                            }
                            // 如果y方向识别成功
                            if (Convert.ToInt32(temp[3]) == 1)
                            {
                                Y_StepR = this.GetY_Step(Convert.ToSingle(temp[1]), bandCount);
                            }
                            else
                            {
                                tBand.RightTol_Y = PreBand.RightTol_Y;
                            }
                        }
                        else
                        {
                            worker.ReportProgress(0, "Error:连续两次Right");
                        }
                        Debug.WriteLine(string.Format("bRightUpdated = {0}: {1}", tbandMonitor.bRightUpdated, protocol));
                        if (worker != null)
                        {
                            worker.ReportProgress(0, string.Format("Right:{0};bandCount={1}", protocol, bandCount));
                        }
                        else
                        {
                            MessageBox.Show("worker is null");
                        }
                        #endregion
                        break;
                    }
                    #endregion
                    if (tbandMonitor.HasReady)
                    {
                        int              pass = 0;
                        SPrinterSetting  ss   = this.m_iPrinterChange.GetAllParam().PrinterSetting;
                        SPrinterProperty sp   = this.m_iPrinterChange.GetAllParam().PrinterProperty;
                        UIPreference     up   = this.m_iPrinterChange.GetAllParam().Preference;
                        worker.ReportProgress(0, string.Format("CalibrationSetting.nStepPerHead={0}", ss.sCalibrationSetting.nStepPerHead));
                        int CellCountInBigCell = 10;

                        // 验证数据
                        if (lYDistance == 0 || lYCellCount == 0 || lHeight == 0)
                        {
                            lYDistance = lYCellCount = lHeight = 0;
                        }

                        if (rYDistance == 0 || rYCellCount == 0 || rHeight == 0)
                        {
                            rYDistance = rYCellCount = rHeight = 0;
                        }

                        if (lXDistance == 0 || lXCellCount == 0 || lWidth == 0)
                        {
                            lXDistance = lXCellCount = lWidth = 0;
                        }

                        if (rXDistance == 0 || rXCellCount == 0 || rWidth == 0)
                        {
                            rXDistance = rXCellCount = rWidth = 0;
                        }
                        #region 步进修正(方案1:隔一个pass重新修正一次步进;方案2:第一次修正,后面基于原始步进值动态调整)
                        worker.ReportProgress(0, string.Format("GetStepRevCrossStitch Y_StepL={0};bandCount={1}", Y_StepL, bandCount));
#if false
                        int step = CoreInterface.GetStepRevCrossStitch((Y_StepL + Y_StepR) / 2, ref pass);
#else
                        int step = CoreInterface.GetStepRevCrossStitch(Y_StepL, ref pass);
#endif
                        worker.ReportProgress(0, string.Format("GetStepRevCrossStitch return pass={0};step={1};Y_StepL={2};bandCount={2}", pass, step, Y_StepL, bandCount));


                        switch (bandCount)
                        {
//							case 0:
                        case 1:
                        {
                            if (bandCount == 0)
                            {
                                if (ss.nKillBiDirBanding != 0)
                                {
                                    this.CurStep = ss.sCalibrationSetting.nPassStepArray[pass / 2 - 1];
                                }
                                else
                                {
                                    this.CurStep = ss.sCalibrationSetting.nPassStepArray[pass - 1];
                                }
                            }
                            if (ss.nKillBiDirBanding != 0)
                            {
                                ss.sCalibrationSetting.nPassStepArray[pass / 2 - 1] = step;
                            }
                            else
                            {
                                ss.sCalibrationSetting.nPassStepArray[pass - 1] = step;
                            }
                            worker.ReportProgress(0, string.Format("y方向步进 newstep={0};CurStep={1};bandCount={2}", step, CurStep, bandCount));

                            CoreInterface.SetPrinterSetting(ref ss);
                            this.m_iPrinterChange.OnPrinterSettingChange(ss);
                            break;
                        }

//							case 2:
//							{
//								float y = up.YCoefficient;//1.0068027f;
//								if(ss.nKillBiDirBanding != 0)
//								{
//									ss.sCalibrationSetting.nPassStepArray[pass/2 -1] = CurStep;
//								}
//								else
//								{
//									ss.sCalibrationSetting.nPassStepArray[pass -1] = CurStep;
//								}
//								worker.ReportProgress(0,string.Format("y方向步进 newstep={0};CurStep={1};bandCount={2}",CurStep,CurStep,bandCount));
//								//								worker.ReportProgress(0,string.Format("y方向步进 缩放比例系数 y={0};delta={1};CurStep={2}",y,step - CurStep,CurStep));
//
//								CoreInterface.SetPrinterSetting(ref ss);
//								this.m_iPrinterChange.OnPrinterSettingChange(ss);
//								break;
//							}
                        default:
                        {
                            float y = up.YOffset;                                    //1.0068027f;
                            if (ss.nKillBiDirBanding != 0)
                            {
                                ss.sCalibrationSetting.nPassStepArray[pass / 2 - 1] = CurStep;
                            }
                            else
                            {
                                ss.sCalibrationSetting.nPassStepArray[pass - 1] = CurStep;
                            }
                            worker.ReportProgress(0, string.Format("y方向步进 newstep={0};CurStep={1};bandCount={2}", CurStep, CurStep, bandCount));
                            //								worker.ReportProgress(0,string.Format("y方向步进 缩放比例系数 y={0};delta={1};CurStep={2}",y,step - CurStep,CurStep));

                            CoreInterface.SetPrinterSetting(ref ss);
                            worker.ReportProgress(0, string.Format("GetStepRevCrossStitch Y_StepL={0};bandCount={1}", Y_StepL, bandCount));
#if false
                            int step = CoreInterface.GetStepRevCrossStitch((Y_StepL + Y_StepR) / 2, ref pass);
#else
                            step = CoreInterface.GetStepRevCrossStitch(Y_StepL, ref pass);
#endif
                            worker.ReportProgress(0, string.Format("GetStepRevCrossStitch return pass={0};step={1};Y_StepL={2};bandCount={3}", pass, step, Y_StepL, bandCount));

//								if(bandCount % 2 ==0)
                            {
                                if (ss.nKillBiDirBanding != 0)
                                {
                                    ss.sCalibrationSetting.nPassStepArray[pass / 2 - 1] = step;
                                }
                                else
                                {
                                    ss.sCalibrationSetting.nPassStepArray[pass - 1] = step;
                                }
                                worker.ReportProgress(0, string.Format("y方向步进 newstep={0};CurStep={1};bandCount={2}", step, CurStep, bandCount));

                                CoreInterface.SetPrinterSetting(ref ss);
                                this.m_iPrinterChange.OnPrinterSettingChange(ss);
                            }
                            break;
                        }
                            #region 注释掉的代码
                            //							default:
//							{
//								float y = up.YCoefficient;//1.0068027f;
//								if(ss.nKillBiDirBanding != 0)
//								{
//									ss.sCalibrationSetting.nPassStepArray[pass/2 -1] = CurStep;
//								}
//								else
//								{
//									ss.sCalibrationSetting.nPassStepArray[pass -1] = CurStep;
//								}
//								worker.ReportProgress(0,string.Format("y方向步进 newstep={0};CurStep={1};bandCount={2}",CurStep,CurStep,bandCount));
//								//								worker.ReportProgress(0,string.Format("y方向步进 缩放比例系数 y={0};delta={1};CurStep={2}",y,step - CurStep,CurStep));
//
//								CoreInterface.SetPrinterSetting(ref ss);
//								this.m_iPrinterChange.OnPrinterSettingChange(ss);
//								break;
//							}
//							default:
//							{
//																float y = up.YCoefficient;//1.0068027f;
//								#if false
//														int cellCountY = 0;
//														if(lYDistance!=0 &&lYCellCount!=0&&lHeight!=0)
//															cellCountY+=CellCountInBigCell;
//														if(rYDistance!=0 &&rYCellCount!=0&&rHeight!=0)
//															cellCountY+=CellCountInBigCell;
//
//														if(lYCellCount+rYCellCount!=0&&lHeight+rHeight!=0)
//														{
//															y =(lYDistance+rYDistance)*(lYCellCount+rYCellCount)/(lYCellCount+rYCellCount)/(lHeight+rHeight);
//														}
//														// 正的缩小,负的放大
//														step = CurStep + (int)(ss.sCalibrationSetting.nStepPerHead * (y-1f));
//
//														worker.ReportProgress(0,string.Format("y方向缩放比例系数 y={0};delta={1};",y,step - CurStep));
//
//														worker.ReportProgress(0,string.Format("GetStepRevCrossStitch return pass={0};step={1};",pass,step));
//														worker.ReportProgress(0,string.Format("old step:{0}; newstep={1};value:{2};basestep:{3}"
//															,CurStep,step,Y_StepL*2.54f/3.61f*ss.sCalibrationSetting.nStepPerHead,ss.sCalibrationSetting.nStepPerHead) );
//
//														if(Math.Abs(CurStep -step)!=Math.Abs(Y_StepL*2.54/3.61f*ss.sCalibrationSetting.nStepPerHead))
//															worker.ReportProgress(0,string.Format("step error!!!!!!{0}:{1}",Math.Abs(CurStep -step),Math.Abs(Y_StepL*2.54f/3.61f*ss.sCalibrationSetting.nStepPerHead)));
//														if(ss.nKillBiDirBanding != 0)
//															ss.sCalibrationSetting.nPassStepArray[pass/2 -1] = step;
//														else
//															ss.sCalibrationSetting.nPassStepArray[pass -1] = step;
//
//								#else
//																if(ss.nKillBiDirBanding != 0)
//																{
//								//									step = CurStep + step - ss.sCalibrationSetting.nPassStepArray[pass/2 -1];
//																	ss.sCalibrationSetting.nPassStepArray[pass/2 -1] = step;
//																}
//																else
//																{
//								//									step =  CurStep + step - ss.sCalibrationSetting.nPassStepArray[pass -1];
//																	ss.sCalibrationSetting.nPassStepArray[pass -1] = step;
//																}
//																worker.ReportProgress(0,string.Format("y方向缩放比例系数 y={0};delta={1};CurStep={2}",y,step - CurStep,CurStep));
//								#endif
//
//																CoreInterface.SetPrinterSetting(ref ss);
//																this.m_iPrinterChange.OnPrinterSettingChange(ss);
//								break;
//							}
                            #endregion
                        }
                        #endregion

                        #region x方向缩放修正(前2pass应用固定修正系数,之后根据采集值动态调整)
                        switch (bandCount)
                        {
                        case 0:
                        case 1:
//							{
//								ToleranceBand tBand_cur1 = new ToleranceBand();
//								tBand_cur1.LeftTol_X = this.tBand.LeftTol_X;
//								float x1 = up.XCoefficient;//0.998f;//1.0068027f;
//
//								// 正的缩小,负的放大
//								tBand_cur1.RightTol_X = (int)(this.m_iPrinterChange.GetPrintingJobInfo().sImageInfo.nImageWidth *(1f-x1));
//								worker.ReportProgress(0,string.Format("x方向缩放比例系数 x={0};delta={1};bandCount={2}",x1,tBand_cur1.RightTol_X,bandCount));
//								CoreInterface.SetToleranceBand(ref tBand_cur1);
//								break;
//							}
                        default:
                        {
                            worker.ReportProgress(0, string.Format("ss.sBaseSetting.eRev23={0};bandCount={1}", ss.sBaseSetting.eRev23, bandCount));

//								ToleranceBand tBand_cur = new ToleranceBand();
                            tBand_curX.LeftTol_X = this.tBand.LeftTol_X;
                            float x = up.XOffset;                                    //0.998f;//1.0068027f;
#if false
                            int cellCount = 0;
                            if (lXDistance != 0 && lXCellCount != 0 && lWidth != 0)
                            {
                                cellCount += CellCountInBigCell;
                            }
                            if (rXDistance != 0 && rXCellCount != 0 && rWidth != 0)
                            {
                                cellCount += CellCountInBigCell;
                            }

                            if (lXCellCount + rXCellCount != 0 && lWidth + rWidth != 0)
                            {
                                x = (lXDistance + rXDistance) * cellCount / (lXCellCount + rXCellCount) / (lWidth + rWidth);
                            }
#endif
                            // 正的缩小,负的放大
                            worker.ReportProgress(0, string.Format("left x={0};right x={1}", tBand.LeftTol_X, tBand.RightTol_X));
                            if (bandCount % 2 == 0)
                            {
                                tBand_curX.RightTol_X = (int)(-tBand.LeftTol_X + tBand.RightTol_X);
                            }
                            worker.ReportProgress(0, string.Format("x方向缩放比例系数 x={0};delta={1};bandCount={2}", x, tBand_curX.RightTol_X, bandCount));
                            CoreInterface.SetToleranceBand(ref tBand_curX);
                            break;
                        }
                        }
                        #endregion
                        bandCount++;
                        PreBand = tBand;
                        CoreInterface.Printer_PauseOrResume();
                        this.tbandMonitor.Reset();
//						if(mTcpipRespondTimer.Enabled)
//						{
//							mTcpipRespondTimer.Stop();
//						}
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                if (worker != null)
                {
                    worker.ReportProgress(0, ex.Message);
                }
            }
        }
Beispiel #5
0
        // 开始进行读取
        public void BeginRead(NetworkStream stream, byte[] buffer, int index)
        {
            try
            {
#if true
                AsyncCallback callBack = new AsyncCallback(OnReadComplete);
                stream.BeginRead(buffer, 0, BufferSize, callBack, stream);
#else
                // 从缓存buffer中读入到文件流中
                byte[] fileBuffer = new byte[BufferSize];                               // 每次传1KB
                int    bytesRead;
                int    totalBytes = 0;
                do
                {
                    bytesRead = stream.Read(buffer, 0, BufferSize);
                    Buffer.BlockCopy(buffer, 0, fileBuffer, totalBytes, bytesRead);
                    totalBytes += bytesRead;
                    Debug.WriteLine(string.Format("Receiving {0} bytes ...", totalBytes));

                    if (bytesRead != 0)
                    {
                        string msg = Encoding.Default.GetString(this.buffer, 0, bytesRead).ToLower();
                        //				string msg1 = Encoding.Default.GetString(this.buffer, 0, totalBytes).ToLower();
                        //				string msg2 = Encoding.ASCII.GetString(this.buffer, 0, totalBytes).ToLower();
                        Array.Clear(this.buffer, 0, this.buffer.Length);                         // 清空缓存,避免脏读

                        // 获取protocol数组
                        //ArrayList protocolArray = handler.GetProtocol(msg);
                        string []     protocolArray = handler.GetProtocol(msg);
                        ToleranceBand tolBand       = new ToleranceBand();
                        tolBand.LeftTol_X = (int)(Convert.ToSingle((string)protocolArray[2]) / 25.4f * 720.0f);

                        foreach (string pro in protocolArray)
                        {
                            // 这里异步调用,不然这里可能会比较耗时
                            //						CoreInterface.SetParasFromTcpIp(pro);
                            worker.ReportProgress(0, pro);
                        }
                        CoreInterface.SetToleranceBand(ref tolBand);
                        CoreInterface.Printer_PauseOrResume();
                    }
                    else
                    {
                        worker.ReportProgress(-1, ResString.GetResString("Shizixiu_UnConnected"));
                        return;
                        //throw new Exception("收到0字节");
                    }
                } while (bytesRead > 0);
#endif
            }
            catch (Exception ex)
            {
                if ((TcpClient)clients[index] != null)
                {
                    ((TcpClient)clients[index]).Close();
                }
                if (worker != null)
                {
                    worker.ReportProgress(0, ex.Message);
                }
                Debug.WriteLine(ex.Message);                            // 捕获异常时退出程序
                MessageBox.Show(ex.Message);
                this.Reset();
            }
        }