2017-04-10 5 views
-2

私は、コンソール自体のバッテリーの電流を考慮したソフトウェアがないいくつかの気象ステーションを持っています。 現在のコンソールを読み取るコードが見つかりましたが、同時に8つのコンソールの現在のコードを読み取る必要があります。複数のCOMポートを同時に読み取る方法

今のところCOMポートと転送レートデータを接続し、TXTファイルに現在の電圧コンソールを記録します。 いくつかのCOMポートを同時に読み取るコードを修正するにはどうすればよいですか?

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 


namespace ConsoleApplication1 
{ 

     public class WeatherLoopData 
    { 
     /* 
      private int barTrend = 0, 
        currWindSpeed = 0, 
        avgWindSpeed = 0, 
        insideHumidity = 0, 
        outsideHumidity = 0, 
        windDirection = 0; 
     */ 
     private float voltage = 0; 
     // private float voltage1 = 0; 
     /* 
     private bool transmitter = true; 

     private float barometer = 0.0F, 
         insideTemp = 0.0F, 
         outsideTemp = 0.0F, 
         dayRain = 0.0F; 
     */ 
     private DateTime sunRise = DateTime.Now, 
         sunSet = DateTime.Now; 
     /* 
     public int BarometricTrend { get { return barTrend; } } 

     public int CurrentWindSpeed { get { return currWindSpeed; } } 

     public int AvgWindSpeed { get { return avgWindSpeed; } } 

     public int InsideHumidity { get { return insideHumidity; } } 

     public int OutsideHumidity { get { return outsideHumidity; } } 

     public float Barometer { get { return barometer; } } 

     public float InsideTemperature { get { return insideTemp; } } 

     public float OutsideTemperature { get { return outsideTemp; } } 

     public float DailyRain { get { return dayRain; } } 

     public int WindDirection 
     { 
      get { return windDirection; } 

     } 
     */ 

     public void Load(byte[] loopArray) 
     { 

      int hours, 
       minutes; 
      string timeString; 
      DateTime currTime; 
      /* 
      barTrend = Convert.ToInt32((sbyte)loopArray[3]);     // Sbyte - signed byte 
      barometer = (float)(BitConverter.ToInt16(loopArray, 7))/1000;  // Uint16 
      insideTemp = (float)(BitConverter.ToInt16(loopArray, 9))/10;  // Uint16 
      insideHumidity = Convert.ToInt32(loopArray[11]);     // Byte - unsigned byte 
      outsideTemp = (float)(BitConverter.ToInt16(loopArray, 12))/10; // Uint16 
      outsideHumidity = Convert.ToInt32(loopArray[33]);     // Byte - unsigned byte 
      windDirection = BitConverter.ToInt16(loopArray, 16);    // Uint16 
      currWindSpeed = Convert.ToInt32(loopArray[14]);      // Byte - unsigned byte 
      avgWindSpeed = Convert.ToInt32(loopArray[15]);      // Byte - unsigned byte 
      dayRain = (float)(BitConverter.ToInt16(loopArray, 50))/100;  // Uint16 
      */ 
      voltage = (float)BitConverter.ToInt16(loopArray, 87) * 300/512/100; //Тока на конзолата 
      //voltage1 = (float)BitConverter.ToInt16(loopArray, 87) * 300/512/100; //Тока на конзолата 
      //transmitter = BitConverter.ToBoolean(loopArray, 86); //Не показва тока на трансмитера 



      // get the current date and time 
      currTime = DateTime.Now; 

      // Time from the Vatnage is all in 24-hour format. I move it into a string so I can manipulate it 
      // more easily. 
      timeString = BitConverter.ToInt16(loopArray, 91).ToString(); // Uint16 
      // Exract hours and minutes and convert them to integers - required by Datetime 
      hours = Convert.ToInt32(timeString.Substring(0, timeString.Length - 2)); 
      minutes = Convert.ToInt32(timeString.Substring(timeString.Length - 2, 2)); 
      // Create a new Datetime instance - use surrent year, month and day 
      sunRise = new DateTime(currTime.Year, currTime.Month, currTime.Day, hours, minutes, 0); 

      timeString = BitConverter.ToInt16(loopArray, 93).ToString(); // Uint16 
      hours = Convert.ToInt32(timeString.Substring(0, timeString.Length - 2)); 
      minutes = Convert.ToInt32(timeString.Substring(timeString.Length - 2, 2)); 
      sunSet = new DateTime(currTime.Year, currTime.Month, currTime.Day, hours, minutes, 0); 
     } 



