- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
[C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn code cách đọc thông số của thiết bị kiêm tra pin (HIOKI BATTERY HiTESTER BT3562) qua Serial port C# winform.
[C#] Đọc thông số thiết bị kiểm tra pin (HIOKI BATTERY HiTESTER BT3562)
Video demo ứng dụng:
Source code C#:
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
namespace Sample
{
public partial class Form1
{
private string MsgBuf = "";
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
SerialPort1.PortName = TextBox1.Text;
SerialPort1.BaudRate = 9600;
SerialPort1.NewLine = Constants.vbCrLf;
SerialPort1.Handshake = System.IO.Ports.Handshake.None;
btnMeasure.Enabled = false;
try
{
SerialPort1.Open();
// SendMsg("*RST") 'Reset
SendMsg(":TRIGger EXTernal"); // Trigger:External
SendMsg(":COMParator ON");
// SendMsg(":FREQuency 200E+03") 'Frequency:200kHz
SendMsg("*TRG"); // Trigger
SendQueryMsg(":MEASure?"); // Acquire measured value
CustomShowMessage(MsgBuf); // Display measured value
}
catch (Exception ex)
{
Interaction.MsgBox(ex.Message);
}
if (SerialPort1.IsOpen)
{
try
{
SerialPort1.Close();
}
catch (Exception ex)
{
Interaction.MsgBox(ex.Message);
}
}
btnMeasure.Enabled = true;
}
private void CustomShowMessage(string msgBuf)
{
if(msgBuf.Length > 10)
{
var temp = msgBuf.Split(new char[] { ' ' });
var temp2 = temp[1].Split('E')[0];
txtValue.Text = temp2;
var currentValue = double.Parse(temp2);
if(txtMin.Text .Length > 0 && txtMax.Text.Length >0) {
var min = double.Parse(txtMin.Text);
var max = double.Parse(txtMax.Text);
if (currentValue < min) {
MessageBox.Show($"Current value: {currentValue} < {min}");
return;
}
if (currentValue > max)
{
MessageBox.Show($"Current value: {currentValue} > {min}");
return;
}
}
}
}
private void SendMsg(string strMsg)
{
try
{
strMsg = strMsg + Constants.vbCrLf;
SerialPort1.WriteLine(strMsg);
}
catch (Exception Ex)
{
Interaction.MsgBox(Ex.Message);
}
}
private void SendQueryMsg(string strMsg)
{
try
{
SendMsg(strMsg);
int Check;
string a;
MsgBuf = null;
MsgBuf = SerialPort1.ReadByte().ToString();
do // Wait until response is received
{
Check = SerialPort1.ReadByte();
if (Conversions.ToString(Strings.Chr(Check)) == Constants.vbLf)
{
break;
}
else if (Conversions.ToString(Strings.Chr(Check)) == Constants.vbCr)
{
}
else
{
MsgBuf = MsgBuf + Strings.Chr(Check);
}
}
while (true);
}
catch (Exception Ex)
{
Interaction.MsgBox(Ex.Message);
}
}
}
}
Thanks for watching!