使用SMSlib问题重新启动服务

时间:2014-01-03 23:07:58

标签: c# sockets service smslib

我目前面临与SMSlib for .NET库相关的问题(您可以在http://www.smslib.org/下载) 顺便说一下,我在库中找到了SendMessage示例(你可以在... \ Examples \ SendMessage中找到它),并尝试使用VS2012编译并运行它...它运行正常,但是当我尝试重新启动时它从未成功添加一点代码后重新启动服务...

您知道如何正确重启服务吗?

btw部分代码如下:

    try
        {
            Console.WriteLine("Example: Read messages from a serial gsm modem.");
            Console.WriteLine(Library.getLibraryDescription());
            Console.WriteLine("Version: " + Library.getLibraryVersion());

            // Start the COM listening thread.
            new Thread(new ThreadStart(com1.Run)).Start();

            // Lets set some callbacks.
            srv.setInboundMessageNotification(new InboundNotification());
            srv.setCallNotification(new CallNotification());
            srv.setGatewayStatusNotification(new GatewayStatusNotification());

            // Create the Gateway representing the serial GSM modem.
            // Due to the Comm2IP bridge, in SMSLib for .NET all modems are considered IP modems.
            IPModemGateway gateway = new IPModemGateway("modem.com14", "127.0.0.1", 12000, "Huawei","E173");
            gateway.setIpProtocol(ModemGateway.IPProtocols.BINARY);

            // Set the modem protocol to PDU (alternative is TEXT). PDU is the default, anyway...
            gateway.setProtocol(AGateway.Protocols.PDU);

            // Do we want the Gateway to be used for Inbound messages?
            gateway.setInbound(true);

            // Do we want the Gateway to be used for Outbound messages?
            gateway.setOutbound(true);



            // Add the Gateway to the Service object.
            srv.addGateway(gateway);

            // Similarly, you may define as many Gateway objects, representing
            // various GSM modems, add them in the Service object and control all of them.

            // Start! (i.e. connect to all defined Gateways)
            srv.startService();

            // Printout some general information about the modem.
            Console.WriteLine();
            Console.WriteLine("Modem Information:");
            Console.WriteLine("  Manufacturer: " + gateway.getManufacturer());
            Console.WriteLine("  Model: " + gateway.getModel());
            Console.WriteLine("  Serial No: " + gateway.getSerialNo());
            Console.WriteLine("  SIM IMSI: " + gateway.getImsi());
            Console.WriteLine("  Signal Level: " + gateway.getSignalLevel() + "dBm");
            Console.WriteLine("  Battery Level: " + gateway.getBatteryLevel() + "%");
            Console.WriteLine();

            // Send one message.
            // Remember to change the recipient!
            OutboundMessage msg = new OutboundMessage("0952998989", "Hello from SMSLib for .NET");
            srv.sendMessage(msg);
            Console.WriteLine(msg);



            Console.WriteLine("Press <ENTER> to terminate...");
            Console.In.ReadLine();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.StackTrace);
        }
        finally
        {
            com1.Stop();
            srv.stopService();

            // Start! (i.e. connect to all defined Gateways)
            srv.startService();
            new Thread(new ThreadStart(com1.Run)).Start();
        }

1 个答案:

答案 0 :(得分:0)

在您完成启动/停止服务之前,不应该致电com1.Stop();。让听众保持活跃状态​​。

E.g。

finally
{
  srv.stopService();
  srv.startService();
  srv.stopService();
  srv.startService();
  srv.stopService();
  srv.startService();
  srv.stopService();
  srv.startService();
  srv.stopService();
  // ... and finally ...
  com1.Stop();
}