草庐IT

c# - WinCE 6.0 通过ActiveSync/Window Mobile Device Center C#连接PC

coder 2023-09-19 原文

我正在开发一个 WinCE 应用程序 (.Net 3.5),它允许通过 TCPIP、串行端口和 USB 连接到终端。

TCPIP 和串行端口已完成,但 USB 有问题。由于客户端需要查看 USB 上的证明,我们需要证明我们可以通过 ActiveSync 发送 Hex 命令。

我google了一段时间,发现ActiveSync/WMDC会提供IP让彼此连接。问题是我无法通过 C# 套接字通过 ActiveSync/WMDC 从 PC ping 或连接到 WinCE。

我唯一知道的是我连接PC时的WinCE IP是:
IP地址:192.168.55.101
子网掩码:255.255.255.0
默认网关:192.168.55.100
主 DNS:127.0.0.1

下面是我用来允许来自 ActiveSync 的所有连接的 WinCE 服务器代码。我正在重新使用我的 TCPIP 代码来连接到 ActiveSync。

_listener.Bind(new IPEndPoint(IPAddress.Any, _port));             
_listener.Listen(100);
_listener.BeginAccept(ConnectionReady, null);

有什么我错过了允许彼此联系的吗?提前致谢。

最佳答案

这似乎是可行的,但不是直接的方式。

一般我们会把PC设为Server,WinCE设为Slave。在这种情况下,它是倒退的。 WinCE为主机,PC为从机。

下面是我用来声明和启动连接的代码:

        private void SetUSBSerialPort()
        {
            try
            {
                usbSerialPort = new USBSerialPort(config.terminal_tcpip_port);
                usbSerialPort.OnReceiveData += new USBSerialPort.EventOnReceiveData(usbSerialPort_OnReceiveData);
                usbSerialPort.Start();
            }
            catch (Exception ex)
            {

            }
        }

下面是我用来通过 ActiveSync 连接 PC 和 WinCE 的类:

