Пример #1
0
        public async void SendData(byte[] Data)
        {
            int i_RtnCode;
            int i_Length = Data.Length;

            //Write data
            if (Gl_Int_ByteDelay == 0)
            {
                i_RtnCode = Sio_write(Gl_Int_Port, ref Data[0], i_Length);
                if (i_RtnCode < 0)
                {
                    ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                    return;
                }
            }
            else
            {
                for (int i = 0; i <= i_Length - 1; i++)
                {
                    i_RtnCode = Sio_write(Gl_Int_Port, ref Data[i], 1);
                    if (i_RtnCode < 0)
                    {
                        ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                        return;
                    }
                    await Task.Delay(Gl_Int_ByteDelay);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// 关闭串口通讯
        /// </summary>
        public void CloseComm()
        {
            int i_RtnCode = Sio_close(Gl_Int_Port);

            if (i_RtnCode != SIO_OK)
            {
                ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                return;
            }
        }
Пример #3
0
        /// <summary>
        /// 初始化串口通讯
        /// </summary>
        /// <param name="Hb_CommParam"></param>
        public void InitComm(Hashtable Ht_CommParam)
        {
            AnalyseCommParam(Ht_CommParam);

            //Open port
            int i_RtnCode = Sio_open(Gl_Int_Port);

            if (i_RtnCode != SIO_OK)
            {
                ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                return;
            }

            //Configure communication parameters
            int mode = Gl_Int_Parity | Gl_Int_ByteSize | Gl_Int_StopBits;

            i_RtnCode = Sio_ioctl(Gl_Int_Port, Gl_Int_Baudrate, mode);
            if (i_RtnCode != SIO_OK)
            {
                ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                return;
            }

            //Flow control
            i_RtnCode = Sio_flowctrl(Gl_Int_Port, 0);
            if (i_RtnCode != SIO_OK)
            {
                ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                return;
            }

            //DTR
            i_RtnCode = Sio_DTR(Gl_Int_Port, 1);
            if (i_RtnCode != SIO_OK)
            {
                ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                return;
            }

            //RTS
            i_RtnCode = Sio_RTS(Gl_Int_Port, 1);
            if (i_RtnCode != SIO_OK)
            {
                ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                return;
            }

            //Set timeout values for sio_read
            Sio_SetReadTimeouts(Gl_Int_Port, Gl_Int_AfterDealy, Gl_Int_ReadIntervalTimeout);
            //Sio_SetWriteTimeouts(Gl_Int_Port, Gl_Int_BeforeDelay, Gl_Int_WriteIntervalTimeout);
            ReadyRead();
        }
Пример #4
0
        /// <summary>
        /// 发送十六进值字符串帧,
        /// </summary>
        /// <param name="Str_Data">发送帧</param>
        public async void SendData(string Str_Data)
        {
            int i_RtnCode;

            Str_Data  = Str_Data.Replace(" ", "");
            Str_Data  = Str_Data.Replace("-", "");
            Str_Data += Str_Data.Length % 2 != 0 ? "0" : "";
            int i_Length = Str_Data.Length / 2;

            byte[] buffer = new byte[i_Length];
            for (int i = 0; i <= i_Length - 1; i++)
            {
                buffer[i] = Convert.ToByte(Str_Data.Substring(i * 2, 2), 16);
            }

            //Write data
            if (Gl_Int_ByteDelay == 0)
            {
                i_RtnCode = Sio_write(Gl_Int_Port, ref buffer[0], i_Length);
                if (i_RtnCode < 0)
                {
                    ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                    return;
                }
            }
            else
            {
                for (int i = 0; i <= i_Length - 1; i++)
                {
                    i_RtnCode = Sio_write(Gl_Int_Port, ref buffer[i], 1);
                    if (i_RtnCode < 0)
                    {
                        ShowErrMsg?.Invoke(this, GetCommErrMsg(i_RtnCode));
                        return;
                    }
                    await Task.Delay(Gl_Int_ByteDelay);
                }
            }
        }