//下面就要发送数据。将二进制字节数组发送到已打开的端口 public static Boolean CMNCT_Send(byte[] bArrayCommand) { try { if (CmnctSk != null) { CmnctSk.Send(bArrayCommand); } else if (CmnctSp != null) { CmnctSp.Write(bArrayCommand, 0, bArrayCommand.Length); } else if (CmnctUc != null) { CmnctUc.Write(bArrayCommand); } else { return(false); } } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); return(false); } return(true); }
//打开一个网口,成功则返回一个可用Socket,失败返回null。Port一般指定为9100 public static Socket CMNCT_OpenLan(String ipAddress, int Port) { Regex rx = new Regex(@"((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))))"); if (!rx.IsMatch(ipAddress)) { MessageBox.Show("非法IP。请注意格式形如xxx.xxx.xxx.xxx的字符串才是有效ip地址"); return(null); } IPAddress ipPrinter = IPAddress.Parse(ipAddress); IPEndPoint ipPrinterPoint = new IPEndPoint(ipPrinter, Port); if (CmnctSk == null) { CmnctSk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { CmnctSk.Connect(ipPrinterPoint); if (!CmnctSk.Connected) { throw new Exception("连接失败!"); } //载入Init资源 //TEXTANDPIC.TAC_Init(); } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); CMNCT_CloseLan(); } } return(CmnctSk); }
//异步发送 public static Boolean CMNCT_SendByNewThread(byte[] bArrayCommand) { try { System.Threading.Thread trWrite = new Thread(new ParameterizedThreadStart(CMNCT_TRStart)); trWrite.Start(bArrayCommand); return(true); } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); return(false); } }
//可以是绝对路径或相对路径 public static Boolean CMNCT_Send(String sBinaryFilePath) { try { if (CmnctSk != null) { CmnctSk.SendFile(sBinaryFilePath); } else if (CmnctSp != null) { System.IO.FileStream fileStream = new System.IO.FileStream(sBinaryFilePath, System.IO.FileMode.Open); byte[] dataToSend = new byte[fileStream.Length]; if (dataToSend.Length > Int32.MaxValue) { MessageBox.Show("数据超长"); fileStream.Close(); return(false); } fileStream.Read(dataToSend, 0, (int)fileStream.Length); CmnctSp.Write(dataToSend, 0, dataToSend.Length); fileStream.Close(); } else if (CmnctUc != null) { System.IO.FileStream fileStream = new System.IO.FileStream(sBinaryFilePath, System.IO.FileMode.Open); byte[] dataToSend = new byte[fileStream.Length]; if (dataToSend.Length > Int32.MaxValue) { MessageBox.Show("数据超长"); fileStream.Close(); return(false); } fileStream.Read(dataToSend, 0, (int)fileStream.Length); CmnctUc.Write(dataToSend); fileStream.Close(); } else { return(false); } } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); return(false); } return(true); }
//当前窗口上指定相对坐标,指定范围,转为点阵数据。 public static byte[][] TAC_TurnAreaToPixData(Form fmImaNoScreen, int nPositionX, int nPositionY, int nAreaWidth, int nAreaHeight) { byte[][] tempDesData = new byte[nAreaHeight][]; for (int i = 0; i < nAreaHeight; i++) { tempDesData[i] = new byte[nAreaWidth]; } try { Bitmap bmpForm = new Bitmap(fmImaNoScreen.Width, fmImaNoScreen.Height); fmImaNoScreen.DrawToBitmap(bmpForm, new Rectangle(0, 0, bmpForm.Width, bmpForm.Height)); Bitmap bmpDes = new Bitmap(nAreaWidth, nAreaHeight); Graphics grpForm = Graphics.FromImage(bmpDes); int Border = (fmImaNoScreen.Width - fmImaNoScreen.ClientSize.Width) / 2; Rectangle destRect = new Rectangle(nPositionX + Border, nPositionY + fmImaNoScreen.Height - fmImaNoScreen.ClientSize.Height - Border, nAreaWidth, nAreaHeight); Rectangle srcRect = new Rectangle(0, 0, nAreaWidth, nAreaHeight); grpForm.DrawImage(bmpForm, srcRect, destRect, GraphicsUnit.Pixel); System.Drawing.Imaging.BitmapData tempBitmapData = bmpDes.LockBits(srcRect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); fmImaNoScreen.Enabled = false; unsafe { byte *tempPixelData = (byte *)tempBitmapData.Scan0.ToPointer(); int jump = tempBitmapData.Stride - nAreaWidth * 3; for (int i = 0; i < nAreaHeight; i++) { for (int j = 0; j < nAreaWidth; j++) { tempDesData[i][j] = TAC_IsBlack(*tempPixelData ^ 0xff, *(tempPixelData + 1) ^ 0xff, *(tempPixelData + 2) ^ 0xff, 1); tempPixelData += 3; } tempPixelData += jump; } } fmImaNoScreen.Enabled = true; } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); return(null); } return(tempDesData); }
//打开一个串口 public static SerialPort CMNCT_OpenCom(String sComName, int nBaudrate, int nDataBits, float nStopBits, int nParity) { try { StopBits[] intToStopBits = { StopBits.One, StopBits.OnePointFive, StopBits.Two }; Parity[] intToParity = { Parity.None, Parity.Odd, Parity.Even }; CmnctSp = new SerialPort(sComName, nBaudrate, intToParity[nParity], nDataBits, intToStopBits[(int)(nStopBits * 2 - 2)]); CmnctSp.Open(); //载入Init资源 //TEXTANDPIC.TAC_Init(); } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); CMNCT_CloseCom(); } return(CmnctSp); }
//将图片转成点阵数据 public static byte[][] TAC_TurnPicToPixData(String sFilePath) { try { Bitmap bmp = new Bitmap(sFilePath); int widthPix = bmp.Width; int heightPix = bmp.Height; Rectangle rct = new Rectangle(0, 0, widthPix, heightPix); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rct, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); //先将像素复制出来 byte[][] bmpBitData = new byte[heightPix][]; unsafe { byte * pBmpData = (byte *)bmpData.Scan0; double gray = 0; for (int i = 0; i < heightPix; i++) { bmpBitData[i] = new byte[widthPix]; for (int j = 0; j < widthPix; j++) { gray = *(pBmpData + 1) * 0.3 + *(pBmpData + 2) * 0.59 + *(pBmpData + 3) * 0.11; if (gray < 128) { bmpBitData[i][j] = 0xfe; } else { bmpBitData[i][j] = 0x00; } pBmpData += 4; } pBmpData += bmpData.Stride - bmpData.Width * 4; } } return(bmpBitData); } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); return(null); } }
/* 将位图转换为256色光栅位图流数据 */ /* byte[height][width] */ public static byte[][] POS_BitmapToStream(Bitmap orgBitmap) { try { int widthPix = orgBitmap.Width; int heightPix = orgBitmap.Height; Rectangle rct = new Rectangle(0, 0, widthPix, heightPix); System.Drawing.Imaging.BitmapData bmpData = orgBitmap.LockBits(rct, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); //先将像素复制出来 byte[][] bmpBitData = new byte[heightPix][]; unsafe { byte * pBmpData = (byte *)bmpData.Scan0; double gray = 0; for (int i = 0; i < heightPix; i++) { bmpBitData[i] = new byte[widthPix]; for (int j = 0; j < widthPix; j++) { gray = *(pBmpData + 1) * 0.3 + *(pBmpData + 2) * 0.59 + *(pBmpData + 3) * 0.11; bmpBitData[i][j] = (byte)gray; pBmpData += 4; } pBmpData += bmpData.Stride - bmpData.Width * 4; } } orgBitmap.UnlockBits(bmpData); return(bmpBitData); } catch (Exception Mistake) { lasterror = Mistake.ToString(); return(null); } }
private static int index = 1; //线程序号 private static void CMNCT_TRStart(Object obArrayCommand) { int nWatashiNoIndex = index++; //只要start了,index就要+1 boIsComplete[0] = true; //如果是第一个线程,那么就将添加0号线程为已完成 try { while (true) { if (boIsComplete[nWatashiNoIndex - 1]) { byte[] bArrayCommand = (byte[])obArrayCommand; if (CmnctSk != null) { CmnctSk.Send(bArrayCommand); boIsComplete[nWatashiNoIndex] = true; return; } else if (CmnctSp != null) { CmnctSp.Write(bArrayCommand, 0, bArrayCommand.Length); boIsComplete[nWatashiNoIndex] = true; return; } else { throw (new Exception("未将对象设置引用到对象实例?")); } } } } catch (Exception Mistake) { MessageBox.Show(Mistake.ToString()); boIsComplete[nWatashiNoIndex] = true; } }