Archive

Archive for the ‘C#.NET’ Category

Execute a method every time interval automatically c#

November 8, 2013 Leave a comment

To call a method automatically for every time interval, we can use timers in C#

See the code below.

class Program
{
public static System.Timers.Timer aTimer;
static void Main(string[] args)
{
aTimer = new System.Timers.Timer(10000);
aTimer.Elapsed += new ElapsedEventHandler(RunThis);
aTimer.AutoReset = true;
aTimer.Enabled = true;
Console.ReadLine();
}
private static void RunThis(object source, ElapsedEventArgs e)
{
Console.WriteLine("Print this in every 10 seconds");
}
}

Happy coding 🙂

Advertisements

How to set up virtual port windows 7 c#

July 25, 2013 Leave a comment

Its really cool to have an emulator to set up virtual port in windows 7/XP

A null modem emulator, get it from here

http://sourceforge.net/projects/com0com/

You need to make paired ports to test the data send/receive

Follow the steps below to set up and test the virtual port.

1. Install the null modem emulator (com0com) as Administrator

2. Run the Windows 7 command prompt as administrator

3. Type the following command and hit enter

bcdedit.exe -set TESTSIGNING ON

4. Reboot the computer

5. Open the program com0com setup to configure virtual ports

com0com

6. Create paired ports, So we can test the data transfer. If any data we push to COMA, we can get it at COMB

7. Useful program to test the data transfer between ports is hyperterminal. Unfortunately its not in the Windows 7, its in XP i guess.

You can get the hyperterminal here http://sdrv.ms/15NJAMo

8. You can test the data from the hyperterminal as below

hyper

If you type something in COM A, you can see the text in COM B and vice versa.

Also you can manage the virtual ports from your c# code with SerialPort tool as well.

Happy coding…:):)

Deserialize google places json api using JSON.NET C#

June 23, 2013 1 comment

https://maps.googleapis.com/maps/api/place/search/json?location=8.487495,76.948623&radius=50000&sensor=false&key=Your_key&types=bank

The above link will list all banks near trivandrum in json format (around 50km).

The format is

{
“debug_info” : [],
“html_attributions” : [
“Listings by \u003ca href=\”http://www.indiacom.com/\”\u003eIndiacom Yellow Pages\u003c/a\u003e”
],
“next_page_token” : “ClRNAAAAiET0Jb-GWSPRrClKlXoDqUzA70eEBaa2oKXpN0uBnZ0rVeGkmia0x8IT78UKkXnJZ9IKDhXRsydMw-20Qu1vFf0U5WHKd_Vyt6OwA6etDC8SENOghFC6xxe63lx7VL7i5O4aFGrN8gtl_GaPmBQlqcQGYUdnZoao”,
“results” : [
{
“geometry” : {
“location” : {
“lat” : 8.5280190,
“lng” : 76.9387870
}
},
“icon” : “http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png”,
“id” : “03d8927f980ecbe599326209ee5018ebbf1d54e6”,
“name” : “Syndicate Bank”,
“reference” : “CnRtAAAA_LbcYBdQ5KDQqNoHxaT2YLBfU5H6cnoi5MgcaFLHcGQQH6EAMKeNzi8KPtNjoTE1d1nBftN0a100yFJ5V103RWO6zZgfsJCb1D33A8DIyr6DyBm4CXOhjECWv5RPf3oriP2WbmQ5U5o8JzloTFApaBIQRZlVXmu5Npmx9SvoQ4a7kBoUzlHi7IgB3JtUAXo8WWCPhi_VQRI”,
“types” : [ “bank”, “finance”, “establishment” ],
“vicinity” : “Kesavadasapuram, Thiruvananthapuram”
}

],
“status” : “OK”
}

You can deserialize in to objects using JSON.NET

The class format should be

public class GooglePlacesResponse
{
public string status { get; set; }
public results[] results { get; set; }
}

public class results
{
public geometry geometry { get; set; }
public string name { get; set; }
public string reference { get; set; }
public string vicinity { get; set; }
}

public class geometry
{
public location location { get; set; }
}

public class location
{
public string lat { get; set; }
public string lng { get; set; }
}

Download json string and Deserialize using JsonConvert class in JSON.NET

WebClient wc = new WebClient();
string jsonStr = wc.DownloadString("https://maps.googleapis.com/maps/api/place/search/json?location=" + currentLoc + "&radius=50000&sensor=false&key=Your_Key&types=" + value);
GooglePlacesResponse gpr = (GooglePlacesResponse)JsonConvert.DeserializeObject<GooglePlacesResponse>(jsonStr);

Image
Happy deserializing:)