How to call to MAIN?

Aug 30 2005 12:13 AM
Hi all!i am using microsoft visual studio.net 2003.i have create a class for communication with serial port via RS-232.It contains  read and write function.i run this in the console application.Class 1 is all the function and class 2 is my MAIN.i use Main to call the read and write function.Can someone please please help me how i can read unlimited bytes?How to call the Public MemoryStream Read(int bufSize) to my MAIN?Please please help me..Thanks a lot!

This is my Class 1

using System;

using System.Runtime.InteropServices;

using System.IO;

using System.Text;

using System.Globalization;

using System.Threading;

 

namespace yong

{

public class port

{

/*-- Definition of variables by Programmer --*/

/*-- Setting win32 API constants --*/

private const uint GENERIC_READ = 0x80000000;

private const uint GENERIC_WRITE = 0x40000000;

private const int CREATE_ALWAYS = 2;

private const int INVALID_HANDLE_VALUE = -1;

/*-- End ofSetting win32 API constants --*/

public int PortNum = 1; //-------------Delaring of Port number to 1

public int BaudRate = 9600; //---------Set BaudRate to 9600

public byte ByteSize = 8; //-----------Set ByteSize to 8

public byte Parity = 0; //-------------0-4 = no,odd,even,mark,space

public byte StopBits = 0; //-----------0,1,2 = 1, 1.5, 2

public int ReadTimeout = 1500; //------Set TimeOut to 1500

private int hComm = -1; //-------------Declaring of comm port win32 file handle

public bool Opened = false; //---------Declaring of Opened and set to false

/*-- End of Definition of variables by Programmer --*/

[StructLayout(LayoutKind.Sequential)]

public struct DCB

{

/*-- taken from c struct in platform sdk --*/

public int DCBlength; //-----------Sizeof(DCB)

public int BaudRate; //------------Current baud rate

public uint flags; //--------------Flags

public ushort wReserved; //--------Not currently used

public ushort XonLim; //-----------Transmit XON threshold

public ushort XoffLim; //----------Transmit XOFF threshold

public byte ByteSize; //-----------Number of bits/byte, 4-8

public byte Parity; //-------------0-4 = no,odd,even,mark,space

public byte StopBits; //-----------0,1,2 = 1, 1.5, 2

public char XonChar; //------------Tx and Rx XON character

public char XoffChar; //-----------Tx and Rx XOFF character

public char ErrorChar; //----------Error replacement character

public char EofChar; //------------End of input character

public char EvtChar; //------------Received event character

public ushort wReserved1; //-------Reserved; Do not use

}/*-- End of StructLayout DCB --*/

[StructLayout(LayoutKind.Sequential)]

private struct COMMTIMEOUTS

{

public int ReadIntervalTimeout;

public int ReadTotalTimeoutMultiplier;

public int ReadTotalTimeoutConstant;

public int WriteTotalTimeoutMultiplier;

public int WriteTotalTimeoutConstant;

}/*-- End of StructLayout Commtimeout --*/

[StructLayout(LayoutKind.Sequential)]

private struct OVERLAPPED

{

public int Internal;

public int InternalHigh;

public int Offset;

public int OffsetHigh;

public int hEvent;

}/*-- End of StructLayout Overlapped --*/

/*-- Importing of Kernel32.dll --*/

[DllImport("kernel32.dll")]

private static extern int CreateFile

(

string lpFileName, //--------------File name

uint dwDesiredAccess, //-----------Access mode

int dwShareMode, //----------------Share mode

int lpSecurityAttributes, //-------SD

int dwCreationDisposition, //------How to create

int dwFlagsAndAttributes, //-------File attributes

int hTemplateFile //---------------Handle to template file

);

[DllImport("kernel32.dll")]

private static extern bool GetCommState

(

int hFile, //---------Handle to communications device

ref DCB lpDCB //------Device-control block

);

[DllImport("kernel32.dll")]

private static extern bool BuildCommDCB

(

string lpDef, //------Device-control string

ref DCB lpDCB //------Device-control block

);

[DllImport("kernel32.dll")]

private static extern bool SetCommState

(

int hFile, //---------Handle to communications device

ref DCB lpDCB //------Device-control block

);

[DllImport("kernel32.dll")]

private static extern bool GetCommTimeouts

(

int hFile, //---------------------------Handle to comm device

ref COMMTIMEOUTS lpCommTimeouts //------Time-out values

);

[DllImport("kernel32.dll")]

private static extern bool SetCommTimeouts

(

int hFile, //---------------------------Handle to comm device

ref COMMTIMEOUTS lpCommTimeouts //------Time-out values

);

[DllImport("kernel32.dll")]

private static extern bool ReadFile

(

int hFile, //------------------------Handle to file

byte[] lpBuffer, //------------------Data buffer

int nNumberOfBytesToRead, //---------Number of bytes to read

ref int lpNumberOfBytesRead, //------Number of bytes read

ref OVERLAPPED lpOverlapped //-------Overlapped buffer

);

[DllImport("kernel32.dll")]

private static extern bool WriteFile

(

int hFile, //--------------------------Handle to file

byte[] lpBuffer, //--------------------Data buffer

int nNumberOfBytesToWrite, //----------Number of bytes to write

ref int lpNumberOfBytesWritten, //-----Number of bytes written

ref OVERLAPPED lpOverlapped //---------Overlapped buffer

);

[DllImport("kernel32.dll")]

private static extern bool CloseHandle

(

int hObject //-------Handle to object

);

[DllImport("kernel32.dll")]

private static extern uint GetLastError();

/*-- End of Importing of Kernel32.dll --*/

 

/*-- Function Open --*/

/*-- To Open the connection between 2 ports via RS232 --*/

public void Open()

{

DCB dcbCommPort = new DCB(); //------Declaring of DCB

COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS(); //------Declaring of COMMTIMEOUTS

/*-- Open Comm Port --*/

hComm = CreateFile( "COM" + PortNum ,GENERIC_READ | GENERIC_WRITE,0, 0,CREATE_ALWAYS ,0,0 ); //------Open comm port by calling dll from kernel32 and get the handle

/*-- Check port cannot be open --*/

if( hComm == INVALID_HANDLE_VALUE )

{

throw(new ApplicationException("Comm Port Can Not Be Opened"));

}/*-- End of Check port cannot be open --*/

/*-- Set the Time out property --*/

GetCommTimeouts(hComm,ref ctoCommPort);

ctoCommPort.ReadTotalTimeoutConstant = 0;

ctoCommPort.ReadTotalTimeoutMultiplier = 0;

ctoCommPort.WriteTotalTimeoutMultiplier = 0;

ctoCommPort.WriteTotalTimeoutConstant = 0;

SetCommTimeouts(hComm,ref ctoCommPort);

/*-- End of Set the Time out property --*/

/*-- Setting of Baud rate, Parity, Word size and Stop bits --*/

GetCommState(hComm, ref dcbCommPort); //------Get the status of current configuration

dcbCommPort.BaudRate = BaudRate; //------Setting of baud rate

dcbCommPort.flags = 0; //------Setting of Flags

//dcbCommPort.flags = 1;//------Setting of Flags

/*-- Check parity more than 0 --*/

if(Parity > 0)

{

dcbCommPort.flags = 2; //------Setting of Flags

}/*-- End of Check parity more than 0 --*/

dcbCommPort.Parity = Parity; //------Setting of Parity

dcbCommPort.ByteSize = ByteSize; //------Setting of ByteSize

dcbCommPort.StopBits = StopBits; //------Setting of StopBits

/*-- Set property and Check for error --*/

if( !SetCommState(hComm, ref dcbCommPort) )

{

throw(new ApplicationException("Comm Port Can Not Be Opened"));

}/*-- End of Set property and Check for error --*/

/*-- End of Setting of Baud rate, Parity, Word size and Stop bits --*/

//Opened = true; //------Set Opened to true

Opened = true;

}/*-- End of Function Open --*/

/*-- Function Close --*/

/*-- Closing of Comm port --*/

public void Close()

{

/*-- Check Open handle is open --*/

if (hComm != INVALID_HANDLE_VALUE)

{

CloseHandle(hComm); //------Close port

}/*-- End of Check Open handle is open --*/

}/*-- End of Function Close --*/


/*-- Function Read --*/

/*-- For receiving of data from other parties --*/

public MemoryStream Read(int bufSize)

{

byte[] BufBytes = new byte[bufSize];

MemoryStream stream = new MemoryStream();

/*-- Check Port is Open --*/

if( hComm != INVALID_HANDLE_VALUE )

{

OVERLAPPED ovlCommPort = new OVERLAPPED();

int BytesRead = 0;

while(ReadFile(hComm, BufBytes, bufSize, ref BytesRead, ref ovlCommPort) != false /* or true (based on your interop declaration) */)

stream.Read(BufBytes,0,BytesRead);

}

return stream;

}

 

/*-- Function Write --*/

/*-- For transfering of data from other parties --*/

public void Write(byte[] WriteBytes)

{

/*-- Check Port is Open --*/

if( hComm != INVALID_HANDLE_VALUE )

{

OVERLAPPED ovlCommPort = new OVERLAPPED(); //------Declaring of ovlCommport

int BytesWritten = 0; //------Declaring of BytesWritten and set to 0

WriteFile(hComm, WriteBytes, WriteBytes.Length, ref BytesWritten, ref ovlCommPort); //------Transfer data to RS232

}/*-- End of Check Port is Open --*/

}/*-- End of Function Write --*/

}/*-- End of class Port --*/

}



This is my class 2

using System;

using System.Text;

using System.Threading;

namespace yong

{

public class Class2

{

static void Main(string[] args)

{

port CommPort = new port();

CommPort.Open();

while(true)

{
/*call write function*/

// Convert string to byte array and send

//string x = Console.ReadLine();

//byte[] byteDateLine = Encoding.ASCII.GetBytes( x.ToCharArray() );

//CommPort.Write(byteDateLine);


 

/*call read function*/

//Convert Byte to String

string sBuffer = Encoding.ASCII.GetString());

Console.WriteLine( sBuffer.ToString() );

}

}

}