关闭

abc

标签: stringbuttoncomponentsobjectthreadbyte
1413人阅读 评论(0) 收藏 举报

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;

namespace mymy
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  
  [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
   public struct MyStruct
  {
   public string state;
   public int stateValue;
  }

  private System.ComponentModel.Container components = null;
  MyStruct myState=new MyStruct();
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label labState;
  private System.Windows.Forms.Label labType;
  private System.Windows.Forms.Label labStateInfo;
  private System.Timers.Timer timScan;

  
  //通道状态枚举
  
  enum ChanelState
  {
   TRK_IDLE,
   TRK_WAIT_CONNECT,
   TRK_PLAY_WELCOM,
   TRK_CHECK_PLAY_WELCOM,
   TRK_PLAY_SELECT,
   TRK_CHECK_PLAY_PASSWORD,
   TRK_CHECK_REC_END,
   TRK_CHECK_PLAY_BYEBYE,
   TRK_CHECK_PLAY_RECORD,
   TRK_WAIT_USER_HANGUP,
   TRK_WAIT_USER_PICKUP,
   TRK_CHECK_PLAY_SELECT,
   TRK_CHECK_REC_START,
   TRK_NOTRK
  };
  ChanelState[] cs=new ChanelState[100];

  #region DLL函数声明

  [DllImport("SHP_A3.dll")]public static extern int SsmStartCti(string lpSsmCfgFileName, string lpIndexCfgFileName);
  [DllImport("SHP_A3.dll")]public static extern int SsmAddToFileList(int ch, string pszFileName, int nFormat, int dwStartPos, int  dwLen);
  [DllImport("SHP_A3.dll")]public static extern string SsmGetLastErrMsgA();
  [DllImport("SHP_A3.dll")]public static extern int SsmGetMaxCfgBoard();
  [DllImport("SHP_A3.dll")]public static extern int SsmGetMaxUsableBoard();
  [DllImport("SHP_A3.dll")]public static extern int SsmGetMaxCh();
  [DllImport("SHP_A3.dll")]public static extern int SsmGetChState(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmGetChType(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmPickup(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmHangup(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmAutoDial(int ch, string szPhoNum);
  [DllImport("SHP_A3.dll")]public static extern int SsmCheckPlay(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmPlayFileList(int ch);  
  [DllImport("SHP_A3.dll")]public static extern int SsmRecToFile(int ch,string pszFileName, int nFormat, uint dwStartPos, uint   dwLen,uint dwTime,int nMask);
  [DllImport("SHP_A3.dll")]public static extern int SsmPlayFile(int ch,string pszFileName, int nFormat, int dwStartPos, uint dwLen);
  [DllImport("SHP_A3.dll")]public static extern int SsmStopPlay(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmStopPlayFile(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmStopPlayFileList(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmStopPlayIndex(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmStopRecToFile(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmStopRing(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmClearFileList(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmSetDtmfStopPlay(int ch,bool bDsf);
  [DllImport("SHP_A3.dll")]public static extern int SsmPlayIndexString(int ch,string pszIdxStr );
  [DllImport("SHP_A3.dll")]public static extern int SsmClearRxDtmfBuf(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmGetRxDtmfLen(int ch);
  [DllImport("SHP_A3.dll")]public static extern int SsmGet1stDtmf(int ch, byte[] pcDtmf);
  [DllImport("SHP_A3.dll")]public static extern int SsmGetDtmfStr(int ch, string pcDtmfstr);
  [DllImport("SHP_A3.dll")]public static extern int SsmChkRecToFile(int ch);

  #endregion

 


  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.labState = new System.Windows.Forms.Label();
   this.button1 = new System.Windows.Forms.Button();
   this.labType = new System.Windows.Forms.Label();
   this.labStateInfo = new System.Windows.Forms.Label();
   this.timScan = new System.Timers.Timer();
   ((System.ComponentModel.ISupportInitialize)(this.timScan)).BeginInit();
   this.SuspendLayout();
   //
   // labState
   //
   this.labState.Location = new System.Drawing.Point(144, 80);
   this.labState.Name = "labState";
   this.labState.Size = new System.Drawing.Size(288, 24);
   this.labState.TabIndex = 0;
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(184, 336);
   this.button1.Name = "button1";
   this.button1.TabIndex = 1;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // labType
   //
   this.labType.Location = new System.Drawing.Point(136, 32);
   this.labType.Name = "labType";
   this.labType.Size = new System.Drawing.Size(280, 23);
   this.labType.TabIndex = 2;
   //
   // labStateInfo
   //
   this.labStateInfo.Location = new System.Drawing.Point(144, 128);
   this.labStateInfo.Name = "labStateInfo";
   this.labStateInfo.TabIndex = 3;
   //
   // timScan
   //
   this.timScan.Enabled = true;
   this.timScan.Interval = 1000;
   this.timScan.SynchronizingObject = this;
   this.timScan.Elapsed += new System.Timers.ElapsedEventHandler(this.timScan_Elapsed);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(656, 493);
   this.Controls.Add(this.labStateInfo);
   this.Controls.Add(this.labType);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.labState);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.timScan)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
   //检测状态
   checkMode();

  }

  /// <summary>
  /// 检测状态
  /// </summary>
  private void checkMode()
  {
   int startOKFlag;
   startOKFlag=SsmStartCti("ShConfig.ini","Shindex.int");
   if(startOKFlag!=0)
   {
    string error=SsmGetLastErrMsgA();
    MessageBox.Show("NO,Not OK!Error:" + error);
   }
   else
   {
    MessageBox.Show("Yes,IS OK!");
   }
  }


  /// <summary>
  /// 开始
  /// </summary>
  private void MyStart()
  {
   for(int i=0;i<8;i++)
   {
    myState=getChanelState(i);
    labStateInfo.Text=myState.state;
    labType.Text=getChanelType(i);
    labState.Text=ScanChanel(i,myState.stateValue);
    
   }
  }

  #region 获取通道状态
  private MyStruct getChanelState(int index)
  {
   int state=SsmGetChState(index);
   switch(state)
   {
    case 0:
     myState.state="Kong Xian";
     myState.stateValue=0;
     break;
    case 1:
     myState.state="Zhen Ling ";
     myState.stateValue=1;
     break;
    case 2:
     myState.state="Tong Hua";
     myState.stateValue=2;
     break;
    case 3:
     myState.state="Deng Dai Bo Hao Yin";
     myState.stateValue=3;
     break;
    case 4:
     myState.state="Deng Dai Bo Hao Yin";
     myState.stateValue=4;
     break;
    case 5:
     myState.state="Bo Hao";
     myState.stateValue=5;
     break;
    case 6:
     myState.state="Deng Dai Bo Hao Jie Guo";
     myState.stateValue=6;
     break;
    case 7:
     myState.state="Guo Qi";
     myState.stateValue=7;
     break;
    case 9:
     myState.state="Deng Hou Bei Jiao Zhai Ji";
     myState.stateValue=9;
     break;
    default:
     break;
   }
   return myState;
  }
  #endregion

  #region 获取通道类型
  private string getChanelType(int index)
  {
   string reValue=null;
   int type=SsmGetChType(index);
   switch(type)
   {
    case 0:
     reValue="Mo Ni";
     break;
    case 1:
     reValue="Ni Xian";
     break;
    case 2:
     reValue="Zuo Xi";
     break;
    case 3:
     reValue="Lu Yin";
     break;
    case 20:
     reValue="-1";
     break;
    default:
     reValue="-1";
     break;
   }
   return reValue;
  }
  #endregion

  #region 检测通道状态
  /// <summary>
  /// 检测通道状态
  /// </summary>
  /// <param name="ch"></param>
  /// <param name="nstate"></param>
  /// <returns></returns>
  private string  ScanChanel(int ch,int nstate )//
  {  
   string mCurinfo="";
   if (nstate ==7)//挂起
   {
    switch(cs[ch])
    {
     case ChanelState.TRK_CHECK_PLAY_WELCOM:
      SsmStopPlayFileList(ch);
      mCurinfo="停止播放欢迎词";
      break;
   
     case ChanelState.TRK_CHECK_PLAY_PASSWORD:
     case ChanelState.TRK_CHECK_REC_START:
     case ChanelState.TRK_CHECK_PLAY_SELECT:
     case ChanelState.TRK_CHECK_PLAY_BYEBYE:
      SsmStopPlayIndex(ch);
      mCurinfo="停止索引放音";
      break;
   
     case ChanelState.TRK_CHECK_REC_END:
      SsmStopRecToFile(ch);
      mCurinfo="停止录音";
      break;
   
     case ChanelState.TRK_CHECK_PLAY_RECORD:
      SsmStopPlayFile(ch);
      mCurinfo="停止文件放音";
      break;
   
    
    }

   }
   switch(cs[ch])
   { 
    case ChanelState.TRK_IDLE:
     if( nstate == 2 )  //外线振铃
     {
      SsmPickup(ch);      
      cs[ch] = ChanelState.TRK_WAIT_CONNECT;
     }
     break;
    case ChanelState.TRK_WAIT_CONNECT:
     if( nstate == 3 )//通话状态
      cs[ch] = ChanelState.TRK_PLAY_WELCOM;
     mCurinfo="播放欢迎词";
     break;
    case ChanelState.TRK_PLAY_WELCOM:
     SsmClearFileList(ch);
     if((SsmAddToFileList(ch,"DemoVoc//Welcom1.voc",6,0,-1)==-1 ||
      SsmAddToFileList(ch,"DemoVoc//Welcom2.voc",6,0,-1)==-1) )
     {
      SsmHangup(ch);
      MessageBox.Show(SsmGetLastErrMsgA());
      cs[ch] = ChanelState.TRK_IDLE;

     }
     else
      if(SsmPlayFileList(ch) == -1)
     {
      SsmHangup( ch );
      MessageBox.Show(SsmGetLastErrMsgA());
      cs[ch] = ChanelState.TRK_IDLE;
     }      
     else
      cs[ch] = ChanelState.TRK_CHECK_PLAY_WELCOM;
     mCurinfo="播放选择提示音";
     break;
    case ChanelState.TRK_CHECK_PLAY_WELCOM:
     if( SsmCheckPlay(ch) > 0) //end of playing "welcom"
      cs[ch] = ChanelState.TRK_PLAY_SELECT;
     mCurinfo="end of playing welcom";
     break;
    case ChanelState.TRK_PLAY_SELECT:

     SsmSetDtmfStopPlay(ch,true); //enable DTMF-Stop-Play function
     if(SsmPlayIndexString(ch,"Select") == -1)
      
     {
      SsmHangup( ch );
      MessageBox.Show(SsmGetLastErrMsgA());
      cs[ch] = ChanelState.TRK_IDLE;
      
     }
     else
     {
      SsmClearRxDtmfBuf(ch);
      cs[ch] = ChanelState.TRK_CHECK_PLAY_SELECT;
     }
     mCurinfo="选择按键";

     break;

    case ChanelState.TRK_CHECK_PLAY_SELECT:

     /////////////////////////////////////////////////////
     
     if(SsmGetRxDtmfLen(ch) >= 1 && (SsmCheckPlay(ch) == 2 || SsmCheckPlay(ch) == 1))
     {
      string tmp1="";
      tmp1+= '/0'; 
      byte[] str=new byte[1];
      int tt=SsmGet1stDtmf(ch, str);
      SsmClearRxDtmfBuf(ch);
      tmp1=System.Text.Encoding.ASCII.GetString(str);
      char[]tmp=tmp1.ToCharArray();
      switch(tmp[0])
      {
       case '1':   //DTMF=1: record to file "test.voc"
        if(SsmPlayIndexString(ch,"RecPrompt") == -1) //play "press any key to start, and any key again to stop".
        {
         
         SsmHangup( ch );
         MessageBox.Show(SsmGetLastErrMsgA());
         cs[ch] = ChanelState.TRK_IDLE;
        }
        else
        {
         
         cs[ch] = ChanelState.TRK_CHECK_REC_START;
         
        }        


         
        break;

       case '2':   //DTMF=2: play recorded file "test.voc"
        mCurinfo="收听留言";
        if(File.Exists("test.voc"))
         if(SsmPlayFile(ch,"test.voc",6,0,0xFFFFFFFF) == -1)
         {
          SsmHangup( ch );
          MessageBox.Show(SsmGetLastErrMsgA());
          cs[ch] = ChanelState.TRK_IDLE;
         }
         
         else
          cs[ch] = ChanelState.TRK_CHECK_PLAY_RECORD;
        else
         if(SsmPlayIndexString(ch,"FileNotFound") == -1) //play "file not found" message
         
        {
         SsmHangup( ch );
         MessageBox.Show(SsmGetLastErrMsgA());
         cs[ch] = ChanelState.TRK_IDLE;

        }
         
        else
         cs[ch] = ChanelState.TRK_CHECK_PLAY_RECORD;

        break ;

       case '3':   //DTMF=3: play user's password,some sample digital
        mCurinfo="查询密码";
        SsmSetDtmfStopPlay(ch,false); //disable DTMF-Stop-Play function while playing password
        if(SsmPlayIndexString(ch,"YourPasswordIs,1,2,3,4") == -1) //play "your password is 1234".
         
        {
         SsmHangup( ch );
         MessageBox.Show(SsmGetLastErrMsgA());
         cs[ch] = ChanelState.TRK_IDLE;
         mCurinfo=SsmGetLastErrMsgA();

        }
         
        else
         cs[ch] = ChanelState.TRK_CHECK_PLAY_PASSWORD;
        break;
    
       
       case '0':   //DTMF=0:  end of service,play "Byebye"
        mCurinfo="退出";
        if ( SsmPlayIndexString(ch, "Byebye" )== -1 )
             
        {
         SsmHangup( ch );
         MessageBox.Show(SsmGetLastErrMsgA());
         cs[ch] = ChanelState.TRK_IDLE;
        }
        
        else
         cs[ch]= ChanelState.TRK_CHECK_PLAY_BYEBYE;
        break; 
           
       default: 
        break;
      }
     }
    
     break;

    case ChanelState.TRK_CHECK_PLAY_PASSWORD:
     if(SsmCheckPlay(ch) > 0)    //this channel is not playing index now
      cs[ch]= ChanelState.TRK_PLAY_SELECT;
     mCurinfo="this channel is not playing index now";
     break ;

    case ChanelState.TRK_CHECK_REC_START:
     if(SsmGetRxDtmfLen(ch) >= 1)
     {
      SsmClearRxDtmfBuf(ch);
      if( SsmRecToFile(ch,"test.voc",6,0,0xffffffff,0,0) == -1) //start from 16000L,maxmium 10 seconds
      {
       SsmHangup( ch );
       MessageBox.Show(SsmGetLastErrMsgA());
       cs[ch] = ChanelState.TRK_IDLE;
       mCurinfo=SsmGetLastErrMsgA();
      
      }
      else
       cs[ch] = ChanelState.TRK_CHECK_REC_END;
     }
     mCurinfo="录音开始";
     break;

    case ChanelState.TRK_CHECK_REC_END:
     if(SsmGetRxDtmfLen(ch)>=1 || SsmChkRecToFile(ch)==0)
     {
      SsmStopRecToFile(ch);
      cs[ch]= ChanelState.TRK_PLAY_SELECT;
      mCurinfo="录音结束";
     }
     break ;

    case ChanelState.TRK_CHECK_PLAY_RECORD:
     if(SsmCheckPlay(ch) > 0)
      cs[ch] = ChanelState.TRK_PLAY_SELECT;
     break;

    case ChanelState.TRK_CHECK_PLAY_BYEBYE:
     if(SsmCheckPlay(ch) > 0)
     {
      SsmHangup(ch);
      cs[ch] = ChanelState.TRK_IDLE;
     }
     break ;

     //////////////////////////////////////////////////////

 

     

   }//end switch
   return mCurinfo;

  }
  #endregion

  private void button1_Click(object sender, System.EventArgs e)
  {
   Thread myt=new Thread(new ThreadStart(MyStart));
   myt.Start();  
  }

  private void timScan_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   MyStart(); 
  }
 }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:458822次
    • 积分:5822
    • 等级:
    • 排名:第4379名
    • 原创:90篇
    • 转载:132篇
    • 译文:2篇
    • 评论:80条
    文章分类