public class ConnectionStateUSB
    {
        internal Socket _conn;
        //internal TcpServiceProvider _provider;
        internal byte[] _buffer;

        /// <SUMMARY>
        /// Tells you the IP Address of the remote host.
        /// </SUMMARY>
        public EndPoint RemoteEndPoint
        {
            get { return _conn.RemoteEndPoint; }
        }

        /// <SUMMARY>
        /// Returns the number of bytes waiting to be read.
        /// </SUMMARY>
        public int AvailableData
        {
            get { return _conn.Available; }
        }

        /// <SUMMARY>
        /// Tells you if the socket is connected.
        /// </SUMMARY>
        public bool Connected
        {
            get { return _conn.Connected; }
        }

        /// <SUMMARY>
        /// Reads data on the socket, returns the number of bytes read.
        /// </SUMMARY>
        public int Read(byte[] buffer, int offset, int count)
        {
            try
            {
                if (_conn.Available > 0)
                    return _conn.Receive(buffer, offset, count, SocketFlags.None);
                else return 0;
            }
            catch
            {
                return 0;
            }
        }

        /// <SUMMARY>
        /// Sends Data to the remote host.
        /// </SUMMARY>
        public bool Write(byte[] buffer, int offset, int count)
        {
            try
            {
                _conn.Send(buffer, offset, count, SocketFlags.None);
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <SUMMARY>
        /// Ends connection with the remote host.
        /// </SUMMARY>
        public void EndConnection()
        {
            if (_conn != null && _conn.Connected)
            {
                _conn.Shutdown(SocketShutdown.Both);
                _conn.Close();
            }
        }
    }

    class USBSerialPort
    {
        Socket newsock;
        Socket client;
        int port;
        IPAddress ipaddress;
        private AsyncCallback ConnectionReady;
        private AsyncCallback AcceptConnection;
        private AsyncCallback ReceivedDataReady;
        private ConnectionStateUSB currentST;
        public bool IsConnected = false;

        public USBSerialPort(int _port)
        {
            port = _port;
            //ConnectionReady = new AsyncCallback(ConnectionReady_Handler);
            //AcceptConnection = new AsyncCallback(AcceptConnection_Handler);
            //ReceivedDataReady = new AsyncCallback(ReceivedDataReady_Handler);
        }

        public void Start()
        {            
            try
            {
                ipaddress = Dns.GetHostEntry("ppp_peer").AddressList[0];
                newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint iep = new IPEndPoint(ipaddress, port);
                newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
            }
            catch (Exception ex)
            {
                throw ex;
                this.Stop();
            }
            finally
            {
                //net_stream = null;
                //tcp_client = null;
            }
        }

        void Connected(IAsyncResult iar)
        {            
            try
            {
                client = (Socket)iar.AsyncState;
                client.EndConnect(iar);

                ConnectionStateUSB st = new ConnectionStateUSB();
                st._conn = client;
                st._buffer = new byte[4];
                //Queue the rest of the job to be executed latter
                //ThreadPool.QueueUserWorkItem(AcceptConnection, st);
                currentST = st;
                //conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
                if (client.Connected)
                    client.BeginReceive(st._buffer, 0, 0, SocketFlags.None,
                                  new AsyncCallback(ReceiveData), st);                
            }
            catch (SocketException e)
            {
                IsConnected = false;
                //conStatus.Text = "Error connecting";
            }
            catch (Exception ex)
            {
                IsConnected = false;
            }
        }

        void ReceiveData(IAsyncResult iar)
        {
            try
            {
                ConnectionStateUSB remote = (ConnectionStateUSB)iar.AsyncState;
                remote._conn.EndReceive(iar);
                try
                {                    
                    this.OnReceiveData(remote);
                    IsConnected = true;
                }
                catch (Exception ex)
                {
                    IsConnected = false;
                }
                //int recv = remote.EndReceive(iar);
                //string stringData = Encoding.ASCII.GetString(data, 0, recv);
                //results.Items.Add(stringData);
                if (remote.Connected)
                    remote._conn.BeginReceive(remote._buffer, 0, 0, SocketFlags.None,
                                  new AsyncCallback(ReceiveData), remote);
            }
            catch (Exception ex)
            {
                this.Stop();
            }
        }

        public void SendData(byte[] data)
        {
            try
            {
                bool a = currentST.Write(data, 0, data.Length);
            }
            catch (Exception ex)
            {
                IsConnected = false;
                MessageBox.Show("Error!\n" + ex.Message + "\n" + ex.StackTrace);
            }
        }

        public void SendData(string data)
        {
            try
            {
                byte[] msg = Encoding.UTF8.GetBytes(data + Convert.ToChar(Convert.ToByte(3)));
                bool a = currentST.Write(msg, 0, msg.Length);
                msg = null;
            }
            catch (Exception ex)
            {
                IsConnected = false;
                MessageBox.Show("Error!\n" + ex.Message + "\n" + ex.StackTrace);
            }
        }


        /// <SUMMARY>
        /// Shutsdown the server
        /// </SUMMARY>
        public void Stop()
        {
            //lock (this)
            //{
                if (newsock != null)
                    newsock.Close();
                if (client != null)
                    client.Close();
                if (currentST != null)
                {
                    currentST._conn.Close();
                    currentST = null;
                }
                IsConnected = false;
            //}
        }

        /// <SUMMARY>
        /// Gets executed when the server accepts a new connection.
        /// </SUMMARY>
        public delegate void EventOnAcceptConnection(ConnectionStateUSB socket);
        public event EventOnAcceptConnection OnAcceptConnection;

        /// <SUMMARY>
        /// Gets executed when the server detects incoming data.
        /// This method is called only if OnAcceptConnection has already finished.
        /// </SUMMARY>
        public delegate void EventOnReceiveData(ConnectionStateUSB socket);
        public event EventOnReceiveData OnReceiveData;

        /// <SUMMARY>
        /// Gets executed when the server needs to shutdown the connection.
        /// </SUMMARY>
        public delegate void EventOnDropConnection(ConnectionStateUSB socket);
        public event EventOnDropConnection OnDropConnection;
    }

希望这对您有所帮助。

关于c# - WinCE 6.0 通过ActiveSync/Window Mobile Device Center C#连接PC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16744615/

有关c# - WinCE 6.0 通过ActiveSync/Window Mobile Device Center C#连接PC的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  3. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  4. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  5. ruby - 通过 ruby​​ 进程共享变量 - 2

    我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是

  6. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  7. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  8. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  9. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  10. 通过 MacPorts 的 RubyGems 是个好主意吗? - 2

    从MB升级到新的MBP后,Apple的迁移助手没有移动我的gem。我这次是通过macports安装ruby​​gems,希望在下次升级时避免这种情况。有什么我应该注意的陷阱吗? 最佳答案 如果你想把你的gems安装在你的主目录中(在传输过程中应该复制过来,作为一个附带的好处,会让你以你自己的身份运行geminstall,而不是root),将gemhome:键设置为您在~/.gemrc中的主目录中的路径. 关于通过MacPorts的RubyGems是个好主意吗?,我们在StackOverf

随机推荐