Ejemplo n.º 1
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="buff"></param>
        public void Send(byte[] buff)
        {
            string recv = DefaultEncoding.GetString(buff);

            LogRevMsg.LogText("发送", recv);
            Lib.LogManager.Logger.LogInfo("发送:" + "\r" + recv);
            this.comm.Write(buff, 0, buff.Length);
            //MachineAdapter.Common.Manager.MonitorManager.Instance.SendLog(recv);
        }
Ejemplo n.º 2
0
        public void Receive()
        {
            IPEndPoint    ipendpoint = client.Client.RemoteEndPoint as IPEndPoint;
            NetworkStream stream     = client.GetStream();

            //2.接收状态,长度<1024字节
            byte[] bytes = new Byte[1024 * 20];
            //string data = string.Empty;
            stream.Read(bytes, 0, bytes.Length);
            LogRevMsg.LogText("接收", Encoding.Default.GetString(bytes));
            if (DataReceived != null)
            {
                DataReceived(stream, bytes);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///回写
        /// </summary>
        /// <param name="result"></param>
        private void ReadCallback(IAsyncResult result)
        {
            int           read;
            NetworkStream networkStream;

            try
            {
                ResetTimeOutTimer();//重置超时次数
                networkStream = client.GetStream();
                if (networkStream == null)
                {
                    return;
                }

                read = networkStream.EndRead(result);
                if (read == 0)
                {
                    return;
                }
                byte[] buffer = result.AsyncState as byte[];
                byte[] Data   = new byte[read];
                Buffer.BlockCopy(buffer, 0, Data, 0, read);
                networkStream.BeginRead(buffer, 0, buffer.Length, ReadCallback, buffer);
                LogRevMsg.LogText("接收", Encoding.Default.GetString(Data));
                if (DataReceived != null)
                {
                    DataReceived(networkStream, Data);
                }
            }
            catch (Exception ex)
            {
                Lib.LogManager.Logger.LogException(ex);
            }
            finally
            {
            }
        }
Ejemplo n.º 4
0
        void comm_DataReceived_Kim(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                //log.Info("开始接收数据");
                byte[]             RS232DataBuff = GetData(comm);
                SerialHandleObject handle        = new SerialHandleObject();
                if (RS232Received != null)
                {
                    RS232Received(handle, RS232DataBuff);
                    if (handle.handled)
                    {
                        return;
                    }
                }
                //Encoding utf8 = Encoding.GetEncoding(65001);
                //Encoding gb2312 = Encoding.GetEncoding("gb2312");
                string recv = DefaultEncoding.GetString(RS232DataBuff);
                //LogRevMsg.LogText("单次接收", recv);
                //log.Info("单次接收" + "\r" + recv);

                if (!IsASTM)
                {
                    currStr += recv;

                    if (CheckEndStr(currStr, ETXStr))
                    {
                        foreach (byte b in RS232DataBuff)
                        {
                            handleComand(b);
                        }
                        LogRevMsg.LogText("接收", currStr);
                        Lib.LogManager.Logger.LogInfo("接收" + "\r" + currStr);
                        //MonitorManager.Instance.SendCommand(
                        //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        if (false)
                        {
                            LogRevMsg.GetFile(currStr);
                        }
                        else
                        {
                            LogRevMsg.GetBufferLog(currStr);
                        }
                        currStr = string.Empty;
                    }
                }
                else
                {
                    try
                    {
                        if (RS232DataBuff.Length == 2 && RS232DataBuff[0] == ASTMCommon.ACK_6 && RS232DataBuff[1] == ASTMCommon.EOT_4)
                        {
                            byte[] buff = new byte[1];
                            buff[0]       = RS232DataBuff[0];
                            RS232DataBuff = buff;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                    foreach (byte b in RS232DataBuff)
                    {
                        if (IsASTM)
                        {
                            HandleAstmCommand(b);
                        }
                        char RecChar = Convert.ToChar(b);
                        currStr += RecChar;
                        if (CheckEndStr(currStr, ETXStr))
                        {
                            LogRevMsg.LogText("接收", currStr);
                            Lib.LogManager.Logger.LogInfo("接收" + "\r" + currStr);
                            //MonitorManager.Instance.SendCommand(
                            //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                            LogRevMsg.GetBufferLog(currStr);
                            currStr = string.Empty;
                        }
                        //if (CheckEndStr(SingleStr, ETXStr) || SingleStr.Contains(ASTMCommon.cENQ_5.ToString()))
                        //{
                        //    LogRevMsg.LogText("接收", SingleStr);
                        //    log.Info("接收" + "\r" + SingleStr);
                        //    //MonitorManager.Instance.SendCommand(
                        //    //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        //    if (!SingleStr.Contains(ASTMCommon.cEOT_4.ToString()))
                        //    {
                        //        SingleStr = string.Empty;
                        //    }
                        //}
                        //if (SingleStr.Contains(ASTMCommon.cEOT_4.ToString()))
                        //{
                        //    if (ETXStr != ASTMCommon.cEOT_4.ToString())
                        //    {
                        //        LogRevMsg.LogText("接收", SingleStr);
                        //        log.Info("接收" + "\r" + SingleStr);
                        //    }
                        //    //MonitorManager.Instance.SendCommand(
                        //    //    RemoteCommand.CMD_LOG, new string[] { LocalConfig.Instance.ItrID, currStr }); //kim 发送至监控服务器
                        //    LogRevMsg.GetBufferLog(currStr);
                        //    currStr = string.Empty;
                        //    SingleStr = string.Empty;
                        //    break;
                        //}
                    }
                }
                if (IsNeedAck)
                {
                    if (RS232DataBuff.Length == 1)
                    {
                        if (RS232DataBuff[0] == ASTMCommon.NAK_21)
                        {
                            return;
                        }
                        if (RS232DataBuff[0] == ASTMCommon.ACK_6)
                        {
                            return;
                        }
                    }
                    SendACK();
                }
            }
            catch (Exception ex)
            {
            }
        }
Ejemplo n.º 5
0
        private void btnPhoto_Click(object sender, EventArgs e)
        {
            btnPhoto.Enabled = false;
            try
            {
                picName = GetImagePath() + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
                //picName = GetImagePath() + "\\" + "xiaosy.jpg";
                #region 拍照生成图片
                if (videoSourcePlayer1.IsRunning)
                {
                    BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                        videoSourcePlayer1.GetCurrentVideoFrame().GetHbitmap(),
                        IntPtr.Zero,
                        Int32Rect.Empty,
                        BitmapSizeOptions.FromEmptyOptions());
                    PngBitmapEncoder pE = new PngBitmapEncoder();
                    pE.Frames.Add(BitmapFrame.Create(bitmapSource));
                    using (Stream stream = File.Create(picName))
                    {
                        pE.Save(stream);
                    }
                    //拍照完成后关摄像头并刷新同时关窗体
                    if (videoSourcePlayer1 != null && videoSourcePlayer1.IsRunning)
                    {
                        videoSourcePlayer1.SignalToStop();
                        videoSourcePlayer1.WaitForStop();
                        videoSourcePlayer1.Visible = false;
                    }
                }
                #endregion

                #region 根据图片获取图片的文字
                AspriseOCR1.GetOCRpart(picName);
                string ORCString = AspriseOCR1.ORCResult;

                //string ORCString = GetVeryfyCode(picName);

                //Bitmap pic = new Bitmap(picName);
                //int width = pic.Size.Width; // 图片的宽度
                //int height = pic.Size.Height; // 图片的高度
                //string ORCString = Marshal.PtrToStringAnsi(OCRpart(picName, -1, 0, 0, width, height));
                string OCRNum = System.Text.RegularExpressions.Regex.Replace(ORCString, @"[^0-9]+", "");
                if (!string.IsNullOrEmpty(OCRNum))
                {
                    string Msg     = ASTMCommon.cENQ_5.ToString() + ASTMCommon.cSTX_2.ToString() + OCRNum + ASTMCommon.cETX_3.ToString() + ASTMCommon.cEOT_4.ToString();
                    byte[] SendMsg = Encoding.Default.GetBytes(Msg);
                    LogRevMsg.LogText("发送", Msg);
                    ClientPort.Send(SendMsg);
                    //ocr.AnswerList.Add(ASTMCommon.cSTX_2.ToString() + OCRNum+ASTMCommon.cETX_3.ToString());
                    //ocr.AnswerList.Add(ASTMCommon.cEOT_4.ToString());
                    //ocr.SendEnqCommand(100);
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show("摄像头异常:" + ex.Message);
            }
            btnPhoto.Enabled = true;
        }