Click to See Complete Forum and Search --> : Accessing COM ports in C# using .NET technology


PareshG
May 16th, 2002, 02:58 PM
Hi
I want to use COM port accessiblity.
I have already written a code for writing on com port using DLLImport and using CreateFile/WriteFile I/O functions.

Question is it waits forever in ReadFile and doesn't return.


immediate help is required.

my snippet of code is given.

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplication4
{

/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
public struct OverLapped
{
public int nInternal;
public int nInternalHigh;
public int nOffset;
public int nOffsetHigh;
public int nEventHandle;
};

#region DLLImports
[DllImport("kernel32.dll")]
static extern int CreateFile(string lpFileName,
int dwDesiredAccess, int dwShareMode,
int lpSecurityAttributes, int dwCreationDisposition,
int dwFlagsAndAttributes, int hTemplateFile );

[DllImport("kernel32.dll")]
static extern int CloseHandle(int Handle);

[DllImport("kernel32.dll")]
static extern bool WriteFile(
int hFile,
Byte []lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OverLapped lpOverlapped
);

[DllImport("kernel32.dll")]
static extern bool ReadFile(
int hFile,
Byte []lpBuffer,
int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead,
ref OverLapped lpOverlapped
);

[DllImport("kernel32.dll")]
static extern bool WaitCommEvent(
int hFile, // handle to comm device
ref int lpEvtMask, // event type
ref OverLapped lpOverlapped // overlapped structure
);



#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Opening COM1 Port ...");
int handle = CreateFile("COM1", unchecked((int)0x80000000)|0x40000000, 0, 0, 3, 0, 0);
Console.WriteLine("Wait ...");
System.Threading.Thread.Sleep(2000);
Console.WriteLine("Writing to a COM1 ...");

int nlpBufferWritten = 0;

OverLapped lpOverLapped = new OverLapped();
lpOverLapped.nInternal = 0;
lpOverLapped.nInternalHigh = 0;
lpOverLapped.nEventHandle = 0;
lpOverLapped.nOffset = 0;
lpOverLapped.nOffsetHigh = 0;

//System.Threading.NativeOverlapped lpO;// = new System.Threading.NativeOverlapped();

//bool bRead = ReadFile(handle, buffer, buffer.Length, ref nlpBufferWritten,ref lpOverLapped);

String strMessage = "AT+CMGF?";
byte []buffer = (new System.Text.UnicodeEncoding()).GetBytes(strMessage);
//lpOverLapped = (OverLapped) lpO;
bool bWrote = WriteFile(handle, buffer, buffer.Length, ref nlpBufferWritten, ref lpOverLapped);

String strWrite = Encoding.Unicode.GetString(buffer,0,nlpBufferWritten);

byte []ReadBuffer = new Byte[165];
int nlpBytesRead = 0;
OverLapped O = new OverLapped();

/*while (true)
{
int x=-1;
OverLapped ov = new OverLapped();
WaitCommEvent(handle,ref x,ref ov);
Console.WriteLine(x);
}
*/

////////////////////// Messing around HERE ... read waits tooo long and never returns

bool bRead = ReadFile(handle, ReadBuffer, 165, ref nlpBytesRead, ref O );
String strRead = Encoding.Unicode.GetString(ReadBuffer,0,nlpBytesRead);
System.Threading.Thread.Sleep(2000);



CloseHandle(handle);

//FileStream fs = File.Create("COM1");
//byte []b = (new UnicodeEncoding()).GetBytes("AT");
//fs.Write(b,0,b.Length);


}
}
}


---------------------------------------------------------------

would someone throw light

Thanks a lot
Paresh