internal void PrintNew() { //1.创建Socket对象 Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse("xxx.xxx.x.xxx"); //2.绑定IP和端口 IPEndPoint endpoint = new IPEndPoint(ip, int.Parse("6000")); try { //这里客户端套接字连接到网络节点(服务端)用的方法是Connect 而不是Bind clientSocket.Connect(endpoint); //MessageBox.Show("ip连接成功..."); } catch { MessageBox.Show("与系统连接失败,请检查计算机以及系统(新)!"); } int[] selectRows = gridView1.GetSelectedRows();//获取当前列表行数 if (selectRows != null && selectRows.Length > 0) { StringBuilder builder = new StringBuilder(); foreach (var row in selectRows) { if (this.gridView1.GetRowCellValue(row, "MachineNumber") != null)//机器代码 { string Sampleno = ""; string sampleno = this.gridView1.GetRowCellValue(row, "SampleNO").ToString();//蜡块号 string[] samplenolist = sampleno.Split(','); for (int i = 0; i < samplenolist.Length; i++) { if (i == 0) { Sampleno += samplenolist[i].ToString(); } else { string[] samplenolistlist = samplenolist[i].ToString().Split('_'); for (int j = 0; j < samplenolistlist.Length; j++) { if (j == samplenolistlist.Length - 1) { Sampleno += "_" + samplenolistlist[j].ToString(); } } } } string rocheCode = this.gridView1.GetRowCellValue(row, "MachineNumber").ToString();//机器代码 if (!string.IsNullOrEmpty(rocheCode)) { builder.Clear(); builder.Append(@"MSH|^~\&|PIS|Lab||BenchMark|"); builder.Append(DateTime.Now.ToString("yyyyMMddHHmmss")); builder.Append("||OML^O21|"); builder.Append(DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString()); builder.Append("|P|2.4|"); builder.Append('\r'); builder.Append("ORC|NW|"); builder.Append(Sampleno); builder.Append("|"); builder.Append('\r'); builder.Append("OBR|1|"); builder.Append(this.gridView1.GetRowCellValue(row, "CaseNO").ToString()); builder.Append("||"); builder.Append(rocheCode + "^" + this.gridView1.GetRowCellValue(row, "MarkerName").ToString()); builder.Append("^STAIN|||||||||||||||||||04926|||||"); builder.Append('\r'); byte[] sendMsg = Encoding.UTF8.GetBytes(builder.ToString()); byte[] bs = new byte[sendMsg.Length + 3]; bs[0] = 0x0b; for (int i = 0; i < sendMsg.Length; i++) { bs[i + 1] = sendMsg[i]; } bs[bs.Length - 2] = 0x1c; bs[bs.Length - 1] = 0x0d; clientSocket.Send(bs); Thread.Sleep(100); } } else { MessageBox.Show("无机器码,请联系管理员!", "标签打印", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } } else { MessageBox.Show("请选择您要打印记录!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } //oGetArgThread.Abort(); clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); }