using System; using System.Collections.Generic; using System.IO; namespace RobvanderWoude { class EnergyLoggerConfig { static string progver = "1.00"; static int minutesinfuture = 1; static int deviceID = 1; static double daytimetariff = 0.076; static double nighttimetariff = 0.051; static Currency currency = Currency.Euro; static DateFormat dateformat = DateFormat.DateDmy; static TimeFormat timeformat = TimeFormat.Clock24Hour; static int Main( string[] args ) { if ( args.Length < 2 || args.Length > 7 ) { return ShowHelp( ); } try { foreach ( string arg in args ) { string key, val; if ( arg.IndexOf( ':' ) == -1 ) { key = arg.ToLower( ); val = null; } else { key = arg.Substring( 0, arg.IndexOf( ':' ) ).ToLower( ); val = arg.Substring( arg.IndexOf( ':' ) + 1 ).ToLower( ); } switch ( key ) { case "/12h": timeformat = TimeFormat.Clock12Hour; break; case "/c": switch ( val[0] ) { case 'd': case 'u': case '$': currency = Currency.USDollar; break; case 'e': currency = Currency.Euro; break; case 'f': case 's': currency = Currency.SwissFrank; break; case 'g': case 'p': currency = Currency.GBPound; break; default: return ShowHelp( ); } break; case "/dt": daytimetariff = Convert.ToDouble( val ); break; case "/f": minutesinfuture = Convert.ToInt32( val ); break; case "/id": deviceID = Convert.ToInt32( val ); break; case "/mdy": dateformat = DateFormat.DateMdy; break; case "/nt": nighttimetariff = Convert.ToDouble( val ); break; default: return ShowHelp( ); } } } catch ( Exception ) { return ShowHelp( ); } if ( daytimetariff <= 0 || daytimetariff >= 10 ) { return ShowHelp( ); } if ( nighttimetariff <= 0 || nighttimetariff >= 10 ) { return ShowHelp( ); } if ( deviceID < 0 || deviceID > 9 ) { return ShowHelp( ); } if ( minutesinfuture < 0 || minutesinfuture > 60 ) { return ShowHelp( ); } // Minute(s) to write the file to SD card and then load the SD card into the Energy Logger 3500 DateTime soon = DateTime.Now.AddMinutes( minutesinfuture ); byte[] bindata = new byte[] { 0xB8, 0xAD, 0xF2, // fixed 3 bytes header Convert.ToByte( deviceID), Convert.ToByte( timeformat ), Convert.ToByte( dateformat ), Convert.ToByte( soon.Hour ), Convert.ToByte( soon.Minute ), Convert.ToByte( soon.Month ), Convert.ToByte( soon.Day ), Convert.ToByte( soon.Year % 100 ), // 2-digit year (without the century) Convert.ToByte( currency ), Convert.ToByte( Convert.ToInt32( nighttimetariff ) ), Convert.ToByte( Convert.ToInt32( nighttimetariff * 10 ) % 10 ), Convert.ToByte( Convert.ToInt32( nighttimetariff * 100 ) % 100 ), Convert.ToByte( Convert.ToInt32( nighttimetariff * 1000 ) % 1000 ), Convert.ToByte( Convert.ToInt32( daytimetariff ) ), Convert.ToByte( Convert.ToInt32( daytimetariff * 10 ) % 10 ), Convert.ToByte( Convert.ToInt32( daytimetariff * 100 ) % 100 ), Convert.ToByte( Convert.ToInt32( daytimetariff * 1000 ) % 1000 ) }; // Create the binary file using ( BinaryWriter binfile = new BinaryWriter( File.Open( Path.Combine( Directory.GetCurrentDirectory( ), "setupel3.bin" ), FileMode.Create ) ) ) { binfile.Write( bindata ); } return 0; } static void ReadConfig( ) { } static int ShowHelp( params string[] errmsg ) { /* EnergyLoggerConfig.exe, Version 1.00 Create a binary configuration file for the VoltCraft Energy Logger 3500 or 4000 Usage: EnergyLoggerConfig /dt:daytimetariff /nt:nighttimetariff [ options ] Where: daytimetariff is the day time tariff for 1 kWh nighttimetariff is the night time tariff for 1 kWh Options: /ID:deviceID set device ID (0..9; default: 1) /12H use 12 hour clock (default: 24 hour clock) /MDY use date format M/D/Y (default: D-M-Y) /F:minutes set time to specified number of minutes in future to allow sufficient time to load the SD card into the device (0..60; default: 1 minute) /C:currency set currency: G = GB Pound, S = Swiss Frank, U = US Dollar, E = Euro (default: Euro) The settings will be written to a binary file named "setupel3.bin", located in the current directory. It should be written to SD card and loaded into the device in the specified number of minutes (/F switch) for optimal accuracy of cost calculations. Once loaded, the device will erase the file from the SD card. Detailed information on the setupel3.bin file format can be found at http://wiki.td-er.nl/index.php?title=Energy_Logger_3500 Written by Rob van der Woude http://www.robvanderwoude.com */ if ( errmsg.Length > 0 ) { List errargs = new List( errmsg ); errargs.RemoveAt( 0 ); Console.Error.WriteLine( ); Console.ForegroundColor = ConsoleColor.Red; Console.Error.Write( "ERROR:\t" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) ); Console.ResetColor( ); } Console.Error.WriteLine( ); Console.Error.WriteLine( "EnergyLoggerConfig.exe, Version {0}", progver ); Console.Error.WriteLine( "Create a binary configuration file for the VoltCraft Energy Logger 3500 or 4000" ); Console.Error.WriteLine( ); Console.Error.Write( "Usage: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.WriteLine( "EnergyLoggerConfig /dt:daytimetariff /nt:nighttimetariff [ options ]" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.Write( "Where: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "daytimetariff" ); Console.ResetColor( ); Console.Error.WriteLine( " is the day time tariff for 1 kWh" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " nighttimetariff" ); Console.ResetColor( ); Console.Error.WriteLine( " is the night time tariff for 1 kWh" ); Console.Error.WriteLine( ); Console.Error.Write( "Options: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "/ID:deviceID" ); Console.ResetColor( ); Console.Error.WriteLine( " set device ID (0..9; default: 1)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /12H" ); Console.ResetColor( ); Console.Error.WriteLine( " use 12 hour clock (default: 24 hour clock)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /MDY" ); Console.ResetColor( ); Console.Error.WriteLine( " use date format M/D/Y (default: D-M-Y)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /F:minutes" ); Console.ResetColor( ); Console.Error.WriteLine( " set time to specified number of minutes in future" ); Console.Error.WriteLine( " to allow sufficient time to load the SD card into" ); Console.Error.WriteLine( " the device (0..60; default: 1 minute)" ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " /C:currency" ); Console.ResetColor( ); Console.Error.Write( " set currency: " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "G" ); Console.ResetColor( ); Console.Error.Write( " = GB Pound, " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "S" ); Console.ResetColor( ); Console.Error.WriteLine( " = Swiss Frank," ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( " U" ); Console.ResetColor( ); Console.Error.Write( " = US Dollar, " ); Console.ForegroundColor = ConsoleColor.White; Console.Error.Write( "E" ); Console.ResetColor( ); Console.Error.WriteLine( " = Euro (default: Euro)" ); Console.Error.WriteLine( ); Console.Error.WriteLine( "The settings will be written to a binary file named \"setupel3.bin\", located in" ); Console.Error.WriteLine( "the current directory. It should be written to SD card and loaded into the" ); Console.Error.WriteLine( "device in the specified number of minutes (/F switch) for optimal accuracy of" ); Console.Error.WriteLine( "cost calculations. Once loaded, the device will erase the file from the SD card." ); Console.Error.WriteLine( "Detailed information on the setupel3.bin file format can be found at" ); Console.ForegroundColor = ConsoleColor.DarkGray; Console.Error.WriteLine( "http://wiki.td-er.nl/index.php?title=Energy_Logger_3500" ); Console.ResetColor( ); Console.Error.WriteLine( ); Console.Error.WriteLine( "Written by Rob van der Woude" ); Console.Error.WriteLine( "http://www.robvanderwoude.com" ); return 1; } } public enum Currency { GBPound = 1, SwissFrank = 2, USDollar = 4, Euro = 8 } public enum DateFormat { DateMdy = 1, DateDmy = 2 } public enum TimeFormat { Clock12Hour = 1, Clock24Hour = 2 } }