導航:首頁 > 編程語言 > 電表編程允許

電表編程允許

發布時間:2023-07-20 13:06:32

❶ 如何編程讀取智能電表的數據

方 法:
/// <summary>
/// 只能通過CreateInstance方法來創建類的實例。單例模式
/// </summary>
public static ElectricityMeter CreateInstance()
{
return _instance;
}
/// <summary>
/// 打開設備
/// </summary>
/// <param name="portName">串口號</param>
/// <param name="frm">調用這個類的窗體。</param>
public void Open( string portName, Form frm )
{
try
{
// 初始化窗體對象
_frm = frm;
_frm.FormClosing += new FormClosingEventHandler( _frm_FormClosing );
//初始化SerialPort對象
_serialPort.PortName = portName;
_serialPort.BaudRate = 2400; // 請將設備的波特率設置為此。
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Parity = Parity.Even;
_serialPort.Open();
}
catch( Exception e )
{
MessageBox.Show( e.Message );
}
}
/// <summary>
/// 關閉設備。
/// </summary>
public void Close()
{
if( _serialPort.IsOpen == true )
{
_serialPort.Close();
_serialPort.Dispose();
}
}
/// <summary>
/// 獲取耗電量
/// </summary>
public Decimal GetPowerConsumption()
{
if( _serialPort.IsOpen == true )
{
// 十六進制的命令字元串
string strCmd = "68 AA AA AA AA AA AA 68 11 04 33 33 33 33 AD 16";
// 轉換為十六進制的位元組數組
string[] strs = strCmd.Split( new char[] { ' ' } ); // 空格分組
byte[] cmdBytes = new byte[ strs.Length ];
// 轉換為十進制的位元組數組
for( int i = 0; i < cmdBytes.Length; i++ ) {
cmdBytes[ i ] = Convert.ToByte( strs[ i ], 16 ); // 16進制轉換為10進制
}
_serialPort.Write( cmdBytes, 0, cmdBytes.Length );
System.Threading.Thread.Sleep( 500 ); // 500ms內應當有響應
byte[] resultBytes = new byte[ 21 ]; // 容量為21的位元組數組
_serialPort.Read( resultBytes, 0, resultBytes.Length );
string n1 = Convert.ToString( resultBytes[ 18 ] - 51, 16 ); // 將十進制轉成16進制的字元串
string n2 = Convert.ToString( resultBytes[ 17 ] - 51, 16 ); // 將十進制轉成16進制的字元串
string n3 = Convert.ToString( resultBytes[ 16 ] - 51, 16 ); // 將十進制轉成16進制的字元串
string n4 = Convert.ToString( resultBytes[ 15 ] - 51, 16 ); // 將十進制轉成16進制的字元串
string resultString = n1 + n2 + n3 + "." + n4;
return Decimal.Parse( resultString );
}
else
{
throw new Exception( "串口沒有打開" );
}
}
/// <summary>
/// 在窗體關閉的時候關閉串口連接。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _frm_FormClosing( object sender, FormClosingEventArgs e )
{
this.Close();
}
}

閱讀全文

與電表編程允許相關的資料

熱點內容
python字典獲取值方法 瀏覽:244
android手機安裝失敗 瀏覽:28
雲計算沒有伺服器 瀏覽:67
怎麼顯示android的APP 瀏覽:121
c編譯器怎麼刪除空格 瀏覽:695
php自動釋放內存 瀏覽:219
golang編譯庫 瀏覽:794
oracle數據字元串加密 瀏覽:603
研究生去上海當程序員 瀏覽:90
u8電腦伺服器連接失敗怎麼解決 瀏覽:569
bat腳本創建日期命名文件夾 瀏覽:104
將圖片轉換為pdf格式 瀏覽:980
java中形參 瀏覽:83
枚舉類型編譯器 瀏覽:519
oraclejava包 瀏覽:568
手機定位手機怎麼定位安卓 瀏覽:523
在哪個app買歐萊雅最便宜 瀏覽:495
程序員吃零食好嗎 瀏覽:261
php工程師主要做什麼 瀏覽:356
tvp保存到哪個文件夾 瀏覽:197