生產製造追溯系統-在線打印

前言ajax

好久沒有寫博客了(大概有4個月的樣子了吧),從2015年8月份開始一直忙於公司的系統,直到2016年6月底所有上線;包含4個廠區,每一個廠區都是上千人的規模,而負責搞這個項目的算上我只有2我的,說多了都是淚:api

美工?沒有架構

測試人員?沒有運維

DBA?沒有ide

架構師?沒有函數

運維?繼續沒有測試

估計你們都沒碰見過這樣的工做吧?哈哈.優化

歷經艱難、跟各個部門(IE、PE、生產、PMC、QA等)脣槍舌戰、好在在6月底總算是所有上線,總算是一點欣慰,畢竟決定了接手這個項目,那就要用心去作,只有用心了才能作好。ui

吐槽完畢,下面開始說正事兒編碼

********我是華麗的分割線************************************************

公司的系統上線以後,我也稍微能夠緩口氣了,加班稍微少了一點,通常到了下午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交流

相關文章
相關標籤/搜索