谁做过板卡com口通讯的程序,用c#及api函数实现?
有的话请提供一个例子好吗?小弟急用!

解决方案 »

  1.   

    我做过一个。很早以前的事情了,我翻出来看看。有删节,也用可能有BUG,仅供参考。    public class ENMgmt : Collector.DeviceMgmt
        {      
            private byte[] allCache;
            private int allCacheSize;        private Thread ioThread;         #region Data Transfer Methods
            private void ReadData(SerialPort ss)
            {
                byte[] reqCommand = new byte[] { 0x40, 0x01, 0x0d };
                if (ss.IsOpen)
                {
                    Thread.Sleep(100);
                    //发送请求指令                
                    try
                    {
                        ss.Write(reqCommand, 0, reqCommand.Length);
                    }
                    catch
                    {
                        throw new Exception("发送命令失败!");
                    }                //确定返回数据框架
                    int cacheSize = Device.ChannelCount * 12 + 10;      //Cache Size
                    int dataSize = Device.ChannelCount * 12 + 5;     //Data Size                byte[] retData = new byte[cacheSize];                //停顿的时间与通道数成正比
                    Thread.Sleep(Device.ChannelCount * 100);
                    int rdLen = 0;
                    try
                    {
                        //读数据
                        if (ss.BytesToRead > 0)
                        {
                            rdLen = ss.Read(retData, 0, retData.Length);
                        }                    if (rdLen <= 0)
                        {
                            throw new Exception("没有收到响应!");
                        }                    //检测缓存剩余空间
                        if (allCacheSize - allCache.Length >= rdLen)
                        {
                            //剩余空间足够时存入总缓存                        //将收到的数据存入recData:recData大小为rdLen
                            byte[] recData = new byte[rdLen];
                            for (int i = 0; i < rdLen; i++)
                            {
                                recData[i] = retData[i];
                            }                        //申请新的总缓存
                            byte[] t = new byte[allCache.Length + rdLen];                        //复制原缓存数据
                            allCache.CopyTo(t, 0);
                            //追加(复制)收到的数据
                            recData.CopyTo(t, allCache.Length);
                            //设置新的缓存为新缓存
                            allCache = t;                    }
                        else
                        {
                            //不能存入,需要释放缓存
                            Log("丢弃缓存数据。");
                       
                            allCache = retData;
                        }               
                        if (allCache.Length >= dataSize)
                        {                        retData = getFrameFromAllCache(dataSize);
                            //解析并发送数据                
                            TransferThenSendData(retData);
                        }
                        else
                        {
                            //收到的数据长度太小
                            throw new Exception("返回数据长度太小!");
                            //原方法
                            //return;
                        }                }
                    catch (Exception anyExp)
                    {
                        throw anyExp;
                    }
                }
            }       
            private SerialPort ss;
            private object ssMutex;        private void IOThread()
            {
                try
                {
                    allCacheSize = (Device.ChannelCount * 12 + 5) * 2 + 50;        //总缓存大小为2倍预计大小+50字节
                    //初始化缓存
                    allCache = new byte[0];
                    //启动设备            
                    if (ss == null)
                        ss = new SerialPort(Device.Port, 19200, Parity.Even, 8, StopBits.One);
                    ss.ReadTimeout = Device.ChannelCount * 1000;        //读超时设置为每个端口1秒钟
                    ss.WriteTimeout = 3 * 1000;                         //写超时固定为3秒
                }
                catch (Exception exp)
                {
                    Log("启动采集线程异常" + exp.Message);
                    return;
                }            int openPortCount = 0;            //进入读写
                while (true)
                {
                    try
                    {
                        if (Device.Enabled)
                        {
                            Thread.Sleep(800);
                            if (ss.IsOpen)
                            {
                                openPortCount = 0;
                            }
                            else
                            {
                                Log("端口未打开,重新打开端口。");
                                try
                                {
                                    openPortCount++;
                                    Thread.Sleep(10000);
                                    ss.Open();
                                    openPortCount = 0;
                                    Log("端口打开成功:");
                                }
                                catch
                                {
                                    Log("端口打开失败!");
                                    if (openPortCount > 10)
                                    {
                                        Log("尝试多次无法打开端口,设备进入休眠状态!");
                                        Thread.Sleep(MonitorTime * 20);
                                    }
                                }
                            }
                            if (ssMutex == null) ssMutex = new Object();
                            lock (ssMutex)
                            {
                                ReadData(ss);
                            }
                        }
                        else
                        {
                            //关闭状态
                            if (ss.IsOpen)
                            {
                                int closeTimes = 0;
                                while (ss.IsOpen)
                                {
                                    try
                                    {
                                        closeTimes++;
                                        if (closeTimes > 10) break;
                                        Thread.Sleep(1000);
                                        ss.Close();
                                    }
                                    catch
                                    {
                                    }
                                }
                                //测试并记录关闭结果
                                if (ss.IsOpen)
                                {
                                    Log("关闭端口失败!");
                                }
                                else
                                {
                                    Log("关闭端口成功!");
                                }
                            }
                            Thread.Sleep(3000);
                        }
                    }
                    catch (ThreadAbortException abort)
                    {
                        Log("数据采集线程被强行中止。");
                        throw abort;
                    }
                    catch (Exception anyExp)
                    {
                        Log("读取数据时发生异常:" + anyExp.Message);
                    }
                }
            }
      

  2.   

    能不能调用MifareOne的库函数?怎么调用!?
      

  3.   

    兄弟们,谁做过MifareOne卡,用c#怎么调用它的库函数啊?
      

  4.   

    普通的Win32动态链接的话按照调用WinAPI一样的方式声明函数就可以调用了,当然你必须得有函数签名。比如:
    [DllImport("mifare.dll")]
    public static int SomeFunction(...);
      

  5.   

    不对啊,怎么还不能调用啊?哈哈、
    phy,错误提示找不到指定的模块,你做过没有啊、哈哈
      

  6.   

    int  wb_download_key (word icdev,    unsigned char  mode, unsigned char  *key)
    上面的这个函数是c++ dll文件里的,怎么改成以c#对应的函数啊!
      

  7.   

    public static extern int wb_download_key(UInt32 icdev,byte mode,byte[] key);