public override bool Connect(RobotConnParm parm) { bool flag = false; if (isConnected) { UpdateInfo(string.Format("Already connected to {0}", connTarget), UTIL.InfoType.alert); return(false); } serialPort.PortName = parm.portName; serialPort.BaudRate = parm.baudRate; serialPort.Parity = parm.parity; serialPort.DataBits = parm.dataBits; serialPort.StopBits = parm.stopBits; connTarget = String.Format("{0}-{1},{2},{3},{4}", parm.portName, parm.baudRate, parm.parity, parm.dataBits, parm.stopBits); try { CallOnConnect(this, eventStatus.before); serialPort.Open(); CallOnConnect(this, eventStatus.after); if (isConnected) { serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); UpdateInfo(string.Format("Port {0} connected - {1}, {2}, {3}, {4}", serialPort.PortName, serialPort.BaudRate, serialPort.Parity, serialPort.DataBits, serialPort.StopBits )); UTIL.WriteRegistry(UTIL.KEY.LAST_CONNECTION_SERIAL, parm.portName); flag = true; } else { UpdateInfo(string.Format("Fail connecting to Port {0} - 115200, N, 8, 1", serialPort.PortName), UTIL.InfoType.error); } } catch (Exception ex) { UpdateInfo("Error: " + ex.Message, UTIL.InfoType.error); } if (!flag) { connTarget = "{" + connTarget + "} - failed"; } return(flag); }
public override bool Connect(RobotConnParm parm) { if (isConnected) { UpdateInfo(string.Format("Already connected to {0}", connTarget), UTIL.InfoType.alert); return(false); } if (parm.address == null) { if ((parm.hostName == null) || (parm.hostName == "")) { parm.hostName = "localhost"; } connTarget = string.Format("{0}:{1}", parm.hostName, parm.port); client.Connect(parm.hostName, parm.port); } else { connTarget = string.Format("{0}:{1}", parm.address, parm.port); client.Connect(parm.address, parm.port); } if (client.Connected) { if (parm.address == null) { UTIL.WriteRegistry(UTIL.KEY.LAST_CONNECTION_IP, parm.hostName); } else { UTIL.WriteRegistry(UTIL.KEY.LAST_CONNECTION_IP, parm.address.ToString()); } UTIL.WriteRegistry(UTIL.KEY.LAST_CONNECTION_PORT, parm.port.ToString()); } else { connTarget = "{" + connTarget + "} - failed"; } return(client.Connected); }