生產製造追溯系統-條碼列印

chenpeng0118發表於2016-12-19

前言

很久沒有寫部落格了(大概有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交流

 

相關文章