用心做好每一件事情!

生产制造追溯系统-在线打印

前言

很久没有写博客了(大概有4个月的样子了吧),从2015年8月份开始一直忙于公司的系统,直到2016年6月底全部上线;包含4个厂区,每个厂区都是上千人的规模,而负责搞这个项目的算上我只有2个人,说多了都是泪:

美工?没有

测试人员?没有

DBA?没有

架构师?没有

运维?继续没有

估计大家都没遇见过这样的工作吧?哈哈.

历经艰难、跟各个部门(IE、PE、生产、PMC、QA等)唇枪舌战、好在在6月底总算是全部上线,总算是一点欣慰,毕竟决定了接手这个项目,那就要用心去做,只有用心了才能做好。

吐槽完毕,下面开始说正事儿

********我是华丽的分割线************************************************

公司的系统上线之后,我也稍微可以缓口气了,加班稍微少了一点,一般到了下午6点半就能下班,所以我还是决定将2015年初整理的这套项目拿出来继续优化,该项目已Web模式为主、客户端模式为辅,互相结合使用;目前主要包含以下几个主要功能模块:

本文主要说一下打印的问题,在生产制造业中条码打印是非常频繁的,也是必不可少的;我曾经亲身经历过这么一件事情:生产线在进行包装的时候,打印了两张卡通标签,但是操作员在将卡通标签贴在盒子上的时候贴反了,也就是说标签上面的序列号与盒子里面装的实物完全对不上,为此在海关被拦截了,当时厂里派了QA、生产、货仓与IT一同去海关处解决这个问题,我刚好在其中,整个过程是非常繁琐的,为此公司高层也要求必须杜绝这种品质事件,故我们也是采用了"在线打印"的方式进行包装,并且只有QA才有标签重打的权限。

这个故事反应了生产线的真实现象,所以我这边采用如下方式完成打印:

     function PrintLabel(box) {
            var api = '<%=MTS.Utility.MtsTool.GetApi() %>';
            
            var lurl = api + "?type=3&action=get_carton_sn&carton_sn=" + box;
            $.ajax({ url: lurl,
                cache: false,
                dataType: "text",
                success: function (data) {
                    if (data == null || data == undefined) {
                        alert("");
                        return;
                    }
                    var arr = data.split("|");
                    if (arr[0] == "0") {
                        alert(arr[2]);
                        return;
                    }
                    var t = eval("(" + arr[2] + ")"); //                                                              
                    try {
                        var labelId = $("#hid_LabelId").val();
                        window.external.PrintLabel("", t.key, t.value, ",", labelId);

                    } catch (e) {

                    };
                }
            });
        }

以上代码是Web应用程序中的脚本,主要是通过API获取需要打印的数据,这里返回的是text类型,其实也可以返回Json格式的数据;用户完成包装之后系统会按照包装规则产生一个唯一的卡通箱号,那么这个箱号就作为API的参数 carton_sn= box传进去,根据该箱号返回真实的包装数据;然后通过window.external调用客户端的打印函数。

通过如下代码获取本地默认打印机:

 

  //获取默认打印机
            System.Drawing.Printing.PrintDocument pringdocument = new System.Drawing.Printing.PrintDocument();
            string pring_name = pringdocument.PrinterSettings.PrinterName;//打印机名

 

因为我这里的客户端程序就是对Web程式加壳了,通过这个客户端程序就可以方便的获取本地默认打印机,采用这种方式比在网页中安装 activex 控件要爽的多,谁用谁知道.

网页传过来的参数以键值对为标准:

  string[] keys = key.Split(splitKey.ToCharArray(), StringSplitOptions.None);
  string[] values = value.Split(splitKey.ToCharArray(), StringSplitOptions.None);

