/// <summary> /// Open a serial port if in config we choose serial communication. /// </summary> /// <param name="comPort"></param> /// <param name="baudRate"></param> /// <returns>A Ccr Port for receiving serial port data</returns> internal bool SerialOpen(int comPort, int baudRate) { commMethod = DrRobotConnectionMethod.SerialComm; if (recBuffer == null) { recBuffer = new byte[4095]; } if (baudRate < 1200) { baudRate = 115200; } serialPort.Close(); string portName = "COM" + comPort.ToString(System.Globalization.NumberFormatInfo.InvariantInfo); if (serialPort == null) { serialPort = new SerialPort(portName, baudRate); } else { serialPort.PortName = portName; serialPort.BaudRate = baudRate; } serialPort.Encoding = Encoding.Default; serialPort.Parity = Parity.None; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; serialPort.Handshake = Handshake.RequestToSend; // Handshake.RequestToSend; //hardware flow control // serialPort.WriteTimeout = 2000; serialPort.NewLine = "\r"; serialPort.ReadTimeout = 0; serialPort.ReceivedBytesThreshold = 1; //the shortest package is ping package serialPort.ReadBufferSize = 4096; //serialPort.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); //serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(portError); try { serialPort.Open(); //start a thread to receive // if (receiveThread == null) { receiveThread = new Thread((dataReceive)); receiveThread.CurrentCulture = new CultureInfo("en-US"); receiveThread.Start(); } } catch { return(false); } return(true); }
//private JaguarCtrl _form; //private string comID = "MOT1"; /*public DrRobotComm(JaguarCtrl form, string id) * { * _form = form; * comID = id; * }*/ //here is the WiFi connecting start internal bool StartClient(string addr, int portNum) { // Connect to remote server commMethod = DrRobotConnectionMethod.WiFiComm; try { // Establish the remote endpoint for the socket // GetHostEntry was an attempt to do a hostname lookup // so that you did not have to type an IP address. // However, it takes a LONG time for an IP address // (around 20 seconds). // IPHostEntry ipHostInfo = Dns.GetHostEntry(addr); // IPAddress ipAddress = ipHostInfo.AddressList[0]; IPAddress ipAddress; int remotePort = portNum; try { ipAddress = IPAddress.Parse(addr); } catch (Exception e) { lastSendErrorMsg = e.Message; return(false); } IPEndPoint remoteEP = new IPEndPoint(ipAddress, remotePort); // Create a TCP socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //udp //clientSocket = new Socket(AddressFamily.InterNetwork, // SocketType.Dgram, ProtocolType.Udp); //need send data to active the communication // Connect to the remote endpoint // We will wait for this to complete rather than do it // asynchronously receiveThread = new Thread((dataReceive)); receiveThread.CurrentCulture = new CultureInfo("en-US"); clientSocket.Connect(remoteEP); localPort = ((IPEndPoint)clientSocket.LocalEndPoint).Port; //for communication need to send a package first //Ping(); receiveThread.Start(); return(true); } catch (SocketException e) { socketErrorCount++; lastRecvError = e.ErrorCode; lastRecvErrorMsg = e.ToString(); return(false); } }