- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Encode và Decode Text to PDU trong lập trình csharp
Bài viết hôm nay, mình sẽ hướng dẫn các bạn cách mã hóa và giải mã PDU trong lập trình C# sử dụng thư viện SMSPDULib
.
[C#] Encode and Decode PDU Winform
PDU là một mã code được sử dụng trong lĩnh vực viễn thông, nếu các bạn nào đã tìm hiểu về lập trình sử dụng tập lệnh AT command để gởi tin nhắn SMS, thì các bạn sẽ thấy PDU này.
Mục đích của bài viết này, là mình giúp các bạn nào đang lập trình gởi SMS với thiết bị DCom 3G hoặc thiết bị chuyên dụng GSM Modem.
Nếu các bạn muốn gởi nội dung tin nhắn dưới dạng Unicode có dấu tiếng việt, thì các bạn cần chuyển đổi Text sang chế độ PDU, để gởi tin nhắn theo chế độ PDU Mode.
Các bạn có thể tìm hiểu thêm về PDU ở link bên dưới:
http://www.gsm-modem.de/sms-pdu-mode.html
Giao diện demo ứng dụng:
Đầu tiên, các bạn cần Import thư viện SMSPDULib
vào, các bạn có thể lấy thư viện ở source code C# mình gởi bên dưới.
Full Source code C# Encode and Decode PDU Mode:
using SMSPDULib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PDU_Decode_Encode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_encode_Click(object sender, EventArgs e)
{
SMS sms = new SMS();
sms.Direction = SMSDirection.Submited;
sms.PhoneNumber = "+84933913122";
sms.ValidityPeriod = new TimeSpan(4, 0, 0, 0);
sms.Message = txt_text.Text;
txt_pdu.Text = sms.Compose(SMS.SMSEncoding.UCS2);
}
private void btn_decode_Click(object sender, EventArgs e)
{
string source = txt_pdu.Text;
SMSType smsType = SMSBase.GetSMSType(source);
switch (smsType)
{
case SMSType.SMS:
SMS sms = new SMS();
SMS.Fetch(sms, ref source);
ShowSMS(sms);
break;
case SMSType.StatusReport:
SMSStatusReport statusReport = new SMSStatusReport();
SMSStatusReport.Fetch(statusReport, ref source);
ShowStatusReport(statusReport);
break;
}
}
private void ShowStatusReport(SMSStatusReport sms)
{
string format = "Service center number: {0}
Service center time stamp: {1}
Message reference #: {2}
Phone number: {3}
Report time stamp: {4}
Report status: {5}";
txt_text.Text = string.Format(format,
sms.ServiceCenterNumber,
sms.ServiceCenterTimeStamp,
sms.MessageReference,
sms.PhoneNumber,
sms.ReportTimeStamp,
sms.ReportStatus);
}
private void ShowSMS(SMS sms)
{
string format = "Service center number: {0}
Service center time stamp: {1}
Message reference #: {2}
Direction: {3}
Phone number: {4}
Status report indication: {5}
Message:
{6}";
txt_text.Text = string.Format(format,
sms.ServiceCenterNumber,
sms.ServiceCenterTimeStamp,
sms.MessageReference,
sms.Direction,
sms.PhoneNumber,
sms.StatusReportIndication,
sms.Message);
}
}
}
HAPPY CODING