c# - 配置通过 GSM 调制解调器发送 SMS 的编码
问题描述
我必须使用保加利亚语发送短信,如果您需要发送英文短信,我会使用可以正常工作的项目(https://www.codeproject.com/Articles/38705/Send-and-Read-SMS-through-a -GSM-调制解调器-使用-AT-Com)。
所以我像打开 Srrial 端口
public SerialPort OpenPort(string p_strPortName, int p_uBaudRate, int p_uDataBits, int p_uReadTimeout, int p_uWriteTimeout)
{
receiveNow = new AutoResetEvent(false);
SerialPort port = new SerialPort();
try
{
port.PortName = p_strPortName; //COM1
port.BaudRate = p_uBaudRate; //9600
port.DataBits = p_uDataBits; //8
port.StopBits = StopBits.One; //1
port.Parity = Parity.None; //None
port.ReadTimeout = p_uReadTimeout; //300
port.WriteTimeout = p_uWriteTimeout; //300
port.Encoding = Encoding.GetEncoding("windows-1251");
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
port.DtrEnable = true;
port.RtsEnable = true;
}
catch (Exception ex)
{
throw ex;
}
return port;
}
我发送来自 MS SQL Server (nvarchar) 的 SMS 文本,例如
public bool sendMsg(SerialPort port, string PhoneNo, string Message)
{
bool isSend = false;
try
{
string recievedData = ExecCommand(port,"AT", 300, "No phone connected");
recievedData = ExecCommand(port,"AT+CMGF=1", 300, "Failed to set message format.");
String command = "AT+CMGS=\"" + PhoneNo + "\"";
recievedData = ExecCommand(port,command, 300, "Failed to accept phoneNo");
command = Message + char.ConvertFromUtf32(26) + "\r";
recievedData = ExecCommand(port,command, 3000, "Failed to send message"); //3 seconds
if (recievedData.EndsWith("\r\nOK\r\n"))
{
isSend = true;
}
else if (recievedData.Contains("ERROR"))
{
isSend = false;
}
return isSend;
}
catch (Exception ex)
{
throw ex;
}
}
//Execute AT Command
public string ExecCommand(SerialPort port,string command, int responseTimeout, string errorMessage)
{
try
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
receiveNow.Reset();
port.Write(command + "\r");
string input = ReadResponse(port, responseTimeout);
if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n"))))
throw new ApplicationException("No success message was received.");
return input;
}
catch (Exception ex)
{
throw ex;
}
}
但我看不到普通文本,它看起来像 abrakadabra ?????? 等等。
请帮我正确编码文本,以便我收到可红色的短信。
谢谢!
------- 用于澄清问题的额外代码 ------------------------- ----
public string ReadResponse(SerialPort port,int timeout)
{
string buffer = string.Empty;
try
{
do
{
if (receiveNow.WaitOne(timeout, false))
{
string t = port.ReadExisting();
buffer += t;
}
else
{
if (buffer.Length > 0)
throw new ApplicationException("Response received is incomplete.");
else
throw new ApplicationException("No data received from phone.");
}
}
while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
}
catch (Exception ex)
{
throw ex;
}
return buffer;
}
public AutoResetEvent receiveNow;
//Receive data from port
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (e.EventType == SerialData.Chars)
{
receiveNow.Set();
}
}
catch (Exception ex)
{
throw ex;
}
}
解决方案
我找到了尽快实施的最佳答案。
https://github.com/welly87/GSMComm
GsmCommMain comm=new GsmCommMain(/*Set your option here*/);
string txtMessage="your long message...";
string txtDestinationNumbers="your destination number";
//select unicode option by a checkBox or any other control
bool unicode = chkUnicode.Checked;
SmsSubmitPdu[] pdu = SmartMessageFactory.CreateConcatTextMessage(txtMessage, unicode, txtDestinationNumbers);
сomm.SendMessages(pdu);
推荐阅读
- vue.js - HTML5 画布工件
- ios - 在 UICollectionView 中点击单元格返回错误的索引
- java - 以编程方式获取 GPU 信息有时会给出“createcontext failed egl_bad_alloc”错误
- javascript - Firebase 电子邮件验证,无需在 React Native 中创建帐户
- mysql - 最后一个值的 SQL 命令。另外带有搜索 ID
- html - 使用 this.state 将属性分配给 HTML 标记
- intellij-idea - 没有找到想法`org.jetbrains.plugins.go`
- python - 新数据框中的 df.loc 值
- c# - 即使连接没有关闭,临时表也不存在
- javascript - 未设置 Firebase 自定义用户声明