     public string DebugString() 
     { 

      StringBuilder outputString = new StringBuilder(); 
      /* 
      Format the string for output 
      outputString.Append("Barometer: " + barometer.ToString("f2") + "in. " + BarTrendText() + Environment.NewLine); 
      outputString.Append("Inside Temp in F: " + insideTemp.ToString() + Environment.NewLine); 
      outputString.Append("Inside Humidity: " + insideHumidity.ToString() + "%" + Environment.NewLine); 
      outputString.Append("Outside Temp: " + outsideTemp.ToString() + Environment.NewLine); 
      outputString.Append("Outside Humidity: " + outsideHumidity.ToString() + "%" + Environment.NewLine); 
      outputString.Append("Wind Direction: " + WindDirectionText() + " @ " + windDirection.ToString() + " degrees" + Environment.NewLine); 
      outputString.Append("Current Wind Speed: " + currWindSpeed.ToString() + "MPH" + Environment.NewLine); 
      outputString.Append("10 Minute Average Wind Speed: " + avgWindSpeed.ToString() + "MPH" + Environment.NewLine); 
      outputString.Append("Daily Rain: " + dayRain.ToString() + "in" + Environment.NewLine); 
      outputString.Append("Sunrise: " + sunRise.ToString("t") + Environment.NewLine); 
      outputString.Append("Sunset: " + sunSet.ToString("t") + Environment.NewLine); 
      outputString.Append("Voltage: " + voltage.ToString() + Environment.NewLine); 
      outputString.Append("Transmitter Voltage: " + transmitter.ToString() + Environment.NewLine); */ 
      File.WriteAllText(@"C:\Users\admin\Desktop\Voltage.txt", "№ станция" + " " + string.Format("{0:yyyy-MM-dd HH-mm}", DateTime.Now) + " " + Convert.ToString(voltage) + "V"); 
      //File.WriteAllText(@"C:\Users\admin\Desktop\Voltage1.txt", "№ станция" + " " + string.Format("{0:yyyy-MM-dd HH-mm}", DateTime.Now) + " " + Convert.ToString(voltage1) + "V"); 


      return (outputString.ToString()); 
     } 

     /* private string BarTrendText() 
     { 
      throw new NotImplementedException(); 
     } */ 

     /* private string WindDirectionText() 
     { 
      throw new NotImplementedException(); 
     } */ 

     public float BatteryVoltage { get { return voltage; } } 

     //public float BatteryVoltage1 { get { return voltage1; } } 



     // public bool TransmitterVoltage { get { return transmitter; } } 


     public DateTime SunRise { get { return sunRise; } } 

     public DateTime SunSet { get { return sunSet; } } 

     // Load - dissassembles the byte array passed in and loads it into local data that the accessors can use. 
     // Actual data is in the format to the right of the assignments - I convert it to make it easier to use 
     // When bytes have to be assembled into 2-byte, 16-bit numbers, I convert two bytes from the array into 
     // an Int16 (16-bit integer). When a single byte is all that's needed, I just convert it to an Int32. 
     // In the end, all integers are cast to Int32 for return. 


     } 
    } 



    using System; 
using System.Collections.Generic; 
using System.Text; 
using System.IO; 


namespace ConsoleApplication1 
{ 

     public class WeatherLoopData 
    { 
     /* 
      private int barTrend = 0, 
        currWindSpeed = 0, 
        avgWindSpeed = 0, 
        insideHumidity = 0, 
        outsideHumidity = 0, 
        windDirection = 0; 
     */ 
     private float voltage = 0; 
     // private float voltage1 = 0; 
     /* 
     private bool transmitter = true; 

     private float barometer = 0.0F, 
         insideTemp = 0.0F, 
         outsideTemp = 0.0F, 
         dayRain = 0.0F; 
     */ 
     private DateTime sunRise = DateTime.Now, 
         sunSet = DateTime.Now; 
     /* 
     public int BarometricTrend { get { return barTrend; } } 

     public int CurrentWindSpeed { get { return currWindSpeed; } } 

     public int AvgWindSpeed { get { return avgWindSpeed; } } 

     public int InsideHumidity { get { return insideHumidity; } } 

     public int OutsideHumidity { get { return outsideHumidity; } } 

     public float Barometer { get { return barometer; } } 

     public float InsideTemperature { get { return insideTemp; } } 

     public float OutsideTemperature { get { return outsideTemp; } } 

     public float DailyRain { get { return dayRain; } } 

     public int WindDirection 
     { 
      get { return windDirection; } 

     } 
     */ 

     public void Load(byte[] loopArray) 
     { 

      int hours, 
       minutes; 
      string timeString; 
      DateTime currTime; 
      /* 
      barTrend = Convert.ToInt32((sbyte)loopArray[3]);     // Sbyte - signed byte 
      barometer = (float)(BitConverter.ToInt16(loopArray, 7))/1000;  // Uint16 
      insideTemp = (float)(BitConverter.ToInt16(loopArray, 9))/10;  // Uint16 
      insideHumidity = Convert.ToInt32(loopArray[11]);     // Byte - unsigned byte 
      outsideTemp = (float)(BitConverter.ToInt16(loopArray, 12))/10; // Uint16 
      outsideHumidity = Convert.ToInt32(loopArray[33]);     // Byte - unsigned byte 
      windDirection = BitConverter.ToInt16(loopArray, 16);    // Uint16 
      currWindSpeed = Convert.ToInt32(loopArray[14]);      // Byte - unsigned byte 
      avgWindSpeed = Convert.ToInt32(loopArray[15]);      // Byte - unsigned byte 
      dayRain = (float)(BitConverter.ToInt16(loopArray, 50))/100;  // Uint16 
      */ 
      voltage = (float)BitConverter.ToInt16(loopArray, 87) * 300/512/100; //Тока на конзолата 
      //voltage1 = (float)BitConverter.ToInt16(loopArray, 87) * 300/512/100; //Тока на конзолата 
      //transmitter = BitConverter.ToBoolean(loopArray, 86); //Не показва тока на трансмитера 



      // get the current date and time 
      currTime = DateTime.Now; 

      // Time from the Vatnage is all in 24-hour format. I move it into a string so I can manipulate it 
      // more easily. 
      timeString = BitConverter.ToInt16(loopArray, 91).ToString(); // Uint16 
      // Exract hours and minutes and convert them to integers - required by Datetime 
      hours = Convert.ToInt32(timeString.Substring(0, timeString.Length - 2)); 
      minutes = Convert.ToInt32(timeString.Substring(timeString.Length - 2, 2)); 
      // Create a new Datetime instance - use surrent year, month and day 
      sunRise = new DateTime(currTime.Year, currTime.Month, currTime.Day, hours, minutes, 0); 

      timeString = BitConverter.ToInt16(loopArray, 93).ToString(); // Uint16 
      hours = Convert.ToInt32(timeString.Substring(0, timeString.Length - 2)); 
      minutes = Convert.ToInt32(timeString.Substring(timeString.Length - 2, 2)); 
      sunSet = new DateTime(currTime.Year, currTime.Month, currTime.Day, hours, minutes, 0); 
     } 



