예제 #1
0
        public void PhotoelectricOpen()
        {
            var port = SerialPortHelp.GetDevicePort(DeviceType.Photoelectric, DetectionType.Light);

            port.DataReceived += (s, e) =>
            {
                var    serialPort = s as SerialPort;
                byte[] bytesData  = new byte[0];
                byte[] bytesTemp  = new byte[0];
                int    bytesRead;
                byte   result = 0x00;

                //获取接收缓冲区中字节数
                bytesRead = serialPort.BytesToRead;
                //保存上一次没处理完的数据
                if (bytesData.Length > 0)
                {
                    bytesTemp = new byte[bytesData.Length];
                    bytesData.CopyTo(bytesTemp, 0);
                    bytesData = new byte[bytesRead + bytesData.Length];
                    bytesTemp.CopyTo(bytesData, 0);
                }
                else
                {
                    bytesData = new byte[bytesRead];
                    bytesTemp = new byte[0];
                }
                //保存本次接收的数据
                for (int i = 0; i < bytesRead; i++)
                {
                    bytesData[bytesTemp.Length + i] = Convert.ToByte(serialPort.ReadByte());//read all data
                }

                for (int i = 0; i < bytesData.Length; i++)
                {
                    if ((bytesData[i] == 0xAA) && (bytesData[i + 2] == 0x0D))
                    {
                        result          = bytesData[i + 1];
                        InPhotoelectric = result > 0x00 ? true : false;
                        i += 2;
                        if (InPhotoelectric)
                        {
                            AllowGetData = true;
                            TempInt++;
                        }
                        else
                        {
                            AllowGetData = false;
                            TempInt      = 0;
                        }
                    }
                }
            };


            if (!port.IsOpen)
            {
                port.Open();
            }
        }
예제 #2
0
        /// <summary>
        /// 光电初始化
        /// </summary>
        public void PhotoelectricInit()
        {
            var port = SerialPortHelp.GetDevicePort(DeviceType.Photoelectric, DetectionType.Light);

            port.DataReceived += Photoelectric_DataReceived;
            if (!port.IsOpen)
            {
                port.Open();
            }
        }
예제 #3
0
 public YYLight()
 {
     PortItems = new List <PortElement>();
     PortItems.Add(SerialPortHelp.GetDevicePort(DeviceType.Photoelectric, DetectionType.Light));
 }