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

電表編程允許

發布時間: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();
}
}

閱讀全文

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

熱點內容
滴滴號碼加密怎麼解除 瀏覽:844
模具編程的職責 瀏覽:941
華為ssh改加密演算法 瀏覽:147
文件夾空白合同 瀏覽:761
pythonwebpy開發 瀏覽:669
不是c編譯器的有 瀏覽:660
win10壓縮包下載 瀏覽:905
逆戰手機app怎麼樣 瀏覽:946
自嗨自我解壓圖片 瀏覽:395
電子書導入kindle哪個文件夾 瀏覽:418
pythontcpserver性能 瀏覽:544
linux文件夾改名 瀏覽:564
單片機開發板是什麼 瀏覽:851
阿里雲伺服器不能截屏 瀏覽:866
如何自己製作聯想伺服器 瀏覽:843
停車場規劃演算法 瀏覽:923
深蹲PDF 瀏覽:908
數據科學包python 瀏覽:849
程序員學習視頻網站 瀏覽:125
吃雞游戲如何安卓轉蘋果 瀏覽:188