我这里调用BarTender进行打印,代码如下:

 format = (BarTender.FormatClass)engine.Formats.Open(filename);
 format.SetNamedSubStringValue(key, value);
 format.PrintSetup.Printer = printerName;
 BarTender.Messages msg = null;
 format.Print("0", false, 1, out msg);

 以下代码是Code 128格式的条码:

  1  public class Code128
  2     {
  3         private DataTable m_Code128 = new DataTable();
  4         private uint m_Height = 40;
  5         /// <summary>
  6         /// 高度
  7         /// </summary>
  8         public uint Height { get { return m_Height; } set { m_Height = value; } }
  9         private Font m_ValueFont = null;
 10         /// <summary>
 11         /// 是否显示可见号码  如果为NULL不显示号码
 12         /// </summary>
 13         public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } }
 14         private byte m_Magnify = 0;
 15         /// <summary>
 16         /// 放大倍数
 17         /// </summary>
 18         public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
 19         /// <summary>
 20         /// 条码类别
 21         /// </summary>
 22         public enum Encode
 23         {
 24             Code128A,
 25             Code128B,
 26             Code128C,
 27             EAN128
 28         }
 29         public Code128()
 30         {
 31             m_Code128.Columns.Add("ID");
 32             m_Code128.Columns.Add("Code128A");
 33             m_Code128.Columns.Add("Code128B");
 34             m_Code128.Columns.Add("Code128C");
 35             m_Code128.Columns.Add("BandCode");
 36             m_Code128.CaseSensitive = true;
 37             #region 数据表
 38             m_Code128.Rows.Add("0", " ", " ", "00", "212222");
 39             m_Code128.Rows.Add("1", "!", "!", "01", "222122");
 40             m_Code128.Rows.Add("2", "\"", "\"", "02", "222221");
 41             m_Code128.Rows.Add("3", "#", "#", "03", "121223");
 42             m_Code128.Rows.Add("4", "$", "$", "04", "121322");
 43             m_Code128.Rows.Add("5", "%", "%", "05", "131222");
 44             m_Code128.Rows.Add("6", "&", "&", "06", "122213");
 45             m_Code128.Rows.Add("7", "'", "'", "07", "122312");
 46             m_Code128.Rows.Add("8", "(", "(", "08", "132212");
 47             m_Code128.Rows.Add("9", ")", ")", "09", "221213");
 48             m_Code128.Rows.Add("10", "*", "*", "10", "221312");
 49             m_Code128.Rows.Add("11", "+", "+", "11", "231212");
 50             m_Code128.Rows.Add("12", ",", ",", "12", "112232");
 51             m_Code128.Rows.Add("13", "-", "-", "13", "122132");
 52             m_Code128.Rows.Add("14", ".", ".", "14", "122231");
 53             m_Code128.Rows.Add("15", "/", "/", "15", "113222");
 54             m_Code128.Rows.Add("16", "0", "0", "16", "123122");
 55             m_Code128.Rows.Add("17", "1", "1", "17", "123221");
 56             m_Code128.Rows.Add("18", "2", "2", "18", "223211");
 57             m_Code128.Rows.Add("19", "3", "3", "19", "221132");
 58             m_Code128.Rows.Add("20", "4", "4", "20", "221231");
 59             m_Code128.Rows.Add("21", "5", "5", "21", "213212");
 60             m_Code128.Rows.Add("22", "6", "6", "22", "223112");
 61             m_Code128.Rows.Add("23", "7", "7", "23", "312131");
 62             m_Code128.Rows.Add("24", "8", "8", "24", "311222");
 63             m_Code128.Rows.Add("25", "9", "9", "25", "321122");
 64             m_Code128.Rows.Add("26", ":", ":", "26", "321221");
 65             m_Code128.Rows.Add("27", ";", ";", "27", "312212");
 66             m_Code128.Rows.Add("28", "<", "<", "28", "322112");
 67             m_Code128.Rows.Add("29", "=", "=", "29", "322211");
 68             m_Code128.Rows.Add("30", ">", ">", "30", "212123");
 69             m_Code128.Rows.Add("31", "?", "?", "31", "212321");
 70             m_Code128.Rows.Add("32", "@", "@", "32", "232121");
 71             m_Code128.Rows.Add("33", "A", "A", "33", "111323");
 72             m_Code128.Rows.Add("34", "B", "B", "34", "131123");
 73             m_Code128.Rows.Add("35", "C", "C", "35", "131321");
 74             m_Code128.Rows.Add("36", "D", "D", "36", "112313");
 75             m_Code128.Rows.Add("37", "E", "E", "37", "132113");
 76             m_Code128.Rows.Add("38", "F", "F", "38", "132311");
 77             m_Code128.Rows.Add("39", "G", "G", "39", "211313");
 78             m_Code128.Rows.Add("40", "H", "H", "40", "231113");
 79             m_Code128.Rows.Add("41", "I", "I", "41", "231311");
 80             m_Code128.Rows.Add("42", "J", "J", "42", "112133");
 81             m_Code128.Rows.Add("43", "K", "K", "43", "112331");
 82             m_Code128.Rows.Add("44", "L", "L", "44", "132131");
 83             m_Code128.Rows.Add("45", "M", "M", "45", "113123");
 84             m_Code128.Rows.Add("46", "N", "N", "46", "113321");
 85             m_Code128.Rows.Add("47", "O", "O", "47", "133121");
 86             m_Code128.Rows.Add("48", "P", "P", "48", "313121");
 87             m_Code128.Rows.Add("49", "Q", "Q", "49", "211331");
 88             m_Code128.Rows.Add("50", "R", "R", "50", "231131");
 89             m_Code128.Rows.Add("51", "S", "S", "51", "213113");
 90             m_Code128.Rows.Add("52", "T", "T", "52", "213311");
 91             m_Code128.Rows.Add("53", "U", "U", "53", "213131");
 92             m_Code128.Rows.Add("54", "V", "V", "54", "311123");
 93             m_Code128.Rows.Add("55", "W", "W", "55", "311321");
 94             m_Code128.Rows.Add("56", "X", "X", "56", "331121");
 95             m_Code128.Rows.Add("57", "Y", "Y", "57", "312113");
 96             m_Code128.Rows.Add("58", "Z", "Z", "58", "312311");
 97             m_Code128.Rows.Add("59", "[", "[", "59", "332111");
 98             m_Code128.Rows.Add("60", "\\", "\\", "60", "314111");
 99             m_Code128.Rows.Add("61", "]", "]", "61", "221411");
100             m_Code128.Rows.Add("62", "^", "^", "62", "431111");
101             m_Code128.Rows.Add("63", "_", "_", "63", "111224");
102             m_Code128.Rows.Add("64", "NUL", "`", "64", "111422");
103             m_Code128.Rows.Add("65", "SOH", "a", "65", "121124");
104             m_Code128.Rows.Add("66", "STX", "b", "66", "121421");
105             m_Code128.Rows.Add("67", "ETX", "c", "67", "141122");
106             m_Code128.Rows.Add("68", "EOT", "d", "68", "141221");
107             m_Code128.Rows.Add("69", "ENQ", "e", "69", "112214");
108             m_Code128.Rows.Add("70", "ACK", "f", "70", "112412");
109             m_Code128.Rows.Add("71", "BEL", "g", "71", "122114");
110             m_Code128.Rows.Add("72", "BS", "h", "72", "122411");
111             m_Code128.Rows.Add("73", "HT", "i", "73", "142112");
112             m_Code128.Rows.Add("74", "LF", "j", "74", "142211");
113             m_Code128.Rows.Add("75", "VT", "k", "75", "241211");
114             m_Code128.Rows.Add("76", "FF", "I", "76", "221114");
115             m_Code128.Rows.Add("77", "CR", "m", "77", "413111");
116             m_Code128.Rows.Add("78", "SO", "n", "78", "241112");
117             m_Code128.Rows.Add("79", "SI", "o", "79", "134111");
118             m_Code128.Rows.Add("80", "DLE", "p", "80", "111242");
119             m_Code128.Rows.Add("81", "DC1", "q", "81", "121142");
120             m_Code128.Rows.Add("82", "DC2", "r", "82", "121241");
121             m_Code128.Rows.Add("83", "DC3", "s", "83", "114212");
122             m_Code128.Rows.Add("84", "DC4", "t", "84", "124112");
123             m_Code128.Rows.Add("85", "NAK", "u", "85", "124211");
124             m_Code128.Rows.Add("86", "SYN", "v", "86", "411212");
125             m_Code128.Rows.Add("87", "ETB", "w", "87", "421112");
126             m_Code128.Rows.Add("88", "CAN", "x", "88", "421211");
127             m_Code128.Rows.Add("89", "EM", "y", "89", "212141");
128             m_Code128.Rows.Add("90", "SUB", "z", "90", "214121");
129             m_Code128.Rows.Add("91", "ESC", "{", "91", "412121");
130             m_Code128.Rows.Add("92", "FS", "|", "92", "111143");
131             m_Code128.Rows.Add("93", "GS", "}", "93", "111341");
132             m_Code128.Rows.Add("94", "RS", "~", "94", "131141");
133             m_Code128.Rows.Add("95", "US", "DEL", "95", "114113");
134             m_Code128.Rows.Add("96", "FNC3", "FNC3", "96", "114311");
135             m_Code128.Rows.Add("97", "FNC2", "FNC2", "97", "411113");
136             m_Code128.Rows.Add("98", "SHIFT", "SHIFT", "98", "411311");
137             m_Code128.Rows.Add("99", "CODEC", "CODEC", "99", "113141");
138             m_Code128.Rows.Add("100", "CODEB", "FNC4", "CODEB", "114131");
139             m_Code128.Rows.Add("101", "FNC4", "CODEA", "CODEA", "311141");
140             m_Code128.Rows.Add("102", "FNC1", "FNC1", "FNC1", "411131");
141             m_Code128.Rows.Add("103", "StartA", "StartA", "StartA", "211412");
142             m_Code128.Rows.Add("104", "StartB", "StartB", "StartB", "211214");
143             m_Code128.Rows.Add("105", "StartC", "StartC", "StartC", "211232");
144             m_Code128.Rows.Add("106", "Stop", "Stop", "Stop", "2331112");
145             #endregion
146         }
147         /// <summary>
148         /// 获取128图形
149         /// </summary>
150         /// <param name="p_Text">文字</param>
151         /// <param name="p_Code">编码</param>      
152         /// <returns>图形</returns>
153         public Bitmap GetCodeImage(string p_Text, Encode p_Code)
154         {
155             string _ViewText = p_Text;
156             string _Text = "";
157             IList<int> _TextNumb = new List<int>();
158             int _Examine = 0;  //首位
159             switch (p_Code)
160             {
161                 case Encode.Code128C:
162                     _Examine = 105;
163                     if (!((p_Text.Length & 1) == 0)) throw new Exception("128C长度必须是偶数");
164                     while (p_Text.Length != 0)
165                     {
166                         int _Temp = 0;
167                         try
168                         {
169                             int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2));
170                         }
171                         catch
172                         {
173                             throw new Exception("128C必须是数字!");
174                         }
175                         _Text += GetValue(p_Code, p_Text.Substring(0, 2), ref _Temp);
176                         _TextNumb.Add(_Temp);
177                         p_Text = p_Text.Remove(0, 2);
178                     }
179                     break;
180                 case Encode.EAN128:
181                     _Examine = 105;
182                     if (!((p_Text.Length & 1) == 0)) throw new Exception("EAN128长度必须是偶数");
183                     _TextNumb.Add(102);
184                     _Text += "411131";
185                     while (p_Text.Length != 0)
186                     {
187                         int _Temp = 0;
188                         try
189                         {
190                             int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2));
191                         }
192                         catch
193                         {
194                             throw new Exception("128C必须是数字!");
195                         }
196                         _Text += GetValue(Encode.Code128C, p_Text.Substring(0, 2), ref _Temp);
197                         _TextNumb.Add(_Temp);
198                         p_Text = p_Text.Remove(0, 2);
199                     }
200                     break;
201                 default:
202                     if (p_Code == Encode.Code128A)
203                     {
204                         _Examine = 103;
205                     }
206                     else
207                     {
208                         _Examine = 104;
209                     }
210 
211                     while (p_Text.Length != 0)
212                     {
213                         int _Temp = 0;
214                         string _ValueCode = GetValue(p_Code, p_Text.Substring(0, 1), ref _Temp);
215                         if (_ValueCode.Length == 0) throw new Exception("无效的字符集!" + p_Text.Substring(0, 1).ToString());
216                         _Text += _ValueCode;
217                         _TextNumb.Add(_Temp);
218                         p_Text = p_Text.Remove(0, 1);
219                     }
220                     break;
221             }
222             if (_TextNumb.Count == 0) throw new Exception("错误的编码,无数据");
223             _Text = _Text.Insert(0, GetValue(_Examine)); //获取开始位
224 
225             for (int i = 0; i != _TextNumb.Count; i++)
226             {
227                 _Examine += _TextNumb[i] * (i + 1);
228             }
229             _Examine = _Examine % 103;           //获得严效位
230             _Text += GetValue(_Examine);  //获取严效位
231             _Text += "2331112"; //结束位
232             Bitmap _CodeImage = GetImage(_Text);
233             GetViewText(_CodeImage, _ViewText);
234             return _CodeImage;
235         }
236         /// <summary>
237         /// 获取目标对应的数据
238         /// </summary>
239         /// <param name="p_Code">编码</param>
240         /// <param name="p_Value">数值 A b  30</param>
241         /// <param name="p_SetID">返回编号</param>
242         /// <returns>编码</returns>
243         private string GetValue(Encode p_Code, string p_Value, ref int p_SetID)
244         {
245             if (m_Code128 == null) return "";
246             DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'");
247             if (_Row.Length != 1) throw new Exception("错误的编码" + p_Value.ToString());
248             p_SetID = Int32.Parse(_Row[0]["ID"].ToString());
249             return _Row[0]["BandCode"].ToString();
250         }
251         /// <summary>
252         /// 根据编号获得条纹
253         /// </summary>
254         /// <param name="p_CodeId"></param>
255         /// <returns></returns>
256         private string GetValue(int p_CodeId)
257         {
258             DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'");
259             if (_Row.Length != 1) throw new Exception("验效位的编码错误" + p_CodeId.ToString());
260             return _Row[0]["BandCode"].ToString();
261         }
262         /// <summary>
263         /// 获得条码图形
264         /// </summary>
265         /// <param name="p_Text">文字</param>
266         /// <returns>图形</returns>
267         private Bitmap GetImage(string p_Text)
268         {
269             char[] _Value = p_Text.ToCharArray();
270             int _Width = 0;
271             for (int i = 0; i != _Value.Length; i++)
272             {
273                 _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);
274             }
275             Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height);
276             Graphics _Garphics = Graphics.FromImage(_CodeImage);
277             //Pen _Pen;
278             int _LenEx = 0;
279             for (int i = 0; i != _Value.Length; i++)
280             {
281                 int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1);  //获取宽和放大系数
282                 if (!((i & 1) == 0))
283                 {
284                     //_Pen = new Pen(Brushes.White, _ValueNumb);
285                     _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
286                 }
287                 else
288                 {
289                     //_Pen = new Pen(Brushes.Black, _ValueNumb);
290                     _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height));
291                 }
292                 //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
293                 _LenEx += _ValueNumb;
294             }
295             _Garphics.Dispose();
296             return _CodeImage;
297         }
298         /// <summary>
299         /// 显示可见条码文字 如果小于40 不显示文字
300         /// </summary>
301         /// <param name="p_Bitmap">图形</param>           
302         private void GetViewText(Bitmap p_Bitmap, string p_ViewText)
303         {
304             if (m_ValueFont == null) return;
305 
306             Graphics _Graphics = Graphics.FromImage(p_Bitmap);
307             SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont);
308             if (_DrawSize.Height > p_Bitmap.Height - 10 || _DrawSize.Width > p_Bitmap.Width)
309             {
310                 _Graphics.Dispose();
311                 return;
312             }
313 
314             int _StarY = p_Bitmap.Height - (int)_DrawSize.Height;
315             _Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarY, p_Bitmap.Width, (int)_DrawSize.Height));
316             _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, 0, _StarY);
317         }
318 
319         //12345678
320         //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
321         //结果为starc +12 +34 +56 +78 +47 +end
322 
323         internal Image GetCodeImage(string p)
324         {
325             throw new NotImplementedException();
326         }
327     }
View Code

