簡體   English   中英

WinCE 6.0通過ActiveSync / Window Mobile Device Center C#連接到PC

[英]WinCE 6.0 connecting to PC via ActiveSync/Window Mobile Device Center C#

我正在開發WinCE應用程序(.Net 3.5),該應用程序允許通過TCPIP,串行端口和USB連接到終端。

TCPIP和串行端口已完成,但USB存在問題。 由於客戶端需要看到USB證明可以完成,因此我們需要證明我們可以通過ActiveSync發送十六進制命令。

我在Google住了一段時間,發現ActiveSync / WMDC將提供IP以允許彼此連接。 問題是我無法通過ActiveSync / WMDC通過C#套接字從PC ping或從PC連接到WinCE。

我唯一了解的是連接到PC時的WinCE IP是:
IP地址:192.168.55.101
子網掩碼:255.255.255.0
默認網關:192.168.55.100
主DNS:127.0.0.1

下面是我的WinCE服務器代碼,用於允許來自ActiveSync的所有連接。 我正在重用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;
    }

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM