串口

jacklee

MSCOMM的到有一些,昨天测试过
初使化:
            axms.CommPort =1;
            axms.Settings ="9600,n,8,1";
            //axms.InputMode=InputModeConstants.comInputModeText;
            axms.InputMode =InputModeConstants.comInputModeBinary ;
            axms.InputLen =1024;
            axms.OutBufferSize=256;
            axms.RThreshold=1;
            axms.PortOpen =true;

发数据:
            byte[] Msg=new byte[3];
            Msg[0]=100;//Convert.ToByte ('a');
            Msg[1]=78;//Convert.ToByte ('b');
            Msg[2]=105;//Convert.ToByte ('c');
            axms.Output=Msg;
注:发数据后ONCOMM事件马上会检测到有数据发送事件,你可以去提取


提取及事件测试:
        private void axms_OnComm(object sender, System.EventArgs e)
        {
            switch(axms.CommEvent )
            {
                case (short)OnCommConstants.comEvCD :
                    textBox1.Text +="CD\r\n";
                    break;
                case (short)OnCommConstants.comEvCTS:
                    textBox1.Text +="CTS\r\n";
                    break;
                case (short)OnCommConstants.comEvDSR:
                    textBox1.Text +="DSR\r\n";
                    break;
                case (int)OnCommConstants.comEvReceive:  //提取
                    ComMsg="";
                    byte[] tp;
                    object tobj;
                    axms.InputLen =0;
                    tobj=axms.Input;
                    tp=(byte[])tobj;
                    for(int i=0;i<tp.Length-1;i++)
                    {
                        ComMsg+=tp[i].ToString ("X")+" ";
                    }
                    textBox1.Text +="Received datas/"+ComMsg+"\r\n";
                    break;
                case (int)OnCommConstants.comEvSend :
                    textBox1.Text +="Message Send.\r\n";
                    break;
                default:
                    textBox1.Text +="Default [else] Event\r\n";
                    break;
            }
            textBox1.Text +="Comm Event--\r\n";
}

你可能感兴趣的:(串口)