public bool addSerialPort(string port) { internalDataStructure data; try { data = new internalDataStructure(); data.sp = new SerialPort(port, 9600, Parity.None, 8, StopBits.One); data.sp.Open(); } catch (Exception e) { e.ToString(); return false; } if (data.sp == null) { return false; } data.sp.ReadTimeout = 0; // there is a 1 relay relay nRelay = new relay(relay.RELAY_STATE.ON, 1); data.relays.Add(nRelay); m_connected.Add(data); sendSetState(nRelay, relay.RELAY_STATE.ON, data); nRelay = new relay(relay.RELAY_STATE.ON, 2); data.relays.Add(nRelay); m_connected.Add(data); sendSetState(nRelay, relay.RELAY_STATE.ON, data); return true; }
void sendSetState(relay to, relay.RELAY_STATE state, internalDataStructure DS) { byte data = 0; data |= (byte)to.getID(); // write the id data |= (byte)(state == relay.RELAY_STATE.ON ? 0x20 : 0); sendData(data, DS.sp); // adding the data to the queue, so we know what we should recieve later DS.packetData.Add((byte)to.getID()); }
public bool setState(byte relayID, relay.RELAY_STATE state) { // we only got 1 in m_connected right now, so we're allowede to do this internalDataStructure ds = m_connected[0]; for (int i = 0; i < ds.relays.Count; i++) { if (ds.relays[i].getID() == relayID) { sendSetState(ds.relays[i], state, ds); ds.relays[i].setState(state); return true; } } return false; }