Jun 27, 2010

All of unlocking HDD Password

Unlocking a password protected harddisk

During development of the Rockbox firmware, on several occations the harddisk has become locked, i.e. password protected. This results in the Archos displaying:

Part. Error
Pls Chck HD

We are still not 100% sure why it happened. Theories range from low-power conditions to accidental chip select failure. It has also happened for normal users, using the standard Archos-supplied firmware, although it was more frequent for us developers.

Note: None of us developers have experienced this problem since march 2002.

We do however know how to unlock the disk:

Windows/DOS unlock

Note: This requires taking the Archos apart, which will void your warranty!

  1. Grab atapwd (written by Alex Mina)
  2. Create a bootable DOS floppy disk, and put atapwd.exe on it
  3. Remove the harddisk from your Archos and plug it into a laptop (or a standard PC, using a 3.5" => 2.5" IDE adapter)
  4. Boot from the floppy and run atapwd.exe
  5. Select the locked harddrive and press enter for the menu
  6. For Fujitsu disks: Choose "unlock with user password", then "disable with user password". The password is empty, so just press enter at the prompt.
  7. For Toshiba and Hitachi disks, if the above doesn't work: Choose "unlock with master password", then "disable with master password". The password is all spaces.
  8. Your disk is now unlocked. Shut down the computer and remove the disk.

Big thanks to Magnus Andersson for discovering the Fujitsu (lack of) user password!

There is also a program for win32, ArchosUnlock.exe, that creates a linux boot disk with the below mentioned patched isd200 driver.

Linux unlock

For those of us using Linux, we have written an isd200 driver patch for unlocking the disk. This modified driver will automatically unlock the disk when you connect your Archos via USB, so you don't have to do anything special. Apply the patch to a 2.4.18 linux kernel tree.

Still locked?

If the above suggestions don't work, here's some background info about the disk lock feature:

The disk lock is a built-in security feature in the disk. It is part of the ATA specification, and thus not specific to any brand or device.

A disk always has two passwords: A User password and a Master password. Most disks support a Master Password Revision Code, which can tell you if the Master password has been changed, or it it still the factory default. The revision code is word 92 in the IDENTIFY response. A value of 0xFFFE means the Master password is unchanged.

A disk can be locked in two modes: High security mode or Maximum security mode. Bit 8 in word 128 of the IDENTIFY response tell you which mode your disk is in: 0 = High, 1 = Maximum.

In High security mode, you can unlock the disk with either the user or master password, using the "SECURITY UNLOCK DEVICE" ATA command. There is an attempt limit, normally set to 5, after which you must power cycle or hard-reset the disk before you can attempt again.

In Maximum security mode, you cannot unlock the disk! The only way to get the disk back to a usable state is to issue the SECURITY ERASE PREPARE command, immediately followed by SECURITY ERASE UNIT. The SECURITY ERASE UNIT command requires the Master password and will completely erase all data on the disk. The operation is rather slow, expect half an hour or more for big disks. (Word 89 in the IDENTIFY response indicates how long the operation will take.)

Apr 10, 2010

Dotnet framework 3.5 SP1 as prerequisite

Q:
i have vs.net 2008 and i have installed sp1 ( for vs and the .net framework3.5) but when i try to add the .net framework 3.5 sp1 as a prerequisite i go the following errors when i build the setup program

there are 57 serrors : here are some sample

Error1The install location for prerequisites has not been set to 'component vendor's web site' and the file 'DotNetFX35SP1\dotNetFX20\aspnet.msp' in item '.NET Framework 3.5 SP1' can not be located on disk. See Help for more information.C:\Documents and Settings\Sami\My Documents\Visual Studio 2008\Projects\WindowsApplication1\Setup1\Setup1.vdprojSetup1

A:
The reason is that we shipped the .NET Frameworks 3.5 SP1 package as Homesite Only, i.e. only supports "Install Prerequisites from Component Vendor's Website". We did this to keep the size of VS 2008 SP1 from being too large.

We did put info in the SP1 readme on how to get the files put on your machine to enable the samesite scenario. Look for "Enable Samesite".

http://download.microsoft.com/download/A/2/8/A2807F78-C861-4B66-9B31-9205C3F22252/VS2008SP1Readme.htm

Enable Samesite for the .NET Framework 3.5 SP1 bootstrapper package

If the .NET Framework 3.5 SP1 bootstrapper package is selected in the Prerequisite dialog box for a Setup project or in ClickOnce publishing, and also the "Download prerequisites from the same location as my application" option is selected, the following build error is shown:

The install location for prerequisites has not been set to 'component vendor's web site' and the file 'dotNetFx35setup.exe' in item 'Microsoft.Net.Framework.3.5.SP1' cannot be located on disk.

To resolve this issue:

Update the Package Data

1. Open the [Program Files]\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder or %ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems
2. Edit the Product.xml file in Notepad.
3. Paste the following into the < PackageFiles > element:



4. Find the element for < PackageFile Name="dotNetFX30\XPSEPSC-x86-en-US.exe" and change the PublicKey value to: 3082010A0282010100A2DB0A8DCFC2C1499BCDAA3A34AD23596BDB6CBE2122B794C8EAAEBFC6D526C232118BBCDA5D2CFB36561E152BAE8F0DDD14A36E284C7F163F41AC8D40B146880DD98194AD9706D05744765CEAF1FC0EE27F74A333CB74E5EFE361A17E03B745FFD53E12D5B0CA5E0DD07BF2B7130DFC606A2885758CB7ADBC85E817B490BEF516B6625DED11DF3AEE215B8BAF8073C345E3958977609BE7AD77C1378D33142F13DB62C9AE1AA94F9867ADD420393071E08D6746E2C61CF40D5074412FE805246A216B49B092C4B239C742A56D5C184AAB8FD78E833E780A47D8A4B28423C3E2F27B66B14A74BD26414B9C6114604E30C882F3D00B707CEE554D77D2085576810203010001
5. Find the element for < PackageFile Name="dotNetFX30\XPSEPSC-amd64-en-US.exe" and change the PublicKey value to the same as in step 4 above
6. Save the product.xml file



Download and Extract the Core Installation Files

1. Navigate to the following URL: http://go.microsoft.com/fwlink?LinkID=118080
2. Download the dotNetFx35.exe file to your local disk.
3. Open a Command Prompt window and change to the directory to which you downloaded dotNetFx35.exe.
4. At the command prompt, type:
dotNetFx35.exe /x:.
This will extract the Framework files to a folder named “WCU” in the current directory.
5. Copy the contents of the WCU\dotNetFramework folder and paste them in the %Program Files%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder (%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems). Note: Do not copy the WCU\dotNetFramework folder itself. There should be 5 folders under the WCU folder, and each of these should now appear in the DotNetFx35SP1 folder. The folder structure should resemble the following:
o DotNetFx35SP1 (folder)
* dotNetFX20 (folder
* dotNetFX30 (folder)
* dotNetFX35 (folder)
* dotNetMSP (folder)
* TOOLS folder)
* en (or some other localized folder)
* dotNetFx35setup.exe (file)



You may now delete the files and folders you downloaded and extracted in steps 2 and 4.

Apr 6, 2010

Sending a bit image to an Epson TM-T88III receipt printer using C# and ESC/POS

by Nicholas Piasecki on December 9th, 2009

At Skiviez/WFS, our “low-volume fulfillment requests” (which usually simply means “retail orders”) get a simple receipt printed out via an Epson TM-T88III receipt printer.

TM-T88III Receipt Printer

TM-T88III Receipt Printer

You’ve probably seen the TM-T88III before, though the current model is the TM-T88IV–they’re ubiquitous and especially common in restaurants. We chose it because the receipt prints quickly (which reduces the probability that a stack of order receipts gets missorted such that an employee puts the wrong receipt in the wrong parcel), is thermal so there is no ink to replace (which reduces the cost of consumables), uses standard thermal receipt paper (which is cheap, and we can always run out to Staples if we run out), and, perhaps most importantly, we found one in a box in the back of the warehouse.

