Click here to Skip to main content
15,895,084 members
Home / Discussions / C#
   

C#

 
AnswerRe: Comparing string with string array Pin
mkani30-Aug-05 21:19
mkani30-Aug-05 21:19 
QuestionTimer Pin
Taurian11029-Aug-05 18:28
Taurian11029-Aug-05 18:28 
AnswerRe: Timer Pin
Andy Brummer29-Aug-05 18:41
sitebuilderAndy Brummer29-Aug-05 18:41 
AnswerRe: Timer Pin
Alomgir Miah29-Aug-05 18:47
Alomgir Miah29-Aug-05 18:47 
AnswerRe: Timer Pin
mav.northwind29-Aug-05 20:35
mav.northwind29-Aug-05 20:35 
Questionhow to add web refrence through coding in c# Pin
mkani29-Aug-05 18:15
mkani29-Aug-05 18:15 
AnswerRe: how to add web refrence through coding in c# Pin
Andy Brummer29-Aug-05 18:48
sitebuilderAndy Brummer29-Aug-05 18:48 
QuestionHow to call to MAIN Pin
levi's29-Aug-05 18:08
levi's29-Aug-05 18:08 
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?I try it so many time but still fail to do it..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() );


}






}



}

QuestionCheck TextBox To Confirm Number? Pin
...---...29-Aug-05 16:27
...---...29-Aug-05 16:27 
AnswerRe: Check TextBox To Confirm Number? Pin
Christian Graus29-Aug-05 16:43
protectorChristian Graus29-Aug-05 16:43 
GeneralRe: Check TextBox To Confirm Number? Pin
...---...29-Aug-05 16:50
...---...29-Aug-05 16:50 
QuestionCrystal report 8.5 - direct to printer Pin
Sarvesvara (BVKS) Dasa29-Aug-05 15:31
Sarvesvara (BVKS) Dasa29-Aug-05 15:31 
Questionwrite a C program Pin
trjskid29-Aug-05 15:16
trjskid29-Aug-05 15:16 
AnswerRe: write a C program Pin
Christian Graus29-Aug-05 16:00
protectorChristian Graus29-Aug-05 16:00 
GeneralRe: write a C program Pin
Judah Gabriel Himango29-Aug-05 17:37
sponsorJudah Gabriel Himango29-Aug-05 17:37 
AnswerRe: write a C program Pin
Matt Gerrans29-Aug-05 20:24
Matt Gerrans29-Aug-05 20:24 
AnswerRe: write a C program Pin
Susan Hernandez31-Aug-05 15:54
Susan Hernandez31-Aug-05 15:54 
QuestionRestricting remoting method calls Pin
Rei Miyasaka29-Aug-05 12:34
Rei Miyasaka29-Aug-05 12:34 
AnswerRe: Restricting remoting method calls Pin
Judah Gabriel Himango29-Aug-05 17:38
sponsorJudah Gabriel Himango29-Aug-05 17:38 
GeneralRe: Restricting remoting method calls Pin
Rei Miyasaka29-Aug-05 17:51
Rei Miyasaka29-Aug-05 17:51 
GeneralRe: Restricting remoting method calls Pin
Judah Gabriel Himango29-Aug-05 19:12
sponsorJudah Gabriel Himango29-Aug-05 19:12 
Questionadd webusercontrol Pin
reza toorani29-Aug-05 9:48
reza toorani29-Aug-05 9:48 
AnswerRe: add webusercontrol Pin
Alomgir Miah29-Aug-05 10:32
Alomgir Miah29-Aug-05 10:32 
GeneralRe: add webusercontrol Pin
reza toorani30-Aug-05 17:11
reza toorani30-Aug-05 17:11 
GeneralRe: add webusercontrol Pin
Anonymous30-Aug-05 17:25
Anonymous30-Aug-05 17:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.