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 parseByte(byte data, internalDataStructure DS) { if (!isFromArduino(data)) { Console.WriteLine("got wrong packet, it doesn't appear to be from arduino but it is!!"); return; } Console.WriteLine("got packet {0}", data); parseArduinoData(data, DS); }
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()); }
void parseArduinoData(byte data, internalDataStructure DS) { // extract the id byte id = (byte)(data & 31); Console.WriteLine("server got id {0} from arduino", id); // this should be the first if (DS.packetData.Count == 0) { Console.WriteLine("Got data from Arduino but didn't expect any"); return; } for(int i = 0; i < DS.packetData.Count; i++) { if (DS.packetData[i] == id) { DS.packetData.RemoveAt(i); Console.WriteLine("Got arduino data as expected with id {0}", id); break; } } }