At the top of the retail receipt, I print out the Skiviez logo. This is stored in a non-volatile area of memory in the printer (called NV RAM in EPSON parlance), and I dole out a command that essentially says “print out the image stored in slot #1,” having previously uploaded the bitmap via the printer driver’s flash utility. This worked great for as long as we only shipped Skiviez orders and only had one TM-T88III printer. But with the advent of WFS, however, Skiviez now ships orders (we call them “fulfillment requests”) for multiple e-commerce stores, each requiring a different logo to appear at the top of the receipt. So I now had two options:

  • I could manually upload the logos for all of the merchants that we ship for into all of the receipt printers that we use, making sure to upload each merchant image into the same NV memory slot for each printer.
  • I could store the merchant’s logo in our database, figure out how to use the “select bit image mode” receipt printer command, and just send the image data just-in-time whenever I generated a receipt.

The second option has obvious maintenance benefits, so that’s the path I started hacking my way down.

A brief primer on ESC/POS and data

Like the Zebra LP2844 and its brethren, the TM-T88III comes with a Windows printer driver that allows Windows and applications to see it as any other GDI-based printer. You can print a Word document to the TM-T88III and it will print, albeit slowly, and it’ll look like crap. That’s because, as with any other GDI printer, the Word document gets rendered as a bitmap and the entire big-ass bitmap gets sent to the poor little receipt printer which then prints it 24 dots at a time. A better method is to use the printer for its intended purpose and use the native printer language to generate the receipt. This allows us to select printer fonts that are specifically designed for the printer’s resolution and speed characteristics and use other advanced features of the printer such as the paper cutter.

That printer language is called ESC/POS, which is entirely unpronounceable and a stupid name. The “POS” stands for “Point of Sale” since this printer belongs to a class of devices commonly known as point of sale devices–cash drawers, VFDs, barcode scanners, and the like. The “ESC” stands for “escape” because the printer treats any data that is sent to it as text–passed straight through–unless it is escaped with a special character to indicate that a command instruction follows in the input stream. The most common escape character used is the ASCII ESC character, and so we have “ESC/POS” as our language name.

If you’ve just picked up one of these printers on eBay, you may find that getting documentation on this language is hard to come by because you need to get a copy of the “ESC/POS Application Programming Guide” from your “EPSON Authorized Dealer,” whoever the hell that is. (Again, we found ours in a box in the back of the warehouse, so my “authorized dealer” is long gone.) Still, some Googling will give you more or less complete and current copies of these guides, which I’ve mirrored here. Go ahead and download them:

(I particularly like how the programming guide has “proprietary” and “confidential” stamped all over it and then proceeds to describe how great EPSON is for “taking initiative” for “expandability” and “universal applicability” on page 7 of the same document. You can’t make this crap up–it’s full of oxymorons.)

Having just come from writing in the EPL2 printer language for our Zebra LP2844 and Zebra ZP 500 Plus thermal label printers, I found the ESC/POS language to be confusing. My confusion mostly stemmed partly from the way the documentation was written and partly because I was used to the way EPL2 worked. Let’s take a look at the documentation for the bit image command that I’m trying to use:

Documentation Snipped

If this were EPL2, I would actually send the document as a string to the printer, so if a command needed the integer 33 as a parameter, I would send the string “33″ (two bytes of data). In ESC/POS, each parameter is a single byte, so if I want to send 33 as a parameter for m in the bit image command above, then I need to send 33 in a single byte: that is, 0b100001, which is 25 + 20 = 32 + 1 = 33.

The reason that this is super confusing is that other parameters in the documentation are specified to be ASCII characters, such as the ‘*’ parameter in the bit image command above. This is because low-level programmers, such as those who designed the ESC/POS language, tend to blur the lines between data types: it’s all bytes at the end of the day. As a result, if you’re using C#, you might be tempted to use a StringBuilder to build up your document to send to the printer. Don’t do it! You’ll inevitably get confused by its overloads. Let’s take that m = 33 parameter as an example:

var sb = new StringBuilder();


// ASCII escape
sb.Append((char)27);

// ASCII '*' for the bit image command
sb.Append('*');

// oops! this appends the string "33" in two bytes, doesn't work
sb.Append(33);

// this is what I want, but semantically weird
sb.Append((char)33);

A much better and less confusing in the long run solution is to eschew any notion that we are dealing with text. Instead, let’s use the semantics that we are sending bytes of raw binary data directly to the printer:

using (var ms = new MemoryStream())

using (var bw = new BinaryWriter(bw))
{
bw.Write(AsciiControlChars.Escape);
bw.Write('*'); // bit-image mode
bw.Write((byte)33); // 24-dot double-density
}

But remember that cast to byte! Otherwise, you’ll get a C# int, which is four bytes, written to the stream, when we only wanted one. Yes, that means that any parameter that an ESC/POS command takes has a maximum value of 255.

You could get by on the StringBuilder method for a while and not have it burn you until you try the bit image command because all the characters that you’re appending happen to be less than 128–greater than that and you start getting into bytes that don’t map to a Unicode character for quirky historical reasons, such as the range 128 to 159, and you’ll be pulling your hair out as to why some of your data is getting “lost.” Just use the BinaryWriter method. You can thank me later.

Why is the bit image command designed this way?

The m parameter in the bit image command as defined in the previous section has 4 values, and each value changes the way we construct the image data bytes and the way the printer interprets them. The first three values are of dubious value, so let’s throw them out for now. We’ll focus on m = 33, which means “24-dot double density mode.” So keep this in mind and set this aside.

When we think of drawing a bitmap in high level software, we usually think of drawing it pixel by pixel, left to right, top to bottom. So the order that the dots get printed as indicated by the programming guide seems strange:

Relationship between image data and the print result

That is, the dots are rendered from top to bottom, then left to right.

Why in the blue hell do I need to rearrange my nice and neat array of bitmap data into this Connect Four scheme of dots? It all comes down to the size of the print head.

For our mental model, the thermal print head in the receipt printer is physically 1 dot wide by 24 dots high where there are 203 dots per inch (square pixels). It moves left to right across the receipt paper, burning up to 24 dots in the vertical (y) direction for each dot in the horizontal (x) direction.

Since we’re talking about a thermal printer here, there’s no concept of gray scale tones–we’ve either burned a dot into the paper (black) or we haven’t. So if we’re sending image data to the printer in bytes, it would make sense to say that a bit set to 1 means “burn a dot” and a bit set to “0″ means do nothing.

But a byte is only 8 bits wide, and we have 24 dots to burn in each “slice” of the image. 24 just happens to be divisible by 8, so we can send 3 bytes of data for each slice to represent our 24 dots. (If the bitmap I want to draw is taller than 24 dots/pixels, then I need to send multiple bit image commands, effectively doing multiple stripes of the image that are 24 dots high; more on this later.)

So, now, the diagram in the programming guide makes sense: we’re not burning 1 dot at time, we’re burning a vertical stripe that is 1 dot wide and 24 dots high at a time, and then moving to the right. So our printer reads our first 3 bytes of image data, burns the dots specified by those 24 bits, then reads the next 3 bytes, and so on.

Aside: Converting the bitmap to monochrome

Right. So a merchant has given me a *.bmp file, and I need to convert that into array of bits that I can send to the receipt printer. It’s a good bet that the bitmap that the merchant sent me for the logo is not monochrome (that is, every pixel is either 100% black or 100% white).

So what we can do is look at each pixel in the bitmap, determine its luma, and if that’s below a certain threshold, count that pixel as black. How did I write the code to do this? I looked the formula up via the intertubes and modified it to fit my needs:

private static BitmapData GetBitmapData(string bmpFileName)

{
using (var bitmap = (Bitmap)Bitmap.FromFile(bmpFileName))
{
var threshold = 127;
var index = 0;
var dimensions = bitmap.Width * bitmap.Height;
var dots = new BitArray(dimensions);

for (var y = 0; y < bitmap.Height; y++)
{
for (var x = 0; x < bitmap.Width; x++)
{
var color = bitmap.GetPixel(x, y);
var luminance = (int)(color.R * 0.3 + color.G * 0.59 + color.B * 0.11);
dots[index] = (luminance < threshold);
index++;
}
}

return new BitmapData()
{
Dots = dots,
Height = bitmap.Height,
Width = bitmap.Width
};
}
}

BitmapData is just a little struct that contains the three properties that you see here. After all, once I have my BitArray with 1 indicating black dots and 0 indicating white dots, I don’t need the original Bitmap instance anymore.

Just to clarify, this means that we’re holding onto a BitArray of monochrome data in the Dots property that conceptually looks something like this:

Conceptual Overview

Marshalling the monochrome data

