Using system;
Using system. Io. ports;
Using system. text;
Using system. Threading;
Namespace weight
{
/// <Summary>
/// Electronic scaling Interface Information Class, encapsulation of comport data
/// </Summary>
Public class weightinformation
{
String _ wdata;
String _ wunit;
String _ qdata;
String _ qunit;
String _ percentage;
/// <Summary>
/// Obtain or set the weight
/// </Summary>
Public String wdata {get {return this. _ wdata;} set {This. _ wdata = value ;}}
/// <Summary>
/// Get or set the weight unit
/// </Summary>
Public String wunit {get {return this. _ wunit;} set {This. _ wunit = value ;}}
/// <Summary>
/// Obtain or set the quantity
/// </Summary>
Public String qdata {get {return this. _ qdata;} set {This. _ qdata = value ;}}
/// <Summary>
/// Obtain or set the Quantity Unit
/// </Summary>
Public String qunit {get {return this. _ qunit;} set {This. _ qunit = value ;}}
/// <Summary>
/// Get or set the percentage
/// </Summary>
Public String percentage {get {return this. _ percentage;} set {This. _ percentage = value ;}}
}
/// <Summary>
/// Electronic Name Data Reading Class
/// </Summary>
Public class weightreader: idisposable
{
# Region fields, attributes, and constructors
SerialPort sp;
Int _ speed = 300;
/// <Summary>
/// Obtain or set the time for the computer to buffer com data, in milliseconds
/// </Summary>
Public int speed
{
Get
{
Return this. _ speed;
}
Set
{
If (value <300)
Throw new exception ("the serial port read buffer time cannot be less than 300 milliseconds! ");
This. _ speed = value;
}
}
Public bool initcom (string portname)
{
Return this. initcom (portname, 4800,300 );
}
/// <Summary>
/// Initialize the serial port
/// </Summary>
/// <Param name = "portname"> data transmission port </param>
/// <Param name = "baudrate"> baud rate </param>
/// <Param name = "Speed"> serial port reading buffer time </param>
/// <Returns> </returns>
Public bool initcom (string portname, int baudrate, int speed)
{
Try
{
SP = new SerialPort (portname, baudrate, parity. None, 8 );
Sp. receivedbytesthreshold = 10;
Sp. Handshake = handshake. requesttosend;
Sp. Parity = parity. None;
Sp. readtimeout = 600;
Sp. writetimeout = 600;
This. speed = speed;
If (! Sp. isopen)
{
Sp. open ();
}
Return true;
}
Catch
{
Throw new exception (string. Format ("unable to initialize serial port {0 }! ", Portname ));
}
}
# Endregion
# region Serial Port Data Reading method
Public weightinformation readinfo ()
{< br> string src = This. readcom ();
weightinformation info = new weightinformation ();
info. wdata = This. decodeweightdata (SRC);
info. wunit = This. decodeweightunit (SRC);
info. percentage = This. decodepercentage (SRC);
info. qdata = This. decodequalitydata (SRC);
info. qunit = This. decodequalityunit (SRC);
Return Info;
}
/// <Summary>
/// Read all the data cached by the comport
/// </Summary>
/// <Returns> </returns>
Private string readcom () // return information
{
If (this. sp. isopen)
{
Thread. Sleep (this. _ speed );
String res = "";
// For (INT I = 0; I <5; I ++)
//{
Byte [] buffer = new byte [sp. bytestoread];
Sp. Read (buffer, 0, buffer. Length );
Res = system. Text. encoding. ASCII. getstring (buffer );
// If (res! = "")
// Break;
//}
If (RES = "")
{
Throw new exception ("the data read from the serial port is empty. Is the parameter set correctly! ");
}
Return res;
}
Return "";
}
# Endregion
# Region Basic fetch Method
/// <Summary>
/// Value from the string
/// </Summary>
/// <Param name = "head"> Start string </param>
/// <Param name = "intervallen"> Interval Length </param>
/// <Param name = "valuelen"> value length </param>
/// <Param name = "src"> source string </param>
/// <Returns> </returns>
Private string decodevalue (string head, int intervallen, int valuelen, string SRC)
{
Int Index = SRC. indexof (head );
Return SRC. substring (index + intervallen, valuelen );
}
/// <Summary>
/// Weight
/// </Summary>
/// <Param name = "srcstring"> source string </param>
/// <Returns> </returns>
Private string decodeweightdata (string srcstring)
{
Return this. decodevalue ("Gs,", 3, 8, srcstring );
}
/// <Summary>
/// Weight unit
/// </Summary>
/// <Param name = "srcstring"> source string </param>
/// <Returns> </returns>
Private string decodeweightunit (string srcstring)
{
Return this. decodevalue ("Gs,", 12, 2, srcstring );
}
/// <Summary>
/// Percentage
/// </Summary>
/// <Param name = "srcstring"> source string </param>
/// <Returns> </returns>
Private string decodepercentage (string srcstring)
{
Return this. decodevalue ("U. W.", 4, 14, srcstring );
}
/// <Summary>
/// Obtain the quantity
/// </Summary>
/// <Param name = "srcstring"> source string </param>
/// <Returns> </returns>
Private string decodequalitydata (string srcstring)
{
Return this. decodevalue ("PCs", 3, 9, srcstring );
}
/// <Summary>
/// Number Unit
/// </Summary>
/// <Param name = "srcstring"> source string </param>
/// <Returns> </returns>
Private string decodequalityunit (string srcstring)
{
Return this. decodevalue ("PCs", 12, 3, srcstring );
}
# Endregion
# Region release all resources
Public void dispose ()
{
If (SP! = NULL & sp. isopen)
{
Sp. Close ();
}
}
# Endregion
}
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/maomaoawen/archive/2009/06/20/4284514.aspx