c#中如何进行com口操作?

简介

串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。串行接口 (Serial Interface)是指数据一位一位地顺序传送。其特点是通信线路简单,只要一对传输线就可以实现双向通信(可以直接利用电话线作为传输线),从而大大降低了成本,特别适用于远距离通信,但传送速度较慢。

与并口区别

串口形容一下就是一条车道,而并口就是有8个车道同一时刻能传送8位(一个字节)数据。但是并不是说并口快,由于8位通道之间的互相干扰(串扰),传输时速度就受到了限制,传输容易出错。串口没有互相干扰。并口同时发送的数据量大,但要比串口慢。[3]  串口硬盘就是这样被人们重视的。.

操作流程

1、添加控件System.IO.Ports.SerialPort

2、打开串口

  public void openSport()
        {
            try
            {
                string[] PortNameArr = SerialPort.GetPortNames();
                sp.PortName = "COM10";
                sp.BaudRate = 9600;
                sp.DataBits = 8;
                int stopbit = 2;
                sp.StopBits = (StopBits)stopbit;
                sp.ReadTimeout = 10000;
                sp.ReceivedBytesThreshold = 1;
                sp.Open();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

3、往串口发命令

 public void cabinet(String command)
        {
            //MessageBox.Show("调用打开通道" + command);
            try
            {
                //MessageBox.Show(command);
                String strSend = command;        //"8A 01 01 11 9B";
                //处理数字转换
                string sendBuf = strSend;
                string sendnoNull = sendBuf.Trim();
                string sendNOComma = sendnoNull.Replace(',', ' ');    //去掉英文逗号
                string sendNOComma1 = sendNOComma.Replace(',', ' '); //去掉中文逗号
                string strSendNoComma2 = sendNOComma1.Replace("0x", "");   //去掉0x
                strSendNoComma2.Replace("0X", "");   //去掉0X
                string[] strArray = strSendNoComma2.Split(' ');

                int byteBufferLength = strArray.Length;
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (strArray[i] == "")
                    {
                        byteBufferLength--;
                    }
                }
                // int temp = 0;
                byte[] byteBuffer = new byte[byteBufferLength];
                int ii = 0;
                for (int i = 0; i < strArray.Length; i++)        //对获取的字符做相加运算
                {

                    Byte[] bytesOfStr = Encoding.Default.GetBytes(strArray[i]);

                    int decNum = 0;
                    if (strArray[i] == "")
                    {
                        //ii--;     //加上此句是错误的,下面的continue以延缓了一个ii,不与i同步
                        continue;
                    }
                    else
                    {
                        decNum = Convert.ToInt32(strArray[i], 16); //atrArray[i] == 12时,temp == 18 
                    }

                    try    //防止输错,使其只能输入一个字节的字符
                    {
                        byteBuffer[ii] = Convert.ToByte(decNum);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        //textBox_uartSend.Enabled = false;
                        return;
                    }

                    ii++;
                }
                sp.Write(byteBuffer, 0, byteBuffer.Length);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

4、命令测试

   cabinet("5A 00 FF A5");

            cabinet("5A 03 02 A5");

灯亮了,说明方法是对的

c#中如何进行com口操作?