Now for the trickiest part of all. We need to take our BitArray of monochrome data, divide it up into 8-dot chunks, represent those 8-dot chunks as bytes, and send them to the printer in the order required by the bit image command, discussed above. That is, I’ll need to send the data as bytes in this order:

Capture

So the printer will draw them one vertical stripe of three bytes each at a time. (If you don’t get it, print out that image, cut up the bytes, and then arrange them in the order shown in the diagram from the EPSON documentation. It’ll instantly make sense once you see it.) The X’s in the diagram correspond to bits that aren’t in our original image that we have to send anyway as padding. Remember, since we selected the 24-dot double density mode, the printer is going to draw a 1 x 24 dot slice as it moves the print head. My example bitmap is only 4 pixels tall, so I have to send 20 zero bits as padding.

And thus we would end up with our pretty image.

Unfortunately, there is some math involved in translating the bits in our BitArray into these bytes that we need to send. Assuming bw is our reference to a BinaryWriter, here’s the code that does just that:

// So we have our bitmap data sitting in a bit array called "dots."

// This is one long array of 1s (black) and 0s (white) pixels arranged
// as if we had scanned the bitmap from top to bottom, left to right.
// The printer wants to see these arranged in bytes stacked three high.
// So, essentially, we need to read 24 bits for x = 0, generate those
// bytes, and send them to the printer, then keep increasing x. If our
// image is more than 24 dots high, we have to send a second bit image
// command to draw the next slice of 24 dots in the image.

// Set the line spacing to 24 dots, the height of each "stripe" of the
// image that we're drawing. If we don't do this, and we need to
// draw the bitmap in multiple passes, then we'll end up with some
// whitespace between slices of the image since the default line
// height--how much the printer moves on a newline--is 30 dots.
bw.Write(AsciiControlChars.Escape);
bw.Write('3'); // '3' just means 'change line height command'
bw.Write((byte)24);

// OK. So, starting from x = 0, read 24 bits down and send that data
// to the printer. The offset variable keeps track of our global 'y'
// position in the image. For example, if we were drawing a bitmap
// that is 48 pixels high, then this while loop will execute twice,
// once for each pass of 24 dots. On the first pass, the offset is
// 0, and on the second pass, the offset is 24. We keep making
// these 24-dot stripes until we've run past the height of the
// bitmap.
int offset = 0;

while (offset < data.Height)
{
// The third and fourth parameters to the bit image command are
// 'nL' and 'nH'. The 'L' and the 'H' refer to 'low' and 'high', respectively.
// All 'n' really is is the width of the image that we're about to draw.
// Since the width can be greater than 255 dots, the parameter has to
// be split across two bytes, which is why the documentation says the
// width is 'nL' + ('nH' * 256).
bw.Write(AsciiControlChars.Escape);
bw.Write('*'); // bit-image mode
bw.Write((byte)33); // 24-dot double-density
bw.Write(width[0]); // width low byte
bw.Write(width[1]); // width high byte

for (int x = 0; x < data.Width; ++x)
{
// Remember, 24 dots = 24 bits = 3 bytes.
// The 'k' variable keeps track of which of those
// three bytes that we're currently scribbling into.
for (int k = 0; k < 3; ++k)
{
byte slice = 0;

// A byte is 8 bits. The 'b' variable keeps track
// of which bit in the byte we're recording.
for (int b = 0; b < 8; ++b)
{
// Calculate the y position that we're currently
// trying to draw. We take our offset, divide it
// by 8 so we're talking about the y offset in
// terms of bytes, add our current 'k' byte
// offset to that, multiple by 8 to get it in terms
// of bits again, and add our bit offset to it.
int y = (((offset / 8) + k) * 8) + b;

// Calculate the location of the pixel we want in the bit array.
// It'll be at (y * width) + x.
int i = (y * data.Width) + x;

// If the image (or this stripe of the image)
// is shorter than 24 dots, pad with zero.
bool v = false;
if (i < dots.Length)
{
v = dots[i];
}

// Finally, store our bit in the byte that we're currently
// scribbling to. Our current 'b' is actually the exact
// opposite of where we want it to be in the byte, so
// subtract it from 7, shift our bit into place in a temp
// byte, and OR it with the target byte to get it into there.
slice |= (byte)((v ? 1 : 0) << (7 - b));
}

// Phew! Write the damn byte to the buffer
bw.Write(slice);
}
}

// We're done with this 24-dot high pass. Render a newline
// to bump the print head down to the next line
// and keep on trucking.
offset += 24;
bw.Write(AsciiControlChars.Newline);
}

// Restore the line spacing to the default of 30 dots.
bw.Write(AsciiControlChars.Escape);
bw.Write('3');
bw.Write((byte)30);

This code looks confusing, and is, but I’ve tried to document it with explanatory comments in line.

Sending the document to the printer

Finally, there is the task of sending this array of bytes directly to the printer. I’ve talked about this before and the code is uninteresting, requiring some P/Invokes into native APIs. Essentially, you pass in the name of the printer as it appears in the Control Panel, the array of bytes from our BinaryWriter, and you’re all set:

private static void Print(string printerName, byte[] document)

{
NativeMethods.DOC_INFO_1 documentInfo;
IntPtr printerHandle;

documentInfo = new NativeMethods.DOC_INFO_1();
documentInfo.pDataType = "RAW";
documentInfo.pDocName = "Bit Image Test";

printerHandle = new IntPtr(0);

if (NativeMethods.OpenPrinter(printerName.Normalize(), out printerHandle, IntPtr.Zero))
{
if (NativeMethods.StartDocPrinter(printerHandle, 1, documentInfo))
{
int bytesWritten;
byte[] managedData;
IntPtr unmanagedData;

managedData = document;
unmanagedData = Marshal.AllocCoTaskMem(managedData.Length);
Marshal.Copy(managedData, 0, unmanagedData, managedData.Length);

if (NativeMethods.StartPagePrinter(printerHandle))
{
NativeMethods.WritePrinter(
printerHandle,
unmanagedData,
managedData.Length,
out bytesWritten);
NativeMethods.EndPagePrinter(printerHandle);
}
else
{
throw new Win32Exception();
}

Marshal.FreeCoTaskMem(unmanagedData);

NativeMethods.EndDocPrinter(printerHandle);
}
else
{
throw new Win32Exception();
}

NativeMethods.ClosePrinter(printerHandle);
}
else
{
throw new Win32Exception();
}
}

The P/Invoke declarations can be found in the sample code at the end of the article, and the documentation is available on MSDN. But, again, there’s nothing sexy going on here.

Conclusions and delusions

And that’s a wrap. I always find it interesting when I encounter a problem at work–which is a small business by any definition of the phrase–and the Google indicates that no one has ever encountered or documented this problem before. And it’s these types of integration problems that I find fascinating; after years of playing in castles in the sky, dealing with databases and exceptions and HTML forms, it’s nice to know that I can dust off the corners of my brain and my college degree and deal with bits and bytes when I need to. Writing software that makes hardware do stuff is always fun, too.

And after hours of watching the receipt printer spew out lines and lines of gibberish, there’s nothing like the feeling of seeing the bitmap print out and knowing that, finally, things have simply started to work.

Good luck!

Download the code used in this article.

Mar 21, 2010

Phương pháp luyện nghe của một thầy giáo biết 6 ngôn ngữ

Nói về phương pháp luyện nghe thì chúng ta cũng đã nói nhiều, nhưng mình thấy cách luyện nghe dưới đây của một thầy giáo - thầy duynhien - người nhiều năm là giảng viên ở một trường đại học lớn, rất đặc biệt, nên trích ra để các bạn tham khảo.

LÀM SAO NGHE ĐƯỢC TIẾNG ANH

(Và nói chung: MỘT NGOẠI NGỮ)


Một trong những trở ngại lớn nhất của chúng ta khi học một ngoại ngữ ấy là chúng ta quá… thông minh và có quá nhiều kinh nghiệm.


Quá thông minh: vì mình không thể nào chấp nhận nghe một câu mà mình không hiểu: cần phải hiểu một câu nói gì trước khi nghe tiếp câu thứ hai, nếu không thì mình không buồn nghe tiếp.

