コード例 #1
0
ファイル: ComConnector.cs プロジェクト: zhangjianqi/RemotePLC
        public override int Receive(ref byte[] commRead, int NumBytes)
        {
            if (!_vcom.IsOpen)
            {
                return(0);
            }

            int num = 0;

            try
            {
                num = _vcom.Read(ref commRead, NumBytes);
            }
            catch (Exception e)
            {
                Logger.Error(e.ToString());
            }
            return(num);
        }
コード例 #2
0
        private void commPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            lock (CommPortMonitor)
            {
                SerialPort LocalPort = (SerialPort)sender;
                if (LocalPort != null)
                {
                    try
                    {
                        //read data waiting in the buffer
                        int bytes = LocalPort.BytesToRead;
                        if (bytes > 0)
                        {
                            byte[] commBuffer = new byte[bytes];
                            CommPort.Read(commBuffer, 0, bytes);

                            //Convert byte received to floats & populate paket to send trought UDP
                            byte[]       prevRemainig;
                            byte[]       actualRemaning;
                            List <float> receivedFloats = CommParserEncoder.ParseBytes(commBuffer, out prevRemainig, out actualRemaning);

                            //Send packets trought the socket;
                            List <byte[]> packetsSent = SendPackets(receivedFloats);

                            if (this.ShowReceivedSerialValues.Checked)
                            {
                                //print floats on serial text box, in main thread with Action Delegate (best way to implement 'almost anonymous delegate' in .NET 2.0)
                                this.BeginInvoke(new Action <List <float> >(PrintFloats), new object[] { receivedFloats });
                            }

                            if (this.LogSerialValuesOnFile.Checked)
                            {
                                DebugLogger.LogPackets(receivedFloats, commBuffer, prevRemainig, actualRemaning, packetsSent);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "COM Port Receiving Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
コード例 #3
0
        // runs sucking data out of the comm port and into the recv buffer.
        // sort of a hack but needs to be polling because ComEmulDrv ports do not
        // seem to work w/ events.
        //  this thread is now somewhat misnamed as it handles both reading and writing
        //  Read and write need to be run on the same thread as Windows does not seem to be
        // able to handle read on one thread and write on another without deadlocking from time
        // to time.
        // not a big problem .. we set the comm port to not block and then alternate reading and
        // writing data from the port.
        private void RecvThread()
        {
            byte[] recv_buf = new byte[100];              // 20 bytes should be enough
            byte[] xmit_buf = new byte[100];
            int    num_read;
            uint   num_to_write;
            int    num_written;

#if DEBUG
            Console.WriteLine("SDRSerialPort.RecvThread alive and well");
#endif
            while (keepRecvThreadRunning)
            {
                try
                {
                    num_read = commPort.Read(recv_buf, 100);                     // fixme -- error checking
                    // Console.WriteLine("numread=" + num_read);
                    if (num_read > 0)
                    {
                        recvBuf.put(recv_buf, (uint)num_read);
                    }
                    num_to_write = xmitBuf.get(xmit_buf, 100);
                    if (num_to_write > 0)
                    {
                        num_written = commPort.Write(xmit_buf, (int)num_to_write);
                    }
                    if (keepRecvThreadRunning && !close_pending)
                    {
                        xmitBuf.waitForData(recvThreadPollingInterval);
                    }
                    // Thread.Sleep(recvThreadPollingInterval);
                }
                catch (ThreadInterruptedException) {}
            }
            if (close_pending)
            {
                commPort.Close();
            }
#if DEBUG
            Console.WriteLine("SDRSerialPort.RecvThread dies");
#endif
        }
コード例 #4
0
        protected void Read(byte[] buffer, int offset, int count)
        {
            ExecuteCommOperation("Read", 1, delegate() { CommPort.Read(buffer, offset, count); });
//            _commPort.Read(buffer, offset, count);
        }