My Blog

lupok
My Blog
RssIcon

ThreeJS: Creare un solido e modficare il punto di vista con il mouse

by host on mercoledì 20 novembre 2013 10:09
Come creare un cubo mediante ThreeJS, e modificare il punto di vista attrevrso movimenti del mouse:


   

 

Per modificare il punto di vista tenere premuto il tasto sinistro e muovere il mouse.

Creare un web service in ASP.NET e pubblicarlo su IIS

by host on lunedì 18 novembre 2013 12:05

1) Creare un nuovo progetto Visual Studio del tipo ASP.NET Web Service Application: 

 

 

Non modifichiamo la classe creata automaticamente da visual studio in quanto scopo del presente documento e' solo quello di mostrare come si crea un servizio ASP.NET e lo si pubblica su IIS:

 

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
 
namespace TestASPNETWebService
{
   /// <summary>
   /// Summary description for Service1
   /// </summary>
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   public class Service1 : System.Web.Services.WebService
   {
 
      [WebMethod]
      public string HelloWorld()
      {
         return "Hello World";
      }
   }
}

2) Tasto destro sul progetto dal Solution Explorer e quindi click su Publish...  

 

 

Impostiamo i parametri necessari per la pubblicazione su IIS, se vogliamo pubblicare il servizio in una specifica directory del percorso radice utilizzato da IIS ("C:\inetpub\wwwroot") occorre crearla prima della pubblicazione. 

 Ad esempio noi pubblichiamo in "C:\inetpub\wwwroot\TestASPNETWebService" 

 

3) Una volta pubblicato in servizio dobbiamo eseguire qualche operazione su IIS, quindi apriamo IIS Manager da Control Panel -> Administrative Tools per convertire ad applicazione il servizio pubblicato su IIS: 

 

 

 

Occorre prestare particolare attenzione che tipo di framework .NET necessario al servizio coincida con quello assegnato di default da IIS, nel caso sia necessario modifcarlo aprire le impostazione avanzate per il servizio e quindi selezionare l'Application Pool corretto: 

 

 

4) a questo punto possiamo utilizzare il servizio:

 

 

 

Use the library "libnodave" to communicate with a SIEMENS S7-200 PLC

by host on lunedì 11 novembre 2013 12:14
A short example developed in .NET showing how to query a PLC siemens S7-200 using PPI protocol and the library "libnodave"..

 

using System;
using System.Collections.Generic;
using System.Text;
 
namespace TestPPI
{
   class Program
   {
      public static void Main()
      {
         libnodave.daveOSserialType fds;
         libnodave.daveInterface di;
         libnodave.daveConnection dc;
 
         fds.rfd = libnodave.setPort("COM6""9600"'E');
         fds.wfd = fds.rfd;
         if (fds.rfd <= 0)
            Console.WriteLine("Error on open serial port ");
         else
         {
            int localPPI = 0;
            int plcPPI = 2;
            int res;
 
            di = new libnodave.daveInterface(fds, "IF1", localPPI, 
               libnodave.daveProtoPPI, libnodave.daveSpeed187k);
            di.setTimeout(1000000);
 
            dc = new libnodave.daveConnection(di, plcPPI, 0, 0);
            if (0 == dc.connectPLC())
            {
               Byte[] bytes = new Byte[16];
               res = dc.readBytes(libnodave.daveFlags, 0, 0, 16, bytes);
               if (res != 0)
                  Console.WriteLine("error {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Console.WriteLine("MD 0: {0}", dc.getS32());
                  Console.WriteLine("MD 4: {0}", dc.getS32());
                  Console.WriteLine("MD 8: {0}", dc.getS32());
                  Console.WriteLine("MD 12: {0}", dc.getFloat());
               }
 
               bytes = new Byte[1];
               res = dc.readBytes(libnodave.daveDB, 1, 20, 1, bytes);
               if (res != 0)
                  Console.WriteLine("error on read VB 10: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
                  Console.WriteLine("VB 20: " + bytes[0]);
 
               bytes = new Byte[2];
               res = dc.readBytes(libnodave.daveDB, 1, 110, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read VW 110: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Array.Reverse(bytes);
                  Console.WriteLine("VW 110: " + BitConverter.ToInt16(bytes, 0));
               }
               bytes = new Byte[4];
               res = dc.readBytes(libnodave.daveDB, 1, 120, 4, bytes);
               if (res != 0)
                  Console.WriteLine("error on read VD 120: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Array.Reverse(bytes);
                  Console.WriteLine("VD 120: " + BitConverter.ToSingle(bytes, 0));
               }
 
               bytes = new Byte[1];
               res = dc.readBytes(libnodave.daveFlags, 0, 10, 1, bytes);
               if (res != 0)
                  Console.WriteLine("error on read MB 10: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
                  Console.WriteLine("MB 10: " + bytes[0]);
 
               bytes = new Byte[2];
 
               res = dc.readBytes(libnodave.daveFlags, 0, 12, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read MW 12: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Array.Reverse(bytes);
                  Console.WriteLine("MW 12: " + BitConverter.ToInt16(bytes, 0));
               }
 
               bytes = BitConverter.GetBytes((Int16)(BitConverter.ToInt16(bytes, 0) + 1));
               Array.Reverse(bytes);
 
               res = dc.writeBytes(libnodave.daveFlags, 0, 12, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on write MW 12: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
 
               bytes = new Byte[2];
               res = dc.readBytes(libnodave.daveFlags, 0, 12, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read MW 12: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Array.Reverse(bytes);
                  Console.WriteLine("MW 12: " + BitConverter.ToInt16(bytes, 0));
               }
 
               res = dc.writeBits(libnodave.daveFlags, 0, 5 * 8 + 3, 1, new Byte[] { 1 });
               if (res != 0)
                  Console.WriteLine("error on write MBX 5.3: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
 
               bytes = new Byte[1];
               res = dc.readBytes(libnodave.daveFlags, 0, 5, 1, bytes);
               if (res != 0)
                  Console.WriteLine("error on read MB 5: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
                  Console.WriteLine("MB 5: " + bytes[0]);
 
               bytes = new Byte[2];
               res = dc.readBytes(libnodave.daveCounter200, 0, 0, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read Z2 0: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
                  Console.WriteLine("Z2 0: " + BitConverter.ToInt16(bytes, 0));
 
               bytes = new Byte[2];
               res = dc.readBytes(libnodave.daveTimer200, 0, 0, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read T2 0: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
                  Console.WriteLine("T2 0: " + BitConverter.ToInt16(bytes, 0));
 
               bytes = new Byte[2];
               res = dc.readBytes(libnodave.daveSysFlags, 0, 10, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read SMW 10: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Array.Reverse(bytes);
                  Console.WriteLine("SMW 10: " + BitConverter.ToInt16(bytes, 0));
               }
 
               bytes = new Byte[2];
               res = dc.readBytes(libnodave.daveAnaIn, 0, 0, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read AEW 0: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Array.Reverse(bytes);
                  Console.WriteLine("AEW 0: " + BitConverter.ToInt16(bytes, 0));
               }
 
               bytes = new Byte[2];
               res = dc.readBytes(libnodave.daveAnaOut, 0, 0, 2, bytes);
               if (res != 0)
                  Console.WriteLine("error on read AAW 0: {0}, {1}", res, 
                     libnodave.daveStrerror(res));
               else
               {
                  Array.Reverse(bytes);
                  Console.WriteLine("AAW 0: " + BitConverter.ToInt16(bytes, 0));
               }
            }
            dc.disconnectPLC();
 
            libnodave.closePort(fds.rfd);
         }
 
         Console.WriteLine("Press ENTER to exit");
         Console.ReadLine();
      }
   }
}

File sharing

by host on venerdì 25 ottobre 2013 10:09

Un esempio che mostra come condividere l'accesso ad un file tra processi, di cui uno scrive e l'altro legge:

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
 
namespace WriteFile
{
   class Program
   {
      static void Main(string[] args)
      {
         Thread thread = new Thread(delegate()
         {
            using (FileStream fs = new FileStream("file.txt", 
               FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
            using (StreamWriter sw = new StreamWriter(fs))
            {
               while (true)
               {
                  try
                  {
                     fs.Position = 0;
                     sw.WriteLine(DateTime.Now.ToString());
                     sw.Flush();
 
                  }
                  finally
                  {
                     Thread.Sleep(500);
                  }
               }
            }
         });
         thread.IsBackground = true;
         thread.Start();
 
         Console.WriteLine("Press ENTER to exit");
         Console.ReadLine();
      }
   }
}

Load different XAML with the same view model

by host on martedì 22 ottobre 2013 10:06
Come caricare XAML differenti che utilizzano view model identici. Utilizzando il pattern MVVM e' possibile utilizzare XAML diversi e collegarli a runtime con contesti dati differenti
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
	Title="Page">
   <Grid>
      <Button Name="button1" Content="Button page1" HorizontalAlignment="Left" 
              Height="87" Margin="56,35,0,0" VerticalAlignment="Top" Width="136" 
              Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=button1, 
              Path=Content}" />
      <Label HorizontalAlignment="Left" Height="41" Margin="24,182,0,0" 
             Content="{Binding Path=MyProp1}" VerticalAlignment="Top" Width="252"/>
   </Grid>
</Page>

Gestire eventi del mouse in MVVM

by host on martedì 22 ottobre 2013 07:10
Come implementare comandi in WPF da collegare mediante pattern MVVM in grado di gestire qualsiasi eventi del mouse:

 

<Button Name="button1" Grid.Row="0" Content="Button" HorizontalAlignment="Left" 
   Height="62" Margin="122,44,0,0" VerticalAlignment="Top" Width="171"
   local:MouseDownUpCommandBehavior.MouseDownCommand="{Binding DownCommand}" 
   local:MouseDownUpCommandBehavior.MouseUpCommand="{Binding UpCommand}" 
   local:MouseDownUpCommandBehavior.CommandParameter="{Binding ElementName=button1, Path=Content}" />

Google drive file management in .NET

by host on venerdì 18 ottobre 2013 15:51

Un esempio che mostra come utilizzare le Google Drive API per .NET al fine di gestire file.
In particolare in questo esempio viene creata una nuova cartella in Google Drive solo se non esiste ed inserito un file in essa, quindi il file viene scaricato e ne viene mostrato il contenuto:

using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Util;
using Google.Apis.Services;
using Google.Apis.Download;
using System.Security.Cryptography.X509Certificates;
using Google.Apis.Upload;
using Google.Apis.Samples.Helper;
using System.Collections.Generic;
 
namespace GoogleDriveUploadFile
{
   class Program
   {
      private const String CLIENT_ID = "<YOUR CLIENTID>.apps.googleusercontent.com";
      private const String CLIENT_SECRET = "<YOUR_CLIENTSECRET>";
 
      private const String STORAGE = "<YOUR_STORAGE>";
      private const String KEY = "<YOUR_KEY>";
 
      private const String FILE_NAME = "document.txt";
      private const String DOWNLOAD_FOLDER_NAME = "download";
      private const String FILE_TITLE = "GoogleDriverTestDocument";
      private const String MIME_TYPE = "text/plain";
      private const String FILE_DESCRIPTION = "Google drive test file";
 
      ...

Catch unhandled thread exceptions

by host on venerdì 11 ottobre 2013 12:47
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
 
namespace CatchUnhandledThreadExceptions
{
   class Program
   {
      static void Main(string[] args)
      {
         AppDomain.CurrentDomain.UnhandledException += 
            new UnhandledExceptionEventHandler(MyHandler);
 
         new Thread(new ThreadStart(doWork)).Start();
 
         Console.WriteLine("Press ENTER to exit");
         Console.ReadLine();
      }
 
      static void MyHandler(object sender, UnhandledExceptionEventArgs args)
      {
         Console.WriteLine(((Exception)args.ExceptionObject).Message);
      }
 
      static void doWork()
      {
         throw new Exception("Secondary thread exception");
      }
   }
}

Drag and drop object between processes

by host on giovedì 10 ottobre 2013 13:11

Un semplice esempio che mostra come spostare oggetti mediante drag and drop tra applicazioni Windows Forms, l'oggetto deve essere prima serializzato a stringa mediante XmlSerializer (volendo potaveno essere utilizzati altri serializzatori disponibili in .NET), una volta serializzato l'oggetto puo' essere trasportato su altre applicazioni mediante i meccanismi di drag and drop presenti in Windows Forms per essere poi deserializzato e gestito nella applicazione di destinazione.

 

Associare un delegato mediante reflection

by host on giovedì 10 ottobre 2013 12:05

Associare un delegato tramite reflection In questo esempio viene mostrato come collegarsi ad un delegato utilizzando reflection.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
 
namespace ConnectToADelegateUsingReflection
{
   class Program
   {
      static void Main(string[] args)
      {
         MyEventGenerator myEventGenerator = new MyEventGenerator();
 
         EventInfo myEventInfo = typeof(MyEventGenerator).GetEvent("MyEvent");
 
         MethodInfo methodInfo = typeof(Program).GetMethod("OnMyEvent",
            BindingFlags.NonPublic | BindingFlags.Static);
 
         Delegate @delegate = Delegate.CreateDelegate(
            myEventInfo.EventHandlerType, methodInfo);
 
         myEventInfo.GetAddMethod()
            .Invoke(myEventGenerator, new Object[] { @delegate });
 
         Console.WriteLine("Press ENTER to exit");
         Console.ReadLine();
 
      }
 
      static void OnMyEvent(Object sender, EventArgs e)
      {
         Console.WriteLine("MyEvent received");
      }
   }
}

Tags