Quá kinh nghiệm: Cuộc đời đã dạy ta không nghe những gì người khác nói mà chỉ hiểu những gì mà nội dung chuyển tải. Nếu không hiểu nội dung, chúng ta không thể lặp lại lời người kia. Cũng vì thế mà - trong giai đoạn đầu học ngoại ngữ - mỗi lần nghe một câu tiếng Anh thì trong đầu phải dịch ra được tiếng Việt thì mới yên tâm, bằng không thì … câu ấy không có nghĩa.

Thế nhưng, đấy là lối học sinh ngữ ngược chiều. Tôi biết được 6 ngôn ngữ, trong đó có ba ngôn ngữ thành thạo nghe nói đọc viết: Việt - Anh - Pháp, và tôi thấy rằng trong các ngôn ngữ tôi biết thì, một cách khách quan, nghe và nói tiếng Việt là khó nhất (vì ở phương tây, không có ngôn ngữ nào mà mình đổi cao độ của một từ thì ý nghĩa từ ấy lại thay đổi: ma - má - mà - mạ - mã - mả). Nhưng các bạn ở forum này, cũng như tôi, đều không có vấn đề gì cả với cái sinh ngữ khó vào bậc nhất ấy!


Thế nhưng những thầy cô dạy chúng ta nghe nói tiếng Việt chẳng phải là những vị chuyên viên ngôn ngữ như các thầy ngoại ngữ mà ta học tại các trường. Thầy dạy tiếng Việt chúng ta là tất cả những người quanh ta từ ngày ta ra đời: cha mẹ, anh chị, hàng xóm, bạn bè… nghĩa là đại đa số những người chưa có một giờ sư phạm nào cả, thậm chỉ không có một khái niệm nào về văn phạm tiếng Việt. Thế mà ta nghe tiếng Việt thoải mái và nói như sáo. Còn tiếng Anh thì không thể như thế được. Ấy là vì đối với tiếng Việt, chúng ta học theo tiến trình tự nhiên, còn ngoại ngữ thì ta học theo tiến trình phản tự nhiên.


Từ lúc sinh ra chúng ta đã nghe mọi người nói tiếng Việt chung quanh (mà chẳng bao giờ ta phản đối: "tôi chẳng hiểu gì cả, đừng nói nữa"! Mới sanh thì biết gì mà hiểu và phản đối!). Sau một thời gian dài từ 9 tháng đến 1 năm, ta mới nói những tiếng nói đầu tiên (từng chữ một), mà không hiểu mình nói gì. Vài năm sau vào lớp mẫu giáo mới học đọc, rồi vào lớp 1 (sáu năm sau khi bắt đầu nghe) mới tập viết… Lúc bấy giờ, dù chưa biết viết thì mình đã nghe đưọc tất cả những gì người lớn nói rồi (kể cả điều mình chưa hiểu). Như vậy, tiến trình học tiếng Việt của chúng ta là Nghe - Nói - Đọc - Viết. Giai đoạn dài nhất là nghe và nói, rồi sau đó từ vựng tự thêm vào mà ta không bao giờ bỏ thời gian học từ ngữ. Và ngữ pháp (hay văn phạm) thì đến cấp 2 mới học qua loa, mà khi qua hết trung học thì ta đã quên hết 90% rồi.


Nhưng tiến trình ta học tiếng Anh (hay bất cứ ngoại ngữ nào) thì hoàn toàn ngược lại.

Thử nhìn lại xem: Trước tiên là viết một số chữ và chua thêm nghĩa tiếng Việt nếu cần. Và kể từ đó, học càng nhiều từ vựng càng tốt, kế đến là học văn phạm, rồi lấy từ vựng ráp vào cho đúng với văn phạm mà VIẾT thành câu! Rồi loay hoay sửa cho đúng luật! Sau đó thì tập đọc các chữ ấy trúng được chừng nào hay chừng ấy, và nhiều khi lại đọc một âm tiếng Anh bằng một âm tiếng Việt! (ví dụ fire, fight, five, file… đều được đọc là ‘phai’ ). Sau đó mới tới giai đoạn NÓI, mà ‘nói’ đây có nghĩa là Đọc Lớn Tiếng những câu mình viết trong đầu mình, mà không thắc mắc người đối thoại có hiểu 'message' của mình hay không vì mình chỉ lo là nói có sai văn phạm hay không. Lúc bấy giờ mới khám phá rằng những câu mình viết thì ai cũng hiểu, như khi mình nói thì chỉ có mình và … Thượng Đế hiểu thôi, còn người bản xứ (tiếng Anh) thì ‘huh - huh’ dài cổ như cổ cò!

Sau thời gian dài thật dài, mình khám phá rằng mình từng biết tiếng Anh, và nói ra thì người khác hiểu tàm tạm, nhưng khi họ nói thì mình không nghe được gì cả (nghĩa là nghe không hiểu gì cả). Lúc bấy giờ mới tập nghe, và rồi đành bỏ cuộc vì cố gắng mấy cũng không hiểu được những gì người ta nói.


Vấn đề là ở đó: chúng ta đã học tiếng Anh ngược với tiến trình tự nhiên, vì quá thông minh và có quá nhiều kinh nghiệm. Tiến trình ấy là Viết - Đọc - Nói - Nghe!


Vì thế, muốn nghe và nói tiếng Anh, chuyện đầu tiên là phải quên đi kinh nghiệm và trí thông minh, để trở lại trạng thái ‘sơ sinh và con nít’, và đừng sử dụng quá nhiều chất xám để phân tích, lý luận, dịch thuật!



Và đây là bí quyết để Nghe:

A. Nghe thụ động:

1. - ‘Tắm’ ngôn ngữ. Nghe không cần hiểu: Hãy nghe! Đừng hiểu.

Bạn chép vào CD một số bài tiếng Anh (ví dụ từ trên forum này). Mỗi bài có thể dài từ 1 đến 5 phút.

Khi nào bạn ở nhà một mình, thì mở các bài đó ra vừa đủ nghe, và cứ lặp đi lặp lại mãi ra rả như âm thanh nền suốt ngày. Bạn không cần để ý đến nó. Bạn cứ làm việc của mình, đánh răng, rửa mặt, học bài làm bài, vào internet… với tiếng lải nhải của bài tiếng Anh. (thậm chí, trong lúc bạn ngủ cũng có thể để cho nó nói).

Trường hợp bạn có CD player, USB player hay iPod, thì đem theo để mở nghe khi mình có thời gian chết - ví dụ: di chuyển lâu giờ trên xe, đợi ai hay đợi đến phiên mình tại phòng mạch.

Công việc ‘tắm ngôn ngữ’ này rất quan trọng, vì cho ta nghe đúng với từng âm của một ngôn ngữ lạ. Tai của chúng ta bắt rất nhanh một âm quen, nhưng loại trừ những âm lạ. Ví dụ: Nếu bạn nghe câu: ‘mặt trời mọc cánh khi chim voi truy cập chén chó’, một câu hoàn toàn vô nghĩa, nhưng bảo bạn lặp lại thì bạn lặp lại được ngay, vì bạn đã quá quen với các âm ấy. Nhưng khi một người nói một câu bằng chừng ấy âm (nghĩa là 11 âm/vần), trong ngôn ngữ bạn chưa từng học, và bảo bạn lặp lại thì bạn không thể nào lặp lại được, và bảo rằng… không nghe được! (Bạn có điếc đâu! Vấn đề là tai bạn không nhận ra được các âm!) Lối 'tắm ngôn ngữ' đó chỉ là vấn đề làm quen đôi tai, và sau một thời gian (lâu đấy chứ không phải vài ngày) bạn sẽ bắt được các âm của tiếng Anh, và thấy rằng âm ấy rất dễ nghe, nhưng hoàn toàn khác với âm Việt. Đừng nản lòng vì lâu ngày mình vẫn không phân biệt âm: hãy nhớ rằng bạn đã tắm ngôn ngữ tiếng Việt ít ra là 9 tháng liên tục ngày đêm trước khi mở miệng nói được tiếng nói đầu tiên và hiểu được một hai tiếng ngắn của cha mẹ; và sau đó lại tiếp tục 'tắm ngôn ngữ' Việt cho đến 4, 5 năm nữa!

2. - Nghe với hình ảnh động.

