ss_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r"));//获取手机短信中心号
string centerNumber = Encoding.ASCII.GetString(ss_port.Read(128));//第一次循环结果为AT+CSCA?+CSCA: "+8613010180500",145 OK//这确
if (centerNumber.Length > 0)
{
  centerNumber = centerNumber.Substring(20, 13);
}
string decodedSMS = sms.smsDecodedsms(centerNumber, phoneNumber, content);//第一次循环结果0891683110100805F011000D91683105238054F2000800046D4B8BD5//正确
byte[] buf = Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r", sms.nLength));
ss_port.Write(buf);
string response = Encoding.ASCII.GetString(ss_port.Read(128));
if (response.Length > 0 && response.EndsWith("> "))
{
  ss_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a", decodedSMS)));
  SendState = true;
}第二次获取中心号码结果:string centerNumber=0891683110100805F011000D91683105238054F2000800046D4B8BD5 ERROR AT+CSCA? +CSCA: "+8613010180500",145 OK好像是第一次的内容加上AT指令+发回结果了。这是什么问题。。在线等。。

解决方案 »

  1.   

    第二次出错误了?
    返回值里有ERROR. 
      

  2.   

    public void Open()
            {
                DCB dcbCommPort = new DCB();
                COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
                // 打开串口 OPEN THE COMM PORT.
                hComm = CreateFile(PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
                // 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
                if (hComm == INVALID_HANDLE_VALUE)
                {
                    throw (new ApplicationException("非法操作,不能打开串口!"));
                }
                // 设置通信超时时间 SET THE COMM TIMEOUTS.
                GetCommTimeouts(hComm, ref ctoCommPort);
                ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
                ctoCommPort.ReadTotalTimeoutMultiplier = 0;
                ctoCommPort.WriteTotalTimeoutMultiplier = 0;
                ctoCommPort.WriteTotalTimeoutConstant = 0;
                SetCommTimeouts(hComm, ref ctoCommPort);
                // 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
                GetCommState(hComm, ref dcbCommPort);
                //dcbCommPort.fOutxCtsFlow = 524800;
                dcbCommPort.BaudRate = BaudRate;
                dcbCommPort.flags = 0;
                //dcb.fBinary=1;
                dcbCommPort.flags |= 1;
                if (Parity > 0)
                {
                    //dcb.fParity=1
                    dcbCommPort.flags |= 2;
                }
                dcbCommPort.Parity = Parity;
                dcbCommPort.ByteSize = ByteSize;
                dcbCommPort.StopBits = StopBits;
                if (!SetCommState(hComm, ref dcbCommPort))
                {
                    CloseHandle(hComm);
                    throw (new ApplicationException("非法操作,不能打开串口!"));
                }
                Opened = true;
            }//dcbCommPort.fOutxCtsFlow = 524800;一开始是没有这句的。但没有这句获取不到中心号码,需要用别的出口工具打开一下再关闭才能获取到。发送正常。但加了这句了就出问题了
      

  3.   

    string response = Encoding.ASCII.GetString(ss_port.Read(128));
      

  4.   


    ss_port.Write(Encoding.ASCII.GetBytes("AT+CSCA?\r"));//获取手机短信中心号
    string centerNumber = Encoding.ASCII.GetString(ss_port.Read(128));
    if (centerNumber.Length > 0)
    {
      centerNumber = centerNumber.Substring(20, 13);
    }
    string decodedSMS = sms.smsDecodedsms(centerNumber, phoneNumber, content);
    byte[] buf = Encoding.ASCII.GetBytes(String.Format("AT+CMGS={0}\r", sms.nLength));
    ss_port.Write(buf);
    string response = Encoding.ASCII.GetString(ss_port.Read(128));
    if (response.Length > 0 && response.EndsWith("> "))
    {
      ss_port.Write(Encoding.ASCII.GetBytes(String.Format("{0}\x01a", decodedSMS)));
      SendState = true;
    }
      

  5.   


    public void Open()
      {
      DCB dcbCommPort = new DCB();
      COMMTIMEOUTS ctoCommPort = new COMMTIMEOUTS();
      // 打开串口 OPEN THE COMM PORT.
      hComm = CreateFile(PortNum, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
      // 如果串口没有打开,就打开 IF THE PORT CANNOT BE OPENED, BAIL OUT.
      if (hComm == INVALID_HANDLE_VALUE)
      {
      throw (new ApplicationException("非法操作,不能打开串口!"));
      }
      // 设置通信超时时间 SET THE COMM TIMEOUTS.
      GetCommTimeouts(hComm, ref ctoCommPort);
      ctoCommPort.ReadTotalTimeoutConstant = ReadTimeout;
      ctoCommPort.ReadTotalTimeoutMultiplier = 0;
      ctoCommPort.WriteTotalTimeoutMultiplier = 0;
      ctoCommPort.WriteTotalTimeoutConstant = 0;
      SetCommTimeouts(hComm, ref ctoCommPort);
      // 设置串口 SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
      GetCommState(hComm, ref dcbCommPort);
      dcbCommPort.fOutxCtsFlow = 524800;
      dcbCommPort.BaudRate = BaudRate;
      dcbCommPort.flags = 0;
      dcbCommPort.flags |= 1;
      if (Parity > 0)
      {
      dcbCommPort.flags |= 2;
      }
      dcbCommPort.Parity = Parity;
      dcbCommPort.ByteSize = ByteSize;
      dcbCommPort.StopBits = StopBits;
      if (!SetCommState(hComm, ref dcbCommPort))
      {
      CloseHandle(hComm);
      throw (new ApplicationException("非法操作,不能打开串口!"));
      }
      Opened = true;
      }