コード例 #1
0
ファイル: Demux.cs プロジェクト: ehnet247/UartMuxDemux
        private void processReceivedPacketsBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            while (worker.CancellationPending == false)
            {
                if (lPacketsToDownload.Count > 0)
                {
                    // Create a string initialized with a header
                    string strPacketToSend = CustomDefs.strMuxHeader;
                    // Add the COM port number to the string to send
                    strPacketToSend += PortTools.GetPortNumber(lPacketsToDownload[0].strPortSource).ToString();
                    // Add a field separator
                    strPacketToSend += CustomDefs.strMuxFieldSeparator;
                    // Add the time
                    strPacketToSend += lPacketsToDownload[0].strTime;
                    // Convert the packet data in ASCII chars
                    foreach (byte dataByte in lPacketsToDownload[0].aData)
                    {
                        strPacketToSend += dataByte.ToString("X2") + CustomDefs.strDemuxByteSeparator;
                    }
                    // Add an end of line char
                    strPacketToSend += CustomDefs.strMuxEndOfLine;
                    // Send the packet
                    masterPort.serialPort.Write(strPacketToSend);
                }
                else
                {
                    // Pause thread for 100ms to breath
                    System.Threading.Thread.Sleep(100);
                }
            }
        }
コード例 #2
0
        private void SortPortsInBoxes()
        {
            // Clear the ComboBox
            comboBoxSlavePortEdit.Items.Clear();
            // Clear the ListBox
            listBoxSlavePorts.Items.Clear();
            // Create a list of index sorted
            List <int> lSortedIndexesList = new List <int>();

            foreach (SlavePort sp in slavePortsList)
            {
                if (sp.serialPort.PortName.Length > 3)
                {
                    lSortedIndexesList.Add(PortTools.GetPortNumber(sp.serialPort.PortName));
                }
            }
            lSortedIndexesList.Sort();
            // Create the same list in string type, with "COM" added at the beginning
            List <string> slavePortsNameList = new List <string>();

            for (int iPortIndex = 0; iPortIndex < lSortedIndexesList.Count; iPortIndex++)
            {
                string strPortToAdd = "COM" + lSortedIndexesList[iPortIndex].ToString();
                slavePortsNameList.Add(strPortToAdd);
                listBoxSlavePorts.Items.Add(strPortToAdd);
                comboBoxSlavePortEdit.Items.Add(strPortToAdd);
            }
        }
コード例 #3
0
        private int GetPortIndexByNumber(int iPortNum)
        {
            int iPortIndex = 0;

            while (iPortIndex < slavePortsList.Count)
            {
                if (PortTools.GetPortNumber(slavePortsList[iPortIndex].serialPort.PortName) == iPortNum)
                {
                    return(iPortIndex);
                }
                iPortIndex++;
            }
            return(-1);
        }
コード例 #4
0
        private void dataReceptionBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            while (worker.CancellationPending == false)
            {
                if ((serialPort.IsOpen) && (serialPort.BytesToRead > 0))
                {
                    string strReceptionTime = PortTools.GetTimeString();
                    int    iBytesToRead     = serialPort.BytesToRead;
                    // Are we already receiving a packet?
                    if ((RxCurrentPacket == null) || (RxCurrentPacket.Count == 0))
                    {
                        // No: instanciate a new packet
                        RxCurrentPacket = new List <byte>();
                        // Get the timecode
                        strCurrentPacketTimecode = PortTools.GetTimeString();
                        // Start the timer if necessary
                        if (eofDetection == EofDetection.Unknown)
                        {
                            timerPacketTimeout.Interval = this.GetPacketTimeoutValue();
                            timerPacketTimeout.Start();
                        }
                    }
                    else
                    {
                        // Yes, a packet is currently being received
                    }
                    // Now read the received bytes
                    // Create a temporary reading buffer
                    byte[] rxBuffer = new byte[iBytesToRead];
                    // Read the received bytes
                    serialPort.Read(rxBuffer, 0, iBytesToRead);
                    // Copy the received bytes to the current packet
                    RxCurrentPacket.AddRange(rxBuffer);
                    // Check if the packet is complete
                    switch (eofDetection)
                    {
                    case EofDetection.FixedSize:
                        if (RxCurrentPacket.Count >= this.iPacketLength)
                        {
                            // Upload the packet
                            UploadDataToMux(serialPort.PortName,
                                            strCurrentPacketTimecode,
                                            RxCurrentPacket.ToArray());
                            // Free the rx buffer
                            RxCurrentPacket.Clear();
                        }
                        break;

                    case EofDetection.FirstByte:
                        // Get the packet size
                        int iPacketSize = (int)RxCurrentPacket[0];
                        if (RxCurrentPacket.Count >= iPacketSize)
                        {
                            // Upload the packet
                            UploadDataToMux(serialPort.PortName,
                                            strCurrentPacketTimecode,
                                            RxCurrentPacket.ToArray());
                        }
                        break;
                    }
                }
                else
                {
                    if (serialPort.IsOpen == false)
                    {
                        dataReceptionBackgroundWorker.CancelAsync();
                    }
                    // Pause thread for 100ms to breath
                    System.Threading.Thread.Sleep(100);
                }
            }
        }