这样一来,操作员手上没有多的条码,必须包装完成之后系统才会一对一的打印一份条码出来,完成一个产品的包装就贴一个条码,很大程度上面避免了条码混乱的问题.

已完成的部分功能

 

#1工单维护:这个一般都是由PMC完成的,PMC根据排期计划合理创建工单,如果企业上了SAP系统,也可以直接链接到SAP系统进行下载工单资料,这样就更方便了.

#2工单优先级:PMC在创建工单的时候会指定该信息,生产过程中系统会体现该信息,起到提示用户的目的,管理者可根据实际情况随时变更该信息。

#3工艺路线维护:工艺路线由 IE 完成,生产部根据 IE 制定的工艺路线进行生产,系统会检测每一个工序的通过情况,比如上一个工序没有做则不可以直接跳到下一个工序。

#4目检过站:操作扫描条码过站,必须按照 IE 制定的工艺路线进行,如果扫描的条码不在当前工序,则系统会提示当前条码的正确位置。

#5目检过站:系统会将不良品强制打入维修中心,在完成修理之前无法进行其它的操作。

 #6组装动态装配:系统支持动态配置装配规则,不同的工单采用不同的规则进行装配,每一个装配条码可独立配置条码规则,比如长度、前缀等信息,防止用户输入错误。

 #7FQC送检:系统采用 AQL 标准动态抽检,打破传统的抽检模式,由系统自动计算需要抽检的产品,同时也由系统自动根据 AQL 标准进行结果判定,有效帮助品质人员进行品质监控与管理。

 #8FQC抽检:生产方面将产品以批次单位送检至QC,系统提示QC需要抽检的产品序列号,QC针对需要抽检的产品检测并录入抽检结果,系统根据抽检情况按照 AQL 自动判定.

 

 

  #9包装规则:针对每个工单配置相应的包装规则,比如卡通箱容量、箱号长度、箱号前缀等信息,并上传卡通标签模板。

 