Nếu có giờ thì xem một số tin tức bằng tiếng Anh (một điều khuyên tránh: đừng xem chương trình tiếng Anh của các đài Việt Nam, ít ra là giai đoạn đầu, vì xướng ngôn viên Việt Nam, phần lớn, nói rất gần với âm Việt Nam (kể cả pronounciation), nên mình dễ quen nghe, và từ đó lỗ tai mình lại hỏng, về sau lại khó nghe người bản xứ nói tiếng Anh - thế là phải học lại lần thứ hai!). Các hình ảnh đính kèm làm cho ta ‘hiểu’ được ít nhiều nội dung bản tin, mà không cần phải ‘dịch’ từng câu của những gì xướng ngôn viên nói. Bạn sẽ yên tâm hơn, sau khi nghe 15 phút tin tức, tự tóm lược lại, thì mình thấy rằng mình đã nắm bắt được phần chính yếu của nội dung bản tin. Và đây là cách thư hai để tắm ngôn ngữ.

B. Nghe chủ động:


1. Bản tin special english.

- Thu một bản tin, và nghe lại rồi chép ra nhiều chừng nào hay chừng nấy… nhớ là đừng tra cứu tự điển hay tìm hiểu nghĩa vội. Đoán nghĩa trong nội dung câu, và nhớ lại âm thanh của từ, hay cụm từ đó, sau này tự nó sẽ rõ nghĩa, nếu trở đi trở lại hoài.

(Ngày xưa, trên đài VOA, sau mỗi chương trình tôi thường nghe một cụm từ tương tự như: statue, statute hay statu gì đó, mà không biết viết thế nào, tuy vẫn hiểu đại loại là: hãy đợi đấy để nghe tiếp. Mãi sau này tôi mới biết rằng thuật ngữ rất quen thuộc ấy là 'stay tune', nhưng một thời gian dài, chính tả của chữ ấy đối với tôi không thành vấn đề!)

2. Chăm chú nghe lại một số bài mình từng nghe trong giai đoạn ‘tắm ngôn ngữ’.

- Lấy lại script của những bài mình từng nghe, đọc lại và nhớ lại trong tưởng tượng lời đọc mà mình từng nghe nhiều lần.

Sau đó xếp bản script và nghe lại để hiểu. Lần này: tự nhiên mình sẽ nghe rõ từng tiếng và hiểu. Trường hợp không hiểu một từ hay cụm từ, thì gắng lặp lại nhiều lần đúng như mình đã nghe, sau đó lật lại script để so sánh.

3. Một số bài Audio trong Forum này.


Nghe nhiều lần, trước khi đọc script. Sau đó, đọc lại script, chủ yếu kiểm tra những từ mình đã nghe hoặc đoán, hoặc những từ mà mình có thể phát âm lại nhưng không hiểu viết và nghĩa thế nào. Qua việc này, nhiều khi ta phát hiện rằng một từ mình rất quen thuộc mà từ xưa đến nay mình cứ in trí là phải nói một cách nào đó, thì thực ra cần phải nói khác hẳn và phát âm như thế thì mới mong nghe đúng và nói cho người khác hiểu. Sau đó, xếp bản script và nghe lại một hai lần nữa. (Ví dụ: hai chữ tomb, bury, khi xưa tôi cứ đinh ninh là sẽ phát âm là 'tôm-b(ơ), bơri' - sau này nghe chữ 'tum, beri' tôi chẳng hiểu gì cả - dù cho tôi nghe rõ ràng là tum, beri -cho đến khi xem script thì mới vỡ lẽ!)

4. Học hát tiếng Anh, và hát theo trong khi nghe.


Chọn một số bài hát mà mình thích, tìm lyrics của nó rồi vừa nghe vừa nhìn lyrics. Sau đó học thuộc lòng và hát song song với ca sĩ, và gắng phát âm cũng như giữ tốc độ và trường độ cho đúng. Khi nào buồn buồn cũng có thể tự hát cho mình nghe (nếu không có giọng tốt và hát sai giọng một tí cũng không sao, vì chủ yếu là tập phát âm, tốc độ, trường độ và âm điệu tiếng Anh).

Và nói cho đúng giọng (qua hát) cũng là một cách giúp mình sau này nhạy tai hơn khi nghe, vì thường thường ngôn ngữ trong các bài hát khó nghe hơn những câu nói bình thường rất nhiều.
--------------
Trước khi tạm dừng topic này, tôi muốn nói thêm một điều.

Có bạn bảo rằng hiện nay mình chưa hiểu, nên cố gắng nghe nhiều cũng vô ích, để mình học thêm, khi nào có nhiều từ vựng để hiểu rồi thì lúc đó sẽ tập nghe sau.

Nghĩ như thế là HOÀN TOÀN SAI. Chính vì bạn chưa hiểu nên mới cần nghe nhiều hơn những người đã hiểu. Muốn biết bơi thì phải nhảy xuống nước, không thể lấy lý do rằng vì mình không thể nổi nên ở trên bờ học cho hết lý thuyết rồi thì mới nhảy xuống, và sẽ biết bơi! Chưa biết bơi mà xuống nước thì sẽ uống nước và ngộp thở đấy, nhưng phải thông qua uống nước và ngộp thở như thế thì mới hy vọng biết bơi.

Muốn biết bơi, thì phải nhảy xuống nước, và nhảy khi chưa biết bơi. Chính vì chưa biết bơi nên mới cần nhảy xuống nước.

Muốn biết nghe và hiểu tiếng Anh thì phải nghe tiếng Anh, nghe khi chưa hiểu gì cả! Và chính vì chưa hiểu gì nên cần phải nghe nhiều.

( Nguồn: dethi.com )


Học tiếng Anh thế nào là tốt nhất ?

Học tiếng Anh thế nào là tốt nhất? Trên thực tế không có cách nào là tốt nhất cho mọi đối tượng cả vì mỗi người có thói quen, khả năng tiếp thu khác nhau. Tuy nhiên đây là một số bí quyết bạn có thể tham khảo.

Muốn học

Trước tiên để học tốt tiếng Anh thì bạn phải xác định là mình muốn học. Nếu bạn không muốn học thì chẳng có lớp nào, thầy nào, sách nào hay bí quyết nào có thể khiến nó trở nên dễ dàng hơn được cả và bạn có thể tìm ra hàng trăm lý do để tránh né. Nếu hiện tại bạn vẫn chưa thể thích được tiếng Anh thì hãy để nó sang một bên cho đến khi bạn cảm thấy mình đã hoàn toàn sẵn sàng.

Xác định động cơ thúc đẩy


Động cơ thúc đẩy trong mọi công việc nói chung và học tiếng Anh nói riêng đóng một vai trò rất quan trọng. Hãy tự hỏi mình: “Tại sao tôi muốn học tiếng Anh? Tại sao tôi muốn nâng cao tiếng Anh của mình?” Có người muốn học tiếng Anh để đi du học, có người để thăng tiến trong công việc lại có những người chỉ đơn thuần để xem những bộ phim hay nghe những bản nhạc bằng tiếng Anh. Xác định được động cơ, bạn sẽ có tinh thần học hành hơn.

Mục tiêu


Đi cùng với động cơ thúc đẩy chính là mục tiêu. Có mục tiêu rõ ràng bạn có thể tự đánh giá quá trình học tập của mình, biết mình đang ở đâu và nên đi bước tiếp theo như thế nào.

Luyện tập


Luyện tập càng nhiều càng tốt, đó chính là cách học tiếng Anh hiệu quả nhất. Bạn có nhiều cách khác nhau để luyện tập: nói chuyện với người nói tiếng Anh; xem phim, nghe nhạc, đọc truyện bằng tiếng Anh; chát bằng tiếng Anh; … Với cách này bạn có thể vừa mở rộng từ vựng lại vừa luyện được nhiều kỹ năng khác nhau.

Xóa bỏ ngại ngùng


Những người mới học hoặc tiếng Anh không tốt thường có tâm lý sợ sai và không mạnh dạn để sử dụng thứ ngôn ngữ này. Điều này khiến họ có ít cơ hội luyện tập và tiếng Anh lại càng không được cải thiện.

Kết bạn với những người nói tiếng Anh


Nếu có thể bạn hãy kết bạn với những người nói tiếng Anh đặc biệt là những người đến từ các nước nói tiếng Anh. Bạn không chỉ có cơ hội được luyện tập ngôn ngữ mà còn hiểu biết rất nhiều về phong tục, tập quán của họ.

Rèn thói quen


