/// <summary> /// Service Start /// </summary> protected override void Start() { // Listen on the main port for requests and call the appropriate handler. Interleave mainInterleave = ActivateDsspOperationHandlers(); // Publish the service to the local Node Directory DirectoryInsert(); // display HTTP service Uri LogInfo(LogGroups.Console, "Service uri: "); //open Scribbler Communications port _scribblerComm = new ScribblerComm(); _scribblerComPort = new ScribblerDataPort(); _scribblerComPort = _scribblerComm.Open(6, 38400); //add custom handlers to interleave mainInterleave.CombineWith(new Interleave( new TeardownReceiverGroup(), new ExclusiveReceiverGroup( Arbiter.ReceiveWithIterator <SensorNotification>(true, _scribblerComPort, SensorNotificationHandler), Arbiter.ReceiveWithIterator <SetMotor>(true, _mainPort, SetMotorHandler), Arbiter.ReceiveWithIterator <Ping>(true, _mainPort, PingHandler), Arbiter.ReceiveWithIterator <SetLED>(true, _mainPort, SetLEDHandler), Arbiter.ReceiveWithIterator <PlayTone>(true, _mainPort, PlayToneHandler) ), new ConcurrentReceiverGroup() )); //play startup tone _scribblerComm.SendCommand(new ScribblerCommand((byte)ScribblerHelper.Commands.SET_SPEAKER, 20, 100, 200)); }
/// <summary> /// Close the connection to a serial port. /// </summary> private void Close() { if (serialPort != null) { if (serialPort.IsOpen) { serialPort.DataReceived -= new SerialDataReceivedEventHandler(serialPort_DataReceived); serialPort.Close(); } serialPort = null; ScribblerComInboundPort = null; } }
/// <summary> /// Open a serial port. /// </summary> /// <param name="comPort"></param> /// <param name="baudRate"></param> /// <returns>A Ccr Port for receiving serial port data</returns> internal ScribblerDataPort Open(int comPort, int baudRate) { if (serialPort != null) { Close(); } if (buffer == null) { buffer = new byte[128]; } Console.WriteLine("Opening COM port: " + comPort + " with baud rate: " + baudRate); serialPort = new SerialPort("COM" + comPort.ToString(System.Globalization.NumberFormatInfo.InvariantInfo), baudRate); serialPort.Encoding = Encoding.Default; serialPort.Parity = Parity.None; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; //serialPort.WriteTimeout = 2000; serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived); ScribblerComInboundPort = new ScribblerDataPort(); try { serialPort.Open(); } catch { Console.WriteLine("Invalid Serial Port."); throw new IOException(); } return(ScribblerComInboundPort); }