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); } } }
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); } }
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); }