1

我使用 mobitek gsm 调制解调器,它使用的源代码是在 VB 中。现在我想将代码转换为 c#。我遇到问题的代码是intModemStatus = SMS.ModemInit(frmModem.txtPort.Text, "")。之后,代码将通过以下选择案例进行:

intModemStatus = SMS.ModemInit(frmModem.txtPort.Text, "")

    Select Case intModemStatus

        Case 0
            FrmModem.txtText.Text = "GSM Modem Not Connected!"
            '[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
            Exit Sub

        Case 1
            FrmModem.txtText.Text = "CONNECTED!"
            '[VB - Module1] frmModem.txtText = "GSM Modem Connected!"
            Exit Sub

        Case 2
            FrmModem.txtText.Text = "PIN Required!"
            '[VB - Module1] frmModem.txtText = "PIN Required!"
            Exit Sub

        Case 3
            FrmModem.txtText.Text = "Incorrect PIN Entered! Warning after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!"
            '[VB - Module1] frmModem.txtText = "Incorrect PIN entered! Warning: after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!"
            Exit Sub

        Case 4
            FrmModem.txtText.Text = "Your SIM card is blocked by TELCO!"
            '[VB - Module1] frmModem.txtText = "Your SIM card is blocked by TELCO!"
            Exit Sub

        Case 5
            FrmModem.txtText.Text = "Your SIM card has problem!"
            '[VB - Module1] frmModem.txtText = "Your SIM card has problem!"
            Exit Sub

        Case Else
            FrmModem.txtText.Text = "GSM Modem Not Connected!"
            '[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
            Exit Sub

    End Select

我已将所有内容都转换为 c#,包括这样的开关盒:

int ModemStatus = sms.ModemInit(txtPort.Text, "");
        switch (intModemStatus)
        {
            case 0:

                txtText.Text = "GSM Modem Not Connected!";
                //[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
                return;

                break;
            case 1:
                txtText.Text = "CONNECTED!";
                //[VB - Module1] frmModem.txtText = "GSM Modem Connected!"
                return;


                break;
            case 2:
                txtText.Text = "PIN Required!";
                //[VB - Module1] frmModem.txtText = "PIN Required!"
                return;


                break;
            case 3:
                txtText.Text = "Incorrect PIN Entered! Warning after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!";
                //[VB - Module1] frmModem.txtText = "Incorrect PIN entered! Warning: after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!"
                return;


                break;
            case 4:
                txtText.Text = "Your SIM card is blocked by TELCO!";
                //[VB - Module1] frmModem.txtText = "Your SIM card is blocked by TELCO!"
                return;


                break;
            case 5:
                txtText.Text = "Your SIM card has problem!";
                //[VB - Module1] frmModem.txtText = "Your SIM card has problem!"
                return;


                break;
            default:
                txtText.Text = "GSM Modem Not Connected!";
                //[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
                return;


                break;
        }

但是,我在使用此代码时遇到了问题int ModemStatus = sms.ModemInit(txtPort.Text, "");。它说

参数 1 无法将字符串转换为短字符串。mobitekSMSAPI5.ModemInit(short, string) 的最佳重载方法匹配有一些无效参数。

然后我试图改变int ModemStatus = sms.ModemInit(txtPort.Text, "");,但它说的一样。

要使用 mobitek gsm 调制解调器,我需要添加 MobitekSMSAPI5 的引用,我做到了。开关代码将确定调制解调器是否已连接。

我真的希望有人能加紧解决这个问题。我卡在中间,我不知道从哪里开始。任何形式的帮助表示赞赏。谢谢你。

这是我的错误:当我使用此代码时,它会出现:

 short port;
if (!short.TryParse(txtPort.Text, out port))
{
    throw new Exception("Failed to parse port");
    // or any other handling - depends on your needs
}

int ModemStatus = sms.ModemInit(port, "");

在此处输入图像描述

现在,当我像下面这样更改代码时,它会出现不同的错误。

在此处输入图像描述

4

4 回答 4

6

sms.ModemInit接受short作为第一个参数。只要您正在处理 VB.Net,字符串到短的转换是隐式完成的。这可能是由于编译器的Option Strict选项,默认情况下是禁用的。启用此选项后,仅允许隐式扩展转换。禁用时(默认状态),此选项允许隐式缩小和扩大转换

然而,在 C# 中,禁止缩小隐式转换,这就是您的翻译代码失败的原因。因此,您需要string显式解析您的值并将解析后的数字传递给该方法:

short port = short.Parse(txtPort.Text);
int ModemStatus = sms.ModemInit(port, "");

或者,更好的是,使用 TryParse 来避免可能的异常:

short port;
if (!short.TryParse(txtPort.Text, out port))
{
    throw new Exception("Failed to parse port");
    // or any other handling - depends on your needs
}

int ModemStatus = sms.ModemInit(port, "");
于 2012-09-24T12:04:27.167 回答
1

你的问题只是几个铸造问题。第一个与端口号有关,该ModemInit方法需要一个short值,但您传递了 a string,因此您已经通过使用short.TryParse.

另一个问题是您的返回类型,该ModemInit方法似乎返回它自己的自定义enum值,如果您只对整数值感兴趣,那么您只需将其转换为int.

int ModemStatus = (int)sms.ModemInit(port, "");
于 2012-09-24T13:17:52.510 回答
1

我会这样做:

short shortValue = 0;
if (short.TryParse(txtPort.Text, out shortValue))
{
    ... continue using shortValue
}
else
{
    ...Tell user the value must be a number
}

通过这种方式,您可以处理用户输入非数字的情况(不诉诸例外)

于 2012-09-24T12:06:40.453 回答
0

正如错误明确指出的那样,您不能将字符串作为短字符串传递。

你需要打电话short.Parse()

于 2012-09-24T12:03:21.630 回答