Archive

Archive for December, 2012

Regular Expression in Javascript

December 22nd, 2012

The tutorial for Regular Expression in Javascript has been uploaded. This tutorial is based upon the book "Regular Expression in Javascript and it fixes some of the issues in the book, including wrong answer to some of the question.

The online version allows you to make changes in the script online and check it. There will be an updated answer and explanation to the all the questions in the form of a working example.

Here are the First 10 Chapters uploaded so far.

Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10

Uncategorized

Syntax Highlighter for WordPress

December 19th, 2012

We just started using Syntax Highlighter for wordpress. As you may have seen, there are a number of our posts that relate to codes or programming and reading them without any highlights was a pain.

We are testing it here on the sample page.

$webite = "referencedesigner";

if ( $website == "referenncedesigner.com" ) {
	echo "You are visiting referencedesigner.com <br />";
}
echo "Welcome to referencedesigner!";

This however, did not give me the colored lines. So I am digging more into it.

Uncategorized

Using Syntax Highlighter for code highlighting

December 19th, 2012

If you write tutorials or if you need to display some code snippets, a highlighted code makes life much easy for the readers. We were recently looking at some solutions that exist on internet and came across a number of solution. One of the most promising one was free Syntax highlighter made available by Alex Gorbachev at his site. We had an opportunity to install and test this and we found it very helpful. But you need to take care of few thing to be able to make it useful. Here are some tips (other than those found scattered on internet).

1. To remove the question mark that appears on the top right hand side you need to include the following option.
toolbar: false

This will make the question mark and the link go away.

2. If you plan on using html code to be displayed use the script method in place of the pre method.

3. To make vertical scroll go away and instead introduce a horizontal scroll, add the following lines in the corresponding stylesheet.

Basically you need to add the

overflow-y: hidden !important;

to remove the vertical scroll and add

overflow-x: auto !important;

to introduce horizontal scroll ( automatic).

<?php
echo "Hello World";
?>

Uncategorized

Working with CSV file in C#.NET

December 15th, 2012

Let us say, we have a csv file with 5 columns and 4 rows. Since CSV file contains data that we often wish to work, we would go through this tutorial to see how we can read, access the CSV file data. Here is the test.csv file the we want to work on.

car

Here is the C#.NET code to accomplish this

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var reader = new StreamReader(File.OpenRead(@"C:\charp\test.csv"));

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                Console.WriteLine(line); 
            }

            Console.ReadLine();
        }
    }
}

There is nothing great in this program. We use the simple StreamReader access the file and then read line by line using Readline. Finally we print the line using Console.Writeline().

Let us now extend our extending. The elements in a row are separated by comma. Let us assume that we wish read and write the line that has car name as "Honda". Here is the program to accomplish that.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var reader = new StreamReader(File.OpenRead(@"C:\csharp\test.csv"));
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');
                if (values[1] == "Honda")
                {
                    Console.WriteLine(line);
                }
            }
            Console.ReadLine();
        }
    }
}

The line

 var values = line.Split(','); 

Splits the line and assigns the individual elements in the array values. The values[0] contain the first column element and the values[1] will contain the second row element.We compare if the car is Honda using the following

if (values[1] == "Honda") 

Exercise for Readers

Try to modify the code such that it will print the two lines that match to two given Car names. Here is the solution

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var reader = new StreamReader(File.OpenRead(@"C:\projects_vikas\Vikas_websites\compare-processor\csharp\test.csv"));
string processor1_input = "Toyota";
string processor2_input = "Honda";
string processor1 ="";
string processor2 ="";
int number_of_matches = 0;
int i;

while ((!reader.EndOfStream) && number_of_matches <=1)
{
var line = reader.ReadLine();
var values = line.Split(',');
if( (values[1] == processor1_input) || (values[1] == processor2_input))
{
if (number_of_matches == 0) processor1 = line; else processor2 = line;
number_of_matches++;
//Console.WriteLine(line);
}
}

for (i=0; i<number_of_matches; i++)
{
if (i == 0) Console.WriteLine(processor1); else Console.WriteLine(processor2);
}

Console.ReadLine();
}
}
}

Uncategorized

Drawing a line on a png image in Visual C#

December 13th, 2012

We are trying to write a small code that will draw a line on a 640 x 480 blank image. It will open an existing png file and and then draw a line on it. The program will be compiled and run using Microsoft's Visual C# Express edition.

Some practical application - If you have a bunch of Files ( jpg or png), you can use this program to draw a border around it. You can use this idea to write a text ( as signature ) on the right side of the images.

Here is the code to do it.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

var img = Bitmap.FromFile(@"C:\tutorials\img.png");
using(var g = Graphics.FromImage(img))
{
g.DrawLine(Pens.Red, new Point(10, 10), new Point(100, 100));
}
img.Save(@"C:\tutorials\img2.png");
}
}
}

If you see the error that

type or namespace name 'Drawing' does not exist in the namespace 'System' (are you missing an assembly reference?)

You will need to dd a "reference" to the System.Drawing.dll assembly. Use the Project, Add Reference... menu command to add a reference to this DLL.

When you make a Console Application, by default Visual Studio does not expect you to be doing graphics so it does not add a "reference" to the System.Drawing.dll assembly.

Once the code runs you should see a file called img2.png in the directory C:/tutorial. You can see other C# tutorials at here .

Uncategorized

RS232 Serial Port Pin Numbering

December 10th, 2012

Many embedded engineers have to work on Serial Ports. In fact serial port still is a dominant mode of communication between devices in embedded systems even after the advent of USB and many other media.

The purpose of this blog is to mention a very simple way to memorise the pin numbering of the Serial Port pin numbering in the DB9 Connector of the Serial Port. I am not going to mention what all the 9 pins of the serial port connector do that. There are other places including wiki where you can get that information. I am also not goint to mention what all the serial port protocol is.

All I am going to mention is that most serial port communication consist of just three pins - A ground, a Transmitter and a Receiver. So Pin 5 in the DB9 is the ground. And if you do not remember it you should. You will often need this pin to be able to measure and check your signal on your scope. That leaves us with two more Pins - A transmitter and a Receiver. The Transmitter is Pin 3 and Receiver is Pin 2.

In your computer the Transmitter is Pin 3. And that is all you need to remember. And here is an easy way to remember - both the words Transmitter and Three has the alphabets ‘T’ and ‘r’. And with this fact memorized you know that the receiver is going to be Pin 2.

And of course, if you still have confusion, hook up a Oscilloscope and attach the ground of the Oscilloscope to Pin 5. Now connect the tip of the oscilloscope probe to Pin 3. If you now open a Hyperterminal and write anything in your Hyperterminal you should see some data on that Pin 3 of the DB9 Connector.

Uncategorized