PComm.dll在C#中如何使用

直接上代码:

 internal class PComm
    {
        public static bool isOpen = false;         
        public static string portName = string.Empty;    

        #region --- constants ---
        //	BAUD rate setting	
        public const int B50 = 0x00;
        public const int B75 = 0x01;
        public const int B110 = 0x02;
        public const int B134 = 0x03;
        public const int B150 = 0x04;
        public const int B300 = 0x05;
        public const int B600 = 0x06;
        public const int B1200 = 0x07;
        public const int B1800 = 0x08;
        public const int B2400 = 0x09;
        public const int B4800 = 0x0A;
        public const int B7200 = 0x0B;
        public const int B9600 = 0x0C;
        public const int B19200 = 0x0D;
        public const int B38400 = 0x0E;
        public const int B57600 = 0x0F;
        public const int B115200 = 0x10;
        public const int B230400 = 0x11;
        public const int B460800 = 0x12;
        public const int B921600 = 0x13;

        // Mode setting
        public const int BIT_5 = 0x00;         //' Data bits define
        public const int BIT_6 = 0x01;
        public const int BIT_7 = 0x02;
        public const int BIT_8 = 0x03;

        public const int STOP_1 = 0x00;        //' Stop bits define
        public const int STOP_2 = 0x04;

        public const int P_EVEN = 0x18;        //' Parity define
        public const int P_ODD = 0x08;
        public const int P_SPC = 0x38;
        public const int P_MRK = 0x28;
        public const int P_NONE = 0x00;

        //* error code 
        public const int SIO_OK = 0;
        public const int SIO_BADPORT = -1;		/* no such port or port not opened */
        public const int SIO_OUTCONTROL = -2;	/* can't control the board */
        public const int SIO_NODATA = -4;		/* no data to read or no buffer to write */
        public const int SIO_OPENFAIL = -5;	/* no such port or port has be opened */
        public const int SIO_RTS_BY_HW = -6;   /* RTS can't set because H/W flowctrl */
        public const int SIO_BADPARM = -7;		/* bad parameter */
        public const int SIO_WIN32FAIL = -8;	/* call win32 function fail, please call */
        /* GetLastError to get the error code */
        public const int SIO_BOARDNOTSUPPORT = -9;	/* Does not support this board */
        public const int SIO_FAIL = -10;		    /* PComm function run result fail */
        public const int SIO_ABORTWRITE = -11;	    /* write has blocked, and user abort write */
        public const int SIO_WRITETIMEOUT = -12;   /* write timeoue has happened */

        #endregion -----end of contants ---

        #region --- Dll Import functions ----

        /*------Port Control---------*/

        /// <summary>
        /// sio_open	Start to receive/transmit data
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        [DllImport("PComm.dll", EntryPoint = "sio_open")]
        public static extern int sio_open(int port);

        //sio_ioctl	Set port baud rate, parity, etc.
        [DllImport("PComm.dll", EntryPoint = "sio_ioctl")]
        public static extern int sio_ioctl(int port, int baud, int mode);

        //sio_flush	Flush input and/or output buffer
        [DllImport("PComm.dll", EntryPoint = "sio_flush")]
        public static extern int sio_flush(int port, int func);

        //sio_close	Stop receiving/transmitting data
        [DllImport("PComm.dll", EntryPoint = "sio_close")]
        public static extern int sio_close(int port);

        //sio_baud	Set baud rate using the actual speed value
        [DllImport("PComm.dll", EntryPoint = "sio_baud")]
        public static extern int sio_baud(int port, int speed);

        /*----- input functions -----*/

        //sio_getch - Read one character at a time from driver input buffer
        [DllImport("PComm.dll", EntryPoint = "sio_getch")]
        public static extern int sio_getch(int port);

        //sio_read - Read a block of data from driver input buffer
        [DllImport("PComm.dll", EntryPoint = "sio_read")]
        public static extern int sio_read(int port, ref byte buf, int length);

        //sio_SetReadTimeouts-Set timeouts for sio_read()
        [DllImport("PComm.dll", EntryPoint = "sio_SetReadTimeouts")]
        public static extern int sio_SetReadTimeouts(int port, uint TotalTimeouts, uint IntervalTimeouts);

        //sio_GetReadTimeouts-Get timeouts for sio_read()
        [DllImport("PComm.dll", EntryPoint = "sio_GetReadTimeouts")]
        public static extern int sio_GetReadTimeouts(int port, ref uint TotalTimeouts, ref uint IntervalTimeouts);

        /*---- Output functions ---*/
        //sio_write	- Write a block of data 
        [DllImport("PComm.dll", EntryPoint = "sio_write")]
        public static extern int sio_write(int port, ref byte buf, int length);

        //sio_putch	Write one character at a time to driver output buffer
        [DllImport("PComm.dll", EntryPoint = "sio_putch")]
        public static extern int sio_putch(int port, int term);

        //sio_SetWriteTimeouts	Set timeouts for sio_write()
        [DllImport("PComm.dll", EntryPoint = "sio_SetWriteTimeouts")]
        public static extern int sio_SetWriteTimeouts(int port, uint TotalTimeouts);

        //sio_GetWriteTimeouts	Get timeouts for sio_write()
        [DllImport("PComm.dll", EntryPoint = "sio_GetWriteTimeouts")]
        public static extern int sio_GetWriteTimeouts(int port, ref uint TotalTimeouts);

        /*---- Port Status Inquiry ---*/
        //sio_lstatus	Get line status
        [DllImport("PComm.dll", EntryPoint = "sio_lstatus")]
        public static extern int sio_lstatus(int port);

        //sio_iqueue	Size of data accumulated in driver input buffer
        [DllImport("PComm.dll", EntryPoint = "sio_iqueue")]
        public static extern int sio_iqueue(int port);

        //sio_oqueue	Size of data not yet sent out (still kept in driver output buffer)
        [DllImport("PComm.dll", EntryPoint = "sio_oqueue")]
        public static extern int sio_oqueue(int port);

        //sio_Tx_hold	Check the reason why data could not be transmitted
        [DllImport("PComm.dll", EntryPoint = "sio_Tx_hold")]
        public static extern int sio_Tx_hold(int port);

        //sio_getbaud	Get the baud rate setting
        [DllImport("PComm.dll", EntryPoint = "sio_getbaud")]
        public static extern int sio_getbaud(int port);

        //sio_getmode	Get the settings of parity, data bits, etc.
        [DllImport("PComm.dll", EntryPoint = "sio_getmode")]
        public static extern int sio_getmode(int port);

        //sio_data_status	Check if any error happen when receiving data
        [DllImport("PComm.dll", EntryPoint = "sio_data_status")]
        public static extern int sio_data_status(int port);

        #endregion

    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值