Học ít nhưng đều đặn mỗi ngày tốt hơn là học tập trung vài tiếng đồng hồ mỗi tuần. Đây là một bí quyết đơn giản nhưng rất hiệu quả. Ngay từ hôm nay bạn hãy lên thời gian biểu cho việc học tiếng Anh đi nhé.


(Theo Tâm Anh - Sức Trẻ Việt Nam)

Feb 6, 2010

Thực hiện việc chuyển Text và Chuyển mã Hexa ra Serial Port

Thực hiện việc chuyển Text và Chuyển mã Hexa ra Serial Port bằng C# với .Net FrameWork 2.0
Code:
using System.IO.Ports;
using System.IO;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

private SerialPort comport = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

public Form1()
{
InitializeComponent();
}

private void btnKetThuc_Click(object sender, EventArgs e)
{
Close();
}
private void btnTestPort_Click(object sender, EventArgs e)
{
try
{
comport.Open();
this.comport.DtrEnable = true;
this.comport.RtsEnable = true;

}
catch
{
txtThongBao.Enabled = true;
txtThongBao.Text = "Có sự cố khi mở cổng Com, hãy kiểm tra lại hệ thống, Chương trình chưa thực hiện được!";
}
if (comport.IsOpen)
{
btnTestPort.Enabled = false;
btnHexTx.Enabled = true;

btnClosePort.Enabled = true;

txtRx.Enabled = true;
txtHexTx.Enabled = true;
txtTx.Enabled = true;
txtThongBao.Enabled = true;
txtThongBao.Text = "Cổng Com 1 đã được mở rồi, bắt đầu chiến đấu đi";

}
else this.Focus();

}


private void dulieuden(object sender, SerialDataReceivedEventArgs e)
{
txtRx.Enabled = true; // Hiển thị TextBox nếu Dữ liệu đến
btnRx.Enabled = true; // Hiển thị button Ẩn nếu Dữ liệu đến
comport.DataReceived += new SerialDataReceivedEventHandler(dulieuden);

}

// Đọc toàn bộ dữ liệu lưu tại buffer, triệu tập dữ liệu lưu trong hàm dulieuden
private void btnRx_Click(object sender, EventArgs e)
{
string docdulieu = comport.ReadExisting().ToString();
this.txtRx.Text = docdulieu;
txtThongBao.Text = "Dữ liệu nhận được ứng với mã Hexa là: " + AtoX(docdulieu);
}

private void btnClosePort_Click(object sender, EventArgs e)
{
comport.Close();
txtThongBao.Text = "Cổng Com đã được đóng";

btnTestPort.Enabled = true;
btnHexTx.Enabled = false;


txtTx.Enabled = false;
txtHexTx.Enabled = false;
txtThongBao.Enabled = false;

btnClosePort.Enabled = false;
}
Việc truyền nhận mã hexa ta quy ước vào một hàm, hàm này với chức năng chuyển đổi sang mã định dạng Hexa để truyền qua Serial Port

// Chuyển đổi Chuỗi ASCII sang Định Dạng Hexa.

Code:
private string AtoX(string asc)
{
int nLines;
int nChars;
int offset;
string hex = " ";

// tính toán số lượng dòng Hexa.
if ((asc.Length % 16) > 0)
nLines = asc.Length / 16 + 1;
else
nLines = asc.Length / 16;

// Chuyển đổi sang những dòng Hexa.
for (int i = 0; i < offset =" i"> 16)
nChars = 16;
else
nChars = asc.Length - offset;
hex += this.HexLine(i, asc.Substring(offset, nChars)) + "\r\n";
}
return hex;
}

///
/// Chuyển một chuỗi ASCII 16 byte sang định dạng hexa.
///

private string HexLine(int lNum, string asc)
{
string hex = "";

// Copy dòng vào bộ đệm.
char[] c = new char[16];
asc.CopyTo(0, c, 0, asc.Length);

// Tạo offset.
hex += String.Format("{0:X8} - {0:X8}", lNum * 16, (lNum + 1) * 16 - 1);
hex += " ";

// Chuyển các ký tự sang định dạng chuẩn hexa.
for (int i = 0; i < nspaces =" 62" i =" 0;" i =" 0;"> 126))
hex += ".";
else
hex += c[i].ToString();
}

// Trả lại dòng hexa .
return hex;
}

private void btnHexTx_Click_1(object sender, EventArgs e)
{
try
{
byte[] data = HexStringToByteAray(txtHexTx.Text);
comport.Write(data, 0, data.Length); //Đưa mảng số Hexa qua cổng com với định dạng chuẩn
string a = this.txtTx.Text;
comport.Write(AtoX(a));
txtThongBao.Text = "Thông tin được gửi ra cổng RS232 với mã Hex là: " + txtHexTx.Text.ToUpper() + "\r\n \r\n" + "Thông tin được gửi ra cổng RS232 chuyển sang mã Hex Tập tin Text là: \r\n \r\n" + AtoX(a).ToString();


}

catch (FormatException)
{
txtThongBao.Text = "Có thể cổng com đóng hoặc bạn nhập chính xác Mã Hexa cần Truyền Nhận. Không nhận được định dạng chuỗi sau: " + txtHexTx.Text.ToUpper();
}
return;
}
một Class riêng để có thể nhúng trực tiếp vào chương trình khi thiết kế ứng dụng phần mềm!

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO.Ports;
using System.Threading;

class ComPorts
{
public Boolean OpenComPortOk;
public ComPorts()
{

}
~ComPorts()
{

}
///
/// Mo cong COM1
///

///
public SerialPort Open(String Port, int Baud, Parity Prt, int DtB, StopBits StB)
{
//SerialPort ComPort = new SerialPort("COM1", 9600, Parity.None , 8, StopBits.One);
SerialPort ComPort = new SerialPort( Port,Baud, Prt, DtB, StB);
try
{
if (!ComPort.IsOpen)
{
ComPort.Open();
this.OpenComPortOk = true;
}
}
catch (Exception e)
{
MessageBox.Show("Lỗi mở cổng:"+e.ToString());

this.OpenComPortOk = false;
return ComPort;
}
ComPort.RtsEnable = true;
ComPort.DtrEnable = true;

return ComPort;
}
public void Close()
{

}
///
/// Send(SerialPort ComPort, String data) se
/// Gui chuoi data ra cong ComPort
///

///
///
public void Send(SerialPort ComPort, String data)
{
ComPort.Write(data);

}
public String Receive(SerialPort ComPort)
{
String ReceiveData = "";
ReceiveData = ComPort.ReadExisting();
return ReceiveData;


}
public byte ReceiveByte(SerialPort ComPort)
{
byte Value;
Value = Convert.ToByte(ComPort.ReadByte());
return Value;
}

}
File gửi kèm
File  Type: rar ComPorts.rar (876 Bytes, 993 views)

Khi bạn sử dụng Serial Port, bạn nhúng Class vào chương trình và khai báo trong cửa sổ code của bạn

using System.IO.Ports;
using System.Threading;

Việc gọi Class bạn chỉ cần làm như sau:

Nhấn CRT + Space và bạn chọn ComPorts

Việc sử dụng chính là bạn khai thác ngay trực tiếp các Method mình đã xây dựng trong Class đó! Truyền tham số vào như ý muốn của bạn!

Chúc bạn thành công!

iện đây cũng muốn nói chút xíu về ứng dụng. Mình thấy có anh em quan tâm đến việc Điều Khiển Máy Tính từ xa! Anh em rất vất vả khi đi thiết kế phần cứng. Quan điểm của mình, thì lại thấy việc này rất đơn giản.

Thiết kế một Modune Thu Phát Hồng Ngoại với việc sủ dụng MCP2155 hoặc MCP2150 để thiết kế!

Sau đó, lên một thiết kế về khung truyền những tương tác với máy tính chúng ta cần điều khiển, gán các chỉ thị và các tập lệnh thành một Khung Truyền riêng! Việc này cần tối ưu vì đó là việc chúng ta đi xây dựng một Class trong chương trình của chúng ta!

Ứng dụng vào ra với Serial Port, viết phần mềm tương tác với các chức năng quản lý hệ thống máy tính. Việc này các Componet của .Net FrameWork 2.0 rất mạnh. Chúng ta chỉ cần Add các Referrence vào modune của chương trình là có thể thao tác một cách dễ dàng!