     public string DebugString() 
     { 

      StringBuilder outputString = new StringBuilder(); 
      /* 
      Format the string for output 
      outputString.Append("Barometer: " + barometer.ToString("f2") + "in. " + BarTrendText() + Environment.NewLine); 
      outputString.Append("Inside Temp in F: " + insideTemp.ToString() + Environment.NewLine); 
      outputString.Append("Inside Humidity: " + insideHumidity.ToString() + "%" + Environment.NewLine); 
      outputString.Append("Outside Temp: " + outsideTemp.ToString() + Environment.NewLine); 
      outputString.Append("Outside Humidity: " + outsideHumidity.ToString() + "%" + Environment.NewLine); 
      outputString.Append("Wind Direction: " + WindDirectionText() + " @ " + windDirection.ToString() + " degrees" + Environment.NewLine); 
      outputString.Append("Current Wind Speed: " + currWindSpeed.ToString() + "MPH" + Environment.NewLine); 
      outputString.Append("10 Minute Average Wind Speed: " + avgWindSpeed.ToString() + "MPH" + Environment.NewLine); 
      outputString.Append("Daily Rain: " + dayRain.ToString() + "in" + Environment.NewLine); 
      outputString.Append("Sunrise: " + sunRise.ToString("t") + Environment.NewLine); 
      outputString.Append("Sunset: " + sunSet.ToString("t") + Environment.NewLine); 
      outputString.Append("Voltage: " + voltage.ToString() + Environment.NewLine); 
      outputString.Append("Transmitter Voltage: " + transmitter.ToString() + Environment.NewLine); */ 
      File.WriteAllText(@"C:\Users\admin\Desktop\Voltage.txt", "№ станция" + " " + string.Format("{0:yyyy-MM-dd HH-mm}", DateTime.Now) + " " + Convert.ToString(voltage) + "V"); 
      //File.WriteAllText(@"C:\Users\admin\Desktop\Voltage1.txt", "№ станция" + " " + string.Format("{0:yyyy-MM-dd HH-mm}", DateTime.Now) + " " + Convert.ToString(voltage1) + "V"); 


      return (outputString.ToString()); 
     } 

     /* private string BarTrendText() 
     { 
      throw new NotImplementedException(); 
     } */ 

     /* private string WindDirectionText() 
     { 
      throw new NotImplementedException(); 
     } */ 

     public float BatteryVoltage { get { return voltage; } } 

     //public float BatteryVoltage1 { get { return voltage1; } } 



     // public bool TransmitterVoltage { get { return transmitter; } } 


     public DateTime SunRise { get { return sunRise; } } 

     public DateTime SunSet { get { return sunSet; } } 

     // Load - dissassembles the byte array passed in and loads it into local data that the accessors can use. 
     // Actual data is in the format to the right of the assignments - I convert it to make it easier to use 
     // When bytes have to be assembled into 2-byte, 16-bit numbers, I convert two bytes from the array into 
     // an Int16 (16-bit integer). When a single byte is all that's needed, I just convert it to an Int32. 
     // In the end, all integers are cast to Int32 for return. 


     } 
    } 
+0

次のコードのようなものを持っているでしょう。 –

+0

どうすればいいの? –

+0

これは少し大きすぎるのでここで答えてください...あなたは何とかマルチスレッドについて学ばなければなりません。 [これはあなたを始めさせるかもしれません。](http://stackoverflow.com/questions/2605966/how-to-create-multiple-threads-in-c-sharp) –

答えて

0

私は、各ステーション用のクラスを作成し、その後、あなたはCOMポートごとに別のスレッドを使用することができます局のリストに

class Program 
    { 
     const int NUMBER_PORTS = 5; 
     static void Main(string[] args) 
     { 
      for(int i = 0; i < NUMBER_PORTS; i++) 
      { 
       WeatherLoopData newStation = new WeatherLoopData("Com" + (i + 1).ToString()); 
       WeatherLoopData.ports.Add(newStation); 
      } 


     } 
     public class WeatherLoopData 
     { 
      public static List<WeatherLoopData> ports = new List<WeatherLoopData>(); 
      SerialPort port = null; 
      public WeatherLoopData(string portName) 
      { 
       port = new SerialPort(portName); 
      } 
      //enter your code here 

     } 
関連する問題