1

I have to use Bulgarian language to send SMS and I use thic project that works fine if you need to send English SMS (https://www.codeproject.com/Articles/38705/Send-and-Read-SMS-through-a-GSM-Modem-using-AT-Com).

So I open Srrial port like

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;
        }

I send SMS text that comes from MS SQL Server (nvarchar) like

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;
            }
        }   

But I cannot see normal text it looks like abrakadabra with ?????? and so on.

Please help me encoding text properly so I get redable SMS.

Thank you!

------- Extra Code to clarify the issue -----------------------------------------

 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;
                }
    }
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Can you post the code for `ReadResponse`? Seems like an encoding issue somewhere. – Zer0 Jun 27 '18 at 01:24
  • @Zer0 Hi! Please check the code. In fact it has nothing... I am pretty sure the problem is around encoding... – NoWar Jun 27 '18 at 01:47
  • I meant for this line `string input = ReadResponse(port, responseTimeout);` which returns a `string`. The extra code you posted returns `void`. – Zer0 Jun 27 '18 at 02:01
  • @Zer0 It is there. Pls take a look. – NoWar Jun 27 '18 at 02:17
  • Give this a shot. `port.Encoding = Encoding.UTF8;`. This supports all characters of the encoding you specify and then some. – Zer0 Jun 27 '18 at 02:34
  • @Zer0 Nope... It does not work. – NoWar Jun 27 '18 at 02:51
  • Odd. The `??????` characters you're seeing are likely due to the fact that when using ASCII encoding (the default) anything larger than 127 is encoded as `?`. So I'm pretty sure somewhere the encoding is too small. Any chance you can reproduce with a smaller example? Let me try and repro in the meantime. – Zer0 Jun 27 '18 at 02:57
  • @Zer0 OK. Just download project from provided link and try it. I use HUAWEI USB modem. And it works fine for English. – NoWar Jun 27 '18 at 03:02
  • I'll try that, but the English character set fits in ASCII, which explains why it works. – Zer0 Jun 27 '18 at 03:03
  • @Zer0 Yeah... Pls help to use different encoding for Bulgarian language. – NoWar Jun 27 '18 at 03:11

1 Answers1

2

I found best answer posible to implement ASAP.

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);

How to concat long SMS in GSMComm Library?

NoWar
  • 36,338
  • 80
  • 323
  • 498