#10包装:包装规则配置完成之后,即可扫描条码进行包装了。

 

结束包装的时候,系统自动将标签打印出来.

 

 

#11不良品维修:生产过程中的不良都会被系统强制打入维修中心,必须经过修理之后才能进行其它工序。

#12不良预警:系统会自动监控指定生产线的不良情况,当不良情况达到了红色预警值,则触发警报,系统自动锁定当前生产线,由管理者分析不良原因并改善之后进行解除预警。

 

#13成品发货:成品发货过程中支持上传实物图片。

 #14品质异常报告:用户发起品质异常,由工程部分析原因并给出改善,由QA确认是否可行。

 #15部分报表:

 #16电子看板:

 

结尾

 

 

 

 

因为工作日需要上班,白天必须做公司的事情,所以只有每天晚上熬夜和周末来做这个项目,说真的还是有点累,如果您觉得文章过得去,还请多多支持,谢谢各位园友!!

如果您有兴趣或者更好的建议,可加楼主QQ:96966 1314交流

 

posted @ 2016-12-19 00:25  无 影  阅读(6227)  评论(24编辑  收藏  举报
作者: Allen【QQ:96966 1314】 网名:无影 出处: http://www.cnblogs.com/allen0118/ 声明: 本文版权归作者和博客园共有!转载时必须保留此段声明,且在文章页面明显位置给出原文连接。