0
votes

I am sending sms via serial Port but receiving ????. Please help me to receive sms properly and in bulk. Below is my code:

SmsHandler.SendSMS("hello world",phonenumber,"USB Modem");

   public static void SendSMS(String smstxt, String PhoneNumber, String DeviceName)
    {
       _serialPort = new SerialPort(Port.getPort(DeviceName), 19200, Parity.None, 8, StopBits.One);
        _serialPort.Handshake = Handshake.None;
        _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
        _serialPort.ReadTimeout = 2000;
        _serialPort.WriteTimeout = 2000;
        _serialPort.Open();
        try
        {
            if (!_serialPort.IsOpen)
                _serialPort.Open();
            _serialPort.Write("AT+CMGF=1\r\n");
            Thread.Sleep(1000);
            _serialPort.Write("AT+CSCA=SERVICE\r\n");// Service Center  
            Thread.Sleep(1000);
            _serialPort.Write("AT+CMGS=\"" + PhoneNumber + "\"" + Environment.NewLine);
            _serialPort.Write(smstxt + char.ConvertFromUtf32(26) + Environment.NewLine);


            MessageBox.Show("Message Sent");

        }
        catch (Exception ex)
        {
            MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
        }
    }
    static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        Thread.Sleep(500);
        string data = _serialPort.ReadLine();
        Console.Write(data);
    }

I am getting garbage at receiver mobile. Please help to resolve the problem. Thankx

3

3 Answers

2
votes

This is three months late, but it might help someone reading this in the future.

To send an SMS via a serial port, you need to convert your text into an hexadecimal representation of your message then send the result instead. For example :

_serialPort.Write(StrToHex(smstxt) + char.ConvertFromUtf32(26) + Environment.NewLine);

And the StrToHex Method can be something like :

public static string StrToHex(string strMessage)
{
    byte[] byteArray = Encoding.UTF8.GetBytes(strMessage);
    string strHex = BitConverter.ToString(byteArray);
    strHex = strHex.Replace("-", "");
    return strHex;
}
0
votes

Perhaps this is related to encoding of the serial port?

Maybe try a different encoding:

 _serialPort.Encoding = Encoding.GetEncoding("iso-8859-1");

Also I could recommend you to look into: http://www.scampers.org/steve/sms/libraries.htm - it features a simple API for doing AT-commands.

0
votes

try to change the console font , use a TrueType font like Lucida Console instead of the default Raster Font as the first one doesn't support Uni characters.