Using system;
Using system. runtime. interopservices;
Namespace nativedll
{
/// <Summary>
/// Summary of SerialPort.
/// </Summary>
Public class SerialPort
{
# Region declares the APIS related to serial call to be referenced
// Win32 API Constants
Private const uint generic_read = 0x80000000;
Private const uint generic_write = 0x40000000;
Private const int open_existing = 3;
Private const int invalid_handle_value =-1;
Private const int maxblock = 4096;
Private const uint purge_txabort = 0x0001; // kill the pending/current writes to the comm port.
Private const uint purge_rxabort = 0x0002; // kill the pending/current reads to The COMM port.
Private const uint purge_txclear = 0x0004; // kill the transmit queue if there.
Private const uint purge_rxclear = 0x0008; // kill the typeahead buffer if there.
[Structlayout (layoutkind. Sequential)]
Private struct DCB
{
// Taken from C struct in Platform SDK
Public int dcblength; // sizeof (DCB)
Public int baudrate; // current baud rate
Public int fbinary; // binary mode, no EOF check
Public int fparity; // enable parity checking
Public int foutxctsflow; // CTS output flow control
Public int foutxdsrflow; // DSR output flow control
Public int fdtrcontrol; // DTR Flow Control Type
Public int fdsrsensiti.pdf; // DSR sensiti.pdf
Public int ftxcontinueonxoff; // xoff continues TX
Public int foutx; // Xon/xoff Out Flow Control
Public int finx; // Xon/xoff in Flow Control
Public int ferrorchar; // enable error replacement
Public int fnull; // enable null Stripping
Public int frtscontrol; // RTS Flow Control
Public int fabortonerror; // abort on Error
Public int fdummy2; // Reserved
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; // encoded ed event character
Public ushort wreserved1; // reserved; do not use
}
[Structlayout (layoutkind. Sequential)]
Private struct commtimeouts
{
Public int readintervaltimeout;
Public int readtotaltimeoutmultiplier;
Public int readtotaltimeoutconstant;
Public int writetotaltimeoutmultiplier;
Public int writetotaltimeoutconstant;
}
[Structlayout (layoutkind. Sequential)]
Private struct overlapped
{
Public int internal;
Public int internalhigh;
Public int offset;
Public int offsethigh;
Public int hevent;
}
[Structlayout (layoutkind. Sequential)]
Private struct COMSTAT
{
/* Public int fctshold;
Public int fdsrhold;
Public int frlsdhold;
Public int fxoffhold;
Public int fxoffsent;
Public int feof;
Public int ftxim;
Public int freserved;
Public int cbinque;
Public int cboutque ;*/
// Shocould have a reverse, I don't know why !!!!!
Public int cboutque;
Public int cbinque;
Public int freserved;
Public int ftxim;
Public int feof;
Public int fxoffsent;
Public int fxoffhold;
Public int frlsdhold;
Public int fdsrhold;
Public int fctshold;
}
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern int createfile (
String lpfilename, // file name
Uint dwdesiredaccess, // Access Mode
Int dww.mode, // share mode
Int lpsecurityattributes, // SD
Int dwcreationdisposition, // how to create
Int dwflagsandattributes, // file attributes
Int htemplatefile // handle to Template File
);
# Else
[Dllimport ("coredll")]
Private Static extern int createfile (
String lpfilename, // file name
Uint dwdesiredaccess, // Access Mode
Int dww.mode, // share mode
Int lpsecurityattributes, // SD
Int dwcreationdisposition, // how to create
Int dwflagsandattributes, // file attributes
Int htemplatefile // handle to Template File
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool getcommstate (
Int hfile, // handle to communications device
Ref DCB lpdcb // device-Control Block
);
# Else
[Dllimport ("coredll")]
Private Static extern bool getcommstate (
Int hfile, // handle to communications device
Ref DCB lpdcb // device-Control Block
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool buildcommdcb (
String lpdef, // device-control string
Ref DCB lpdcb // device-Control Block
);
# Else
[Dllimport ("coredll")]
Private Static extern bool buildcommdcb (
String lpdef, // device-control string
Ref DCB lpdcb // device-Control Block
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool setcommstate (
Int hfile, // handle to communications device
Ref DCB lpdcb // device-Control Block
);
# Else
[Dllimport ("coredll")]
Private Static extern bool setcommstate (
Int hfile, // handle to communications device
Ref DCB lpdcb // device-Control Block
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool getcommtimeouts (
Int hfile, // handle to comm device
Ref commtimeouts lpcommtimeouts // time-out values
);
# Else
[Dllimport ("coredll")]
Private Static extern bool getcommtimeouts (
Int hfile, // handle to comm device
Ref commtimeouts lpcommtimeouts // time-out values
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool setcommtimeouts (
Int hfile, // handle to comm device
Ref commtimeouts lpcommtimeouts // time-out values
);
# Else
[Dllimport ("coredll")]
Private Static extern bool setcommtimeouts (
Int hfile, // handle to comm device
Ref commtimeouts lpcommtimeouts // time-out values
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
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
);
# Else
[Dllimport ("coredll")]
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
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
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
);
# Else
[Dllimport ("coredll")]
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
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool closehandle (
Int hobject // handle to object
);
# Else
[Dllimport ("coredll")]
Private Static extern bool closehandle (
Int hobject // handle to object
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool clearcommerror (
Int hfile, // handle to file
Ref int lperrors,
Ref COMSTAT lpstat
);
# Else
[Dllimport ("coredll")]
Private Static extern bool clearcommerror (
Int hfile, // handle to file
Ref int lperrors,
Ref COMSTAT lpstat
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool purgecomm (
Int hfile, // handle to file
Uint dwflags
);
# Else
[Dllimport ("coredll")]
Private Static extern bool purgecomm (
Int hfile, // handle to file
Uint dwflags
);
# Endif
# If fullframework
[Dllimport ("Kernel32")]
Private Static extern bool setupcomm (
Int hfile,
Int dwinqueue,
Int dwoutqueue
);
# Else
[Dllimport ("coredll")]
Private Static extern bool setupcomm (
Int hfile,
Int dwinqueue,
Int dwoutqueue
);
# Endif
# Endregion
// SerialPort member variable
Private int hcomm = invalid_handle_value;
Private bool bopened = false;
Public bool opened
{
Get
{
Return bopened;
}
}
/// <Summary>
/// Serial port initialization Function
/// Lpfilename port name
/// Baudrate baud rate
/// Parity check bit
/// Bytesize data bit
/// Stopbits stop bit
/// <Summary>
Public bool openport (string lpfilename, int baudrate, byte parity, byte bytesize, byte stopbits)
{
// Open the COMM port.
Hcomm = createfile (lpfilename, generic_read | generic_write, 0, 0, open_existing, 0, 0 );
// If the port cannot be opened, bail out.
If (hcomm = invalid_handle_value)
{
Return false;
}
Setupcomm (hcomm, maxblock, maxblock );
// set the comm timeouts.
commtimeouts ctocomatrix = new commtimeouts ();
getcommtimeouts (hcomm, ref ctocomatrix);
ctocomatrix. readintervaltimeout = int32.maxvalue;
ctocommp ort. readtotaltimeoutconstant = 0;
ctocommp ort. readtotaltimeoutmultiplier = 0;
ctocommp ort. writetotaltimeoutmultiplier = 10;
ctocommp ort. writetotaltimeoutconstant = 1000;
setcommtimeouts (hcomm, ref ctocommp ORT);
// Set baud rate, parity, word size, and stop bits.
// There are other ways of doing setting these but this is the easiest.
// If You Want To later add code for other baud rates, remember
// That the argument for buildcommdcb must be a pointer to a string.
// Also note that buildcommdcb () defaults to no handshaking.
DCB dcbcommp ORT = new DCB ();
Dcbcommort. dcblength = marshal. sizeof (dcbcommort );
Getcommstate (hcomm, ref dcbcommp ORT );
Dcbcommp ORT. baudrate = baudrate;
Dcbcommp ORT. Parity = parity;
Dcbcommp ORT. bytesize = bytesize;
Dcbcommp ORT. stopbits = stopbits;
Setcommstate (hcomm, ref dcbcommp ORT );
Purgecomm (hcomm, purge_rxclear | purge_rxabort );
Purgecomm (hcomm, purge_txclear | purge_txabort );
Bopened = true;
Return true;
}
// Close the serial port
Public bool closeport ()
{
If (hcomm = invalid_handle_value)
{
Return false;
}
If (closehandle (hcomm ))
{
Hcomm = invalid_handle_value;
Bopened = false;
Return true;
}
Else
{
Return false;
}
}
// Write data to the serial port
Public bool writeport (byte [] writebytes)
{
If (hcomm = invalid_handle_value)
{
Return false;
}
COMSTAT = new COMSTAT ();
Int dwerrorflags = 0;
Clearcommerror (hcomm, ref dwerrorflags, ref COMSTAT );
If (dwerrorflags! = 0)
Purgecomm (hcomm, purge_txclear | purge_txabort );
Overlapped ovlcommp ORT = new overlapped ();
Int byteswritten = 0;
Return writefile (hcomm, writebytes, writebytes. length, ref byteswritten, ref ovlcommp ORT );
}
// Read data from the serial port
Public bool readport (INT numbytes, ref byte [] commread)
{
If (hcomm = invalid_handle_value)
{
Return false;
}
COMSTAT = new COMSTAT ();
Int dwerrorflags = 0;
Clearcommerror (hcomm, ref dwerrorflags, ref COMSTAT );
If (dwerrorflags! = 0)
Purgecomm (hcomm, purge_rxclear | purge_rxabort );
If (COMSTAT. cbinque> 0)
{
Overlapped ovlcommp ORT = new overlapped ();
Int bytesread = 0;
Return readfile (hcomm, commread, numbytes, ref bytesread, ref ovlcommp ORT );
}
Else
{
Return false;
}
}
}
}