Thêm một nhân tố rất hữu ích nữa là việc ta điều khiển máy tính của ta qua Internet! Mình ngồi ở Văn Phòng cũng có thể điều khiển được máy tính của ta! Ta lập trình Socket C# với .Net. Có thời gian mình sẽ Post ứng dụng của mình lên để anh em cùng tham khảo về kỹ thuật lập trình Mạng với C#.

Xin cảm ơn!

http://www.dreamincode.net/forums/showtopic35775.htm
http://dientuvietnam.net/forums/showthread.php?t=3046&page=4

SerialPort (RS-232 Serial COM Port) in C# .NET

SerialPort (RS-232 Serial COM Port) in C# .NET

Published 23 March 5 7:28 PM | coad

This article is about communicating through the PC's Serial COM RS-232 port using Microsoft .NET 2.0 or later by using the System.IO.Ports.SerialPort class.

Way in the Past...

Back in my early (pre C# MVP) days, I had written an article on DevHood titled Serial COM Simply in C#. It became quite popular and is (currently) the top Google search for "serial c#". Now every week I get several e-mails with people asking questions and it is high time for some serious updating.

The article was (originally) written soon after .NET was introduced to the world, and other .NET serial port controls had not yet been written. So at the time, this was an easy solution. Just use the MSComm.ocx control from VS6 which most Devs had at the time. Now however, there are many easier and preferred methods than dealing with the complexities of interoping with an old legacy (non-.NET) ActiveX OCX control.

A Bright Future is Here!

Today, the primary solution is to use the new SerialPort control that is part of .NET 2.0 and is freely available in C# Express on MSDN. It is considerably easier to use and is a true .NET component.

Download Button  - Small For loyal fans of the tutorial, I've written a sample code application, SerialPortTerminal.zip, which you can try out to see how the new SerialPort control is used. Requires Visual Studio 2005 & .NET 2.0 or later to compile & run.

Continued Support

Due to the high volume of e-mail I've been receiving on serial port communication in general, I've wrote this post and added an FAQ section to the bottom of the post. Check this first if you have questions. If you post a question as a comment that hasn't been answered in the post, I'll add it to the FAQ section.

For those of you who I've directed to this blog post, please understand that I'm just another guy working away at a full time job (helping manage the release of Team System) who has worked with serial RS-232 ports in the past (moving on to USB now). I like to help people, hence I wrote the original article, this sample code, and the FAQ, but do not have the resources to answer additional serial port communication questions. Please check out the SerialCom FAQ for other .NET COM Port solutions and technical support options.

Get Connected Up

You can obtain USB to Serial adapters (from NewEgg, search using Froogle) and have just about as many ports on your PC as you like. I carry around two adapters with a null modem (what is it?, search Froogle) between them so I can create a loopback to send & receive through to separate ports on most any computer. I'd recommend doing the same for when writing code for the serial port.

If you'd like to quickly and easily create your own external devices to communicate with the PC, I recommend starting with the Parallax BASIC Stamp modules. Parallax has lots of easy accessories (such as LCDs, RF, Sounds, AD & DA, etc) for their modules. After that you could migrate to an Atmel Microcontroller (recommended) or Microchip PIC.

Let's Have the Code

Here is an example of how easy it is to use the new SerialPort control.

Very simply, here is how you can send a bit of data out the port.

// This is a new namespace in .NET 2.0
// that contains the SerialPort class
using System.IO.Ports;

private static void SendSampleData()
{
// Instantiate the communications
// port with some basic settings
SerialPort port = new SerialPort(
"COM1", 9600, Parity.None, 8, StopBits.One);

// Open the port for communications
port.Open();

// Write a string
port.Write("Hello World");

// Write a set of bytes
port.Write(new byte[] {0x0A, 0xE2, 0xFF}, 0, 3);

// Close the port
port.Close();
}


Now let's take a look at what it takes to read data in from the communications port. This demonstrates reading text; for an example of reading binary data, see my SerialPortTerminal.zip sample app.

  1. Create a new "Console Application" and replace all the default class code with this code
  2. Add a reference to "System.Windows.Forms" to the project
  3. Run w/ F5, to exit the app, press Ctrl-Break.
  4. Get Connected Up with two USB to Serial adapters and a null modem
  5. Use another app, the code above, or the SerialPortTerminal.zip example to send data and watch it come in with this code


#region Namespace Inclusions
using System;
using System.IO.Ports;
using System.Windows.Forms;
#endregion

namespace SerialPortExample
{
class SerialPortProgram
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM1",
9600, Parity.None, 8, StopBits.One);

[STAThread]
static void Main(string[] args)
{
// Instatiate this class
new SerialPortProgram();
}

private SerialPortProgram()
{
Console.WriteLine(
"Incoming Data:");

// Attach a method to be called when there
// is data waiting in the port's buffer
port.DataReceived += new
SerialDataReceivedEventHandler(port_DataReceived);

// Begin communications
port.Open();

// Enter an application loop to keep this thread alive
Application.Run();
}

private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e)
{
// Show all the incoming data in the port's buffer
Console.WriteLine(port.ReadExisting());
}
}
}


One of the (several) new methods that is supported, and one I'm very glad is finally here, is the ability to obtain a list of the COM ports installed on the computer (ex: COM1, COM2, COM4). This is definately helpful when you want to present the list of ports avalible for the user to select from (as in the SerialPortTerminal.zip Win App example).

foreach (string s in SerialPort.GetPortNames())
System.Diagnostics.Trace.WriteLine(s);


Here are two helpful little methods for sending files through the serial port. Of course, these are the bare essentials and as always, you should check to make sure the port is open first (port.IsOpen) and use try/catch around trying to open a file, but you get the gist with this code. The binary sending routine is limited to about 2GB (the size of an int), but this should be okay for most uses.

using System.IO;

private static void SendTextFile(
SerialPort port, string FileName)
{ port.Write(File.OpenText(FileName).ReadToEnd()); }

private static void SendBinaryFile(
SerialPort port, string FileName)
{
using (FileStream fs = File.OpenRead(FileName))
port.Write((
new BinaryReader(fs)).ReadBytes(
(int)fs.Length), 0, (int)fs.Length);
}


RS-232 Project Photos

Each of these involve RS-232 serial port communications.

Just what's needed to get started with microcontrollers,
a Basic Stamp, mini LCD display, power, and RS-232 port.


Two USB to Serial adapters with a null modem
to loopback and test your serial software.


The brains to a mini automated radio station that let me
control my PC & home using my HAM radio from around town.



Port Wiring Notes

DB9 Male (Pin Side)                   DB9 Female (Pin Side)
DB9 Female (Solder Side) DB9 Male (Solder Side)
------------- -------------
\ 1 2 3 4 5 / \ 5 4 3 2 1 /
\ 6 7 8 9 / \ 9 8 7 6 /
--------- ---------

DB9 Female to DB9 Female Null-Modem Wiring
2 | 3 | 7 | 8 | 6&1| 5 | 4
---- ---- ---- ---- ---- ---- ----
3 | 2 | 8 | 7 | 4 | 5 | 6&1

9-pin 25-pin Assignment From PC
------ ------ ------------------------- ------------
Sheild 1 Case Ground Gnd
1 8 DCD (Data Carrier Detect) Input
2 3 RX (Receive Data) Input
3 2 TX (Transmit Data) Output
4 20 DTR (Data Terminal Ready) Output
5 7 GND (Signal Ground) Gnd
6 6 DSR (Data Set Ready) Input
7 4 RTS (Request To Send) Output
8 5 CTS (Clear To Send) Input
9 22 RI (Ring Indicator) Input

- RTS & DTR are binary outputs that can be manually set and held
- DCD, DSR, CTS, and RI are binary inputs that can be read
- RX & TX can not be set manually and are controlled by the UART
- maximum voltages are between -15 volts and +15 volts
- binary outputs are between +5 to +15 volts and -5 to -15 volts
- binary inputs are between +3 to +15 volts and -3 to -15 volts
- input voltages between -3 to +3 are undefined while output voltages
between -5 and +5 are undefined
- positive voltages indicate ON or SPACE, negative voltages indicate
OFF or MARK


Other Resources

Here are some additional sites, libraries, tutorials, etc. These are links that I just found around the net and am providing for convenience (they are not endorsed).

Where CF = Compact Framework

The Final Say (aka Conclusion)

The new SerialPort class in .NET 2.0 rocks! It is much easier to use than getting the old MSComm.ocx control going in a .NET app, contains new functionality, is a 'native' .NET control, has docs built into the MSDN Library, and is easy to use.

Frequently Asked Questions (FAQ)

I'm adding this section (as of 8/10/06) to address the common questions I get on this post and through e-mail. Chances are, if you ask a good question in the comments here, I'll post it here for others to see easily.

  1. Q: When updating a control (like a text box) while in the DataRecieved event, I get an error.
    A: The SerialPort class raises events on a separate thread than the main form was create on. Windows Forms controls must be modified only on their original thread. Thankfully there is an easy way to do this. Each Windows control has a "Invoke" method which will run code on the control's original thread. So to put the recently received data into a text box (txtLog), this would do it: txtLog.Invoke(new EventHandler(delegate { txtLog.Text += comport.ReadExisting(); }); You can see this more in action in the "Log" event of "Terminal.cs" my sample code project, SerialPortTerminal.zip.
  2. Q: I can't find the System.IO.Ports namespace.
    A: Using Visual Studio 2003? The new namespace, and SerialPort class, is part of .NET 2.0 and Visual Studio 2005. It is not included in .NET 1.x (and Visual Studio 2003). Even if you have .NET 2.0 or Visual Studio 2005 installed, you can not access the class from within Visual Studio 2003.
  3. Q: I only have .NET 1.1, what can I do?
    A: Upgrade to .NET 2.0. Seriously, it's free. In fact, you can get the great C# and VB Visual Studio Interactive Development Environment (IDE) editors for FREE now with C# Express and VB Express. The .NET 2.0 Software Development Kit (SDK) for command-line development is also free. If you really must stay in .NET 1.1, you can use the technique I talk about in Serial COM Simply in C# or a 3rd party library.
  4. Q: I'm sending data to my device, but it is not responding.
    A: First make sure the device will respond using a standard app like Hyperterminal. Check the settings (baud rate, data bits, stop bits, etc) and make sure they match with your code. Try sending binary data via binary arrays. Many devices expect a carriage return at the end of a command, so be sure to send 0x0D or \n. String data can be easily converted to a binary array using:
    byte[] data = System.Text.ASCIIEncoding.Default.GetBytes("Hello World\n");
    com.Write(data, 0, data.Length);
    Many devices require several carriage returns first to sync baud rates, so send several, like: com.Output("".PadLeft(9, '\n')); It you're communicating with a modem, make sure Echo Back is turned on. Send "ATE1\n". Other than this, just keep trying and playing around with it. It can be hard because you don't see the response from the device as easily as you would with a terminal app.
  5. Q: When I put received data to a text box or rich text box, I get a strange symbols.
    A: The default font of text boxes is designed only to show standard characters. Try using "CharMap" (a free tool in WinXP, click "Start", "Run", type "CharMap", enter). "Terminal" is a font designed to show classic ASCII characters and is what most terminal apps (like my sample code and Hyperterminal) use. There are also many ASCII codes that won't display correctly. This is why I choose to show the hex data instead of an ASCII string a lot of the time. System.Convert.ToString(mybyte, 16) will convert a byte to a string hex code, for example: byte b = 13; string s = Convert.ToStrong(b, 16).PadLeft(2, '0'), then s will contain "0D". See the "ByteArrayToHexString" and "HexStringToByteArray" methods in my sample app, SerialPortTerminal.zip.
  6. Q: What about USB communications? How can I do USB?
    This post isn't about USB. Believe me, I wish the .NET framework supported USB natively, and I'm doing what I can here at Microsoft to see USB get into the framework in the future. For now, you can use a USB to Serial adapter. I use a lot of these. They plug into the USB port, then appear just as a SerialPort to the PC. Microcontroller vendors such as Microchip, Atmel, and TI make chips that do this for projects. There is a lot of info on USB and C# out there too (such as this great article).
  7. Q: Can I use the sample code here in my own projects (commercial or not)?
    Yes! All sample code on my blog is free public domain material. I'm not a legal guy so I don't know the exact words to use, but I'll try... I'm not responsible for any problems! Use at your own rick etc. However, have at it, if it helps you out, fantastic, that's what it's here for.
  8. Q: When using SerialPort.Open() (or SerialPort.PortOpen = true) I get the exception "UnauthorizedAccessException" or the error "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in axinterop.mscommlib.dll"
    It may be one of a few factors:
    • It may require Administrative privileges to use the port.
    • The port may already be open by another program, only one app can use a port at a time.
    • The port may not exist on the computer (this happens a lot). Verify the port you're trying to open is listed in the Device Manager (FAQ #9).
    • The name being provided is not exactly correct.
      Use the full name of the port when opening, like "COM1" (not "1")
  9. Q: How do I know what COM ports are on my PC?
    Use the Device Manager ("Start", "Run", "devmgmt.msc") and look for the "Ports" node (see below). If you don't see a Ports node, it's because there are not Serial or Parallel ports installed in the PC. Many laptops these day's don't have a serial port. You can get more serial ports very easily today with USB to Serial adapters.

  10. Q: How do I communicate with my specific device? Modem, Mobile Phone, LED/LCD Display, Scanner, etc
    This post is specific to general, device independent, serial port communications. You will need to find information about the protocol used for your specific device elsewhere. Comments that ask about specific devices will be deleted (to reduce spam). I'd recommend looking on the manufacture's website, writing/calling the manufacture, or searching online for your specific device, like "Motorola Razor Serial Protocol".
  11. Q: What pins can I use for powering devices, a high signal, or for boolean input & output?
    The TX & RX pins carry the standard serial signal, but the other pins can be used as high/low input/output pins. The output pins (4 DTR or 8 CTS), supply 5 to 15 volts (15v is proper RS-232 standard) when high and 0 to -15 volts when low. They only supply flea current so they're not meant to be used for powering any devices (like USB is designed for). However, they can be used as a reference voltage or for switching to one of the input pins for a high or low signal. The input pins (1 DCD, 6 DSR, 8 CTS, and 9 RI) can be used to detect a high or low signal. Proper RS-232 signal levels are -15v for a low and +15v for a high (compared to ground, pin 5). A MAX232 or similar chip takes a TTL 0-5v input and produces the -15v to +15v levels. However, most PC RS-232 COM ports will accept a 0v for low and 5v for high, but it is not guaranteed and alters from PC to PC. If you want a simple "toggle high", just hold pin 4 DTR high, and switch it to pin 1 DCD. The .NET SerialPort class has easy to use properties for switching the output pins high or low and for detecting the current level of the input pins. I have been able to use pin 4 DTR for a very low current (20ma max) PIC processors, but not reliably. I prefer to always supply external power and use pin 4 as a signal to turn on or off my device. I'll attach pin 4 to a transistor that switches my power source to my PIC to turn it on or off.
  12. Q: What about ‘packets’? Does RS-232 support any commands or data segregation?
    No. Serial data flow through RS-232 has nothing to do with ‘packets’. It’s just a stream of bytes in and out. Any notion of data compartmentalization (packets) would have to be coded by you for your unique use. Much of my time working with serial has been spent on defining useful packet like protocols, that usually include some type of header, command structure, and CRC check. For example, the first two bytes are the packet length, the next two bytes is the command, next two bytes are parameters, and the last byte is a CRC. Then my apps would buffer incoming data and look in the buffer for valid packets. Of course it differs depending on the device you’re working with and your specific needs. USB does have specific communications protocol defined, one of them being command based, like the little packet just mentioned. But with USB, you’re able to get the whole command and parameter together at once, with serial you have to create the protocol yourself, buffer, and parse the data. My previous serial post has more info under the header “Protocol Development”.
  13. Q: How do you detect when a device is connected or disconnected?
    Simply put, the device usually start or stop sending data. There is no built in events on a connect or disconnect. But there are a few tricks you can do, if you’re creating the serial device you have more options. I’ve had my devices (and PC apps) send a constant “are you there” set of bytes, like 00 00 11 11 00 00 hex (I’d use a ‘are you there’ custom ‘packet’ as in Q12 above) till a device on the other end responds. You could also use hardware, that’s what some of the other signals lines are for (CDC, DSR, RTS, CTS, RI), you can tie one of these high and then catch an event when the line goes high you know there’s a device there, when it goes low the device is gone.
  14. Q: How can I get more support? What are my support options?