I looked forward to the release of “Mango” the new version of the O.S. for my Windows Phone 7 smartphone, and here it is at last! There are plenty of new features introduced by this new version now in beta, but always available for developers who want to take advantage in developing solutions that use the new features provided by the Development Team.
My main interest was the introduction of Sockets. Actually, ( and I think this is the strength of .NET Framework) I hadn’t to learn anything new but only implement also for WP7 the functionalities that I already used in other types of applications (Win forms, WPF, Micro Framework and Silverlight). The functionalities exposed by the most complete version of the framework (currently Version 4.0) are not always implemented in their entirety also for the reduced framework intended for different platforms than PC, but generally they don’t differ much from the reference version.
In the demo version I have implemented the use of Sockets to send commands ModBus RTU standard to a PLC home automation, which allow me to modify the register which controls the relay turning on the light of the office. In order to study the various standard please refer to the following references:
PLC –> http://en.wikipedia.org/wiki/Programmable_logic_controller
ModBus RTU Protocol –> http://en.wikipedia.org/wiki/Modbus; www.modbus.org
To put it simply what I have to do is to build an Array of Bytes of data in the ModBus RTU standard and send it to the PLC via Socket by indicating what is the status of the register set. The digital relays have been set to work like simple buttons ( press – turn on – press – turn off – press – turn on – press – turn off - ) I will always indicate to the register of the digital relay to set its own state to True (the same as saying “press the button”) because the PLC is programmed to automatically reset the status of the digital relay that has raised its switching on bit, lowering it after it has executed the command, just like a physical button that returns to the off position.
Otherwise, I would have to manually send a second ModBus command to reset the bit of the digital relay. Naturally, my home automation system involves a few devices connected to each other as the following layout:

The use of this type of Hardware involves the sending of the ModBus command using the UDP protocol so that the use of the sockets is limited only to the sending of the Array of Byte created by my class for the management of the ModBus protocol (not shown here) through a Socket object:
Private Sub SendPacket(PLCIPAddress As String, Port As Integer)
Dim Client As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim Packet As Byte() = ModBus.GetModBusBuffer(…)
If Client IsNot Nothing Then
Dim socketEventArg As New SocketAsyncEventArgs()
Dim Addr As IPAddress = IPAddress.Parse(PLCIPAddress)
socketEventArg.RemoteEndPoint = New IPEndPoint(Addr, Port)
AddHandler socketEventArg.Completed, AddressOf SendComplete
socketEventArg.SetBuffer(Packet, 0, Packet.Length)
Client.SendToAsync(socketEventArg)
End If
End Sub
Private Sub SendComplete(sender As Object, e As SocketAsyncEventArgs)
If e.SocketError <> SocketError.Success Then
‘Gestione eccezioni di comunicazione
End If
End Sub
Obviously, everything is seen in a very simple way because we can introduce several different implementations. First of all, the reading of the status of a register, or to check whether the light is on or off; to put a dimmer and to adjust the intensity of the light through a slider; to manage the thermostat in order to raise or to lower the temperature of the room; etc..etc..
Probably there will be other posts about the building automation, but for now I hope you get the idea of how it may be easy to develop apparently complex applications and how high are the business opportunity created by Microsoft technologies.
Do you fancy seeing the program in action?
How to turn on office lights with WP7
Alberto
At this link http://bit.ly/kcmiTQ you can found some videos about new Windows 8.
This new release of Microsoft’s OS it’s a very bigger paradigm jump, not only for developers but especially for the users!
Enjoy!
P.S. NOT TO BE MISSED!!
Alberto.
Another feature added to the Productivity Power Tools for Visual Studio 2010 is Organize Imports with simple mouse clicks. A simple right-click in the Code editor show a contextual menu "Organize Imports":

This allows us to remove unused Import, to sort them alphabetically or in a single command to perform both operations (removal and sorting).
See you!
Alberto.
SQL Server 2008 has been eliminated NO_LOG BACKUP command that could be useful to truncate the log files of large size.
BACKUP LOG [DatabaseName] WITH NO_LOG
Many developers need to compress the testing databases files to avoid getting the disk space used unnecessarily. By executing the following you can shrink to a minimum all the log files of a database server:
EXECUTE sp_msforeachdb
'USE ?;
ALTER DATABASE ? SET RECOVERY SIMPLE;
DECLARE @LogLogicalName nvarchar(100);
SELECT @LogLogicalName = file_name(2);
DBCC SHRINKFILE(@LogLogicalName, 100);
ALTER DATABASE ? SET RECOVERY FULL;'
“sp_msforeachdb” is a no-documented stored procedure that runs the script for all the db in your server.
Use with caution!
Alberto.
In Linq doesn’t exists the IN statment. In fact, the ability to filter the values for multiple occurrences not missing, it writes only in a different way, using a different approach. In this case, if you want to filter the results of a LINQ query, showing more than a filter value, it must build a collection of values to extract and use the Contains method of the collection itself. In other words if I want to extract all of a customers named "Rossi" and "Bianchi", I could just use this approach:
Sub Main()
Dim Coll As New List(Of Customer)
Coll.Add(New Customer With {.ID = 1, .FirstName = "Franco", .LastName = "Rossi"})
Coll.Add(New Customer With {.ID = 2, .FirstName = "Gianni", .LastName = "Verdi"})
Coll.Add(New Customer With {.ID = 3, .FirstName = "Lorenzo", .LastName = "Bianchi"})
Coll.Add(New Customer With {.ID = 4, .FirstName = "Gianmaria", .LastName = "Rossi"})
Coll.Add(New Customer With {.ID = 5, .FirstName = "Luca", .LastName = "Bianchi"})
Coll.Add(New Customer With {.ID = 6, .FirstName = "Vittorio", .LastName = "Rossi"})
Coll.Add(New Customer With {.ID = 7, .FirstName = "Filippo", .LastName = "Verdi"})
Dim FilterValue() As String = {"Rossi", "Bianchi"}
Dim Query = Coll.Where(Function(t) FilterValue.Contains(t.LastName))
For Each c As Customer In Query
…
HTH
Alberto
If you want to use Entity Framework generated by SQL 2008 on a SQL 2005 can be raise the error "type datetime2 not supported for the current version of SQL. " To work around this problem you must open the file edmx with the XML editor and change the value of ProviderManifestToken = "2008 " to ProviderManifestToken = "2005"
HTH
Alberto
In community someone asked how to count the numbers of a decimal number. Classic example of how you might use Linq in a single line of code:
Dim DoubleNumber As Double = 54637.3456789
Dim Count As Integer = DoubleNumber.ToString("f").Where(Function(c) Char.IsDigit(c)).Count
HTH
Alberto
If you need to extract an image from ImageList control use this code:
Bitmap = DirectCast(MyImageList.Images(0), Bitmap)
or if you need an icon:
Me.Icon = Drawing.Icon.FromHandle(DirectCast(MyImageList.Images(0), Bitmap).GetHicon)
Alberto