Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialPipeline.cs Source File

SerialPipeline.cs

00001 /*******************************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved.
00003 * 
00004 * This software is protected by copyright laws of the United States and
00005 * of foreign countries. This material may also be protected by patent laws
00006 * and technology transfer regulations of the United States and of foreign
00007 * countries. This software is furnished under a license agreement and/or a
00008 * nondisclosure agreement and may only be used or reproduced in accordance
00009 * with the terms of those agreements. Dissemination of this information to
00010 * any party or parties not specified in the license agreement and/or
00011 * nondisclosure agreement is expressly prohibited.
00012 *
00013 * The above copyright notice and this permission notice shall be included
00014 * in all copies or substantial portions of the Software.
00015 *
00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00020 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00021 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00022 * OTHER DEALINGS IN THE SOFTWARE.
00023 *
00024 * Except as contained in this notice, the name of Maxim Integrated
00025 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00026 * Products, Inc. Branding Policy.
00027 *
00028 * The mere transfer of this software does not imply any licenses
00029 * of trade secrets, proprietary technology, copyrights, patents,
00030 * trademarks, maskwork rights, or any other form of intellectual
00031 * property whatsoever. Maxim Integrated Products, Inc. retains all
00032 * ownership rights.
00033 *******************************************************************************
00034 */
00035 
00036 using System;
00037 using System.Collections.Generic;
00038 using System.Linq;
00039 using System.Text;
00040 using System.IO.Ports;
00041 using System.Diagnostics;
00042 using RPCSupport.Pipelines;
00043 using System.Threading;
00044 using System.Collections;
00045 
00046 namespace RPCSupport
00047 {
00048     public class SerialPipeline : Pipeline {
00049         protected SerialPort serialPort;
00050 
00051         public SerialPipeline() 
00052         {
00053             serialPort = new SerialPort();
00054         }
00055 
00056         public override void Connect(String PortName)
00057         {
00058             // Set the read/write timeouts
00059             serialPort.ReadTimeout = 90000;
00060             serialPort.WriteTimeout = 90000;
00061 
00062             serialPort.PortName = PortName;
00063             serialPort.BaudRate = 9600;
00064 
00065             // always the same
00066             serialPort.DataBits = 8;
00067             serialPort.Parity = Parity.None;
00068             serialPort.StopBits = StopBits.One;
00069             serialPort.Handshake = Handshake.None;
00070 
00071             serialPort.DtrEnable = true;
00072             serialPort.RtsEnable = true;
00073             Thread.Sleep(10);
00074             serialPort.Open();
00075 
00076             //serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
00077             //serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived);
00078         }
00079 
00080         public override void Disconnect()
00081         {
00082             serialPort.Close();
00083         }
00084 
00085         public override bool IsConnected()
00086         {
00087             return serialPort.IsOpen;
00088         }
00089 
00090         public override String[] ScanAvailablePortNames()
00091         {
00092             return SerialPort.GetPortNames();
00093         }
00094 
00095         /*
00096         public override String SendRPC(String Name, String Method, params String[] Args)
00097         {
00098             //write to serial port and receive result
00099             String Response;
00100             String Arguments = "";
00101 
00102             if(Args != null)
00103             {
00104                 for(int i = 0; i < Args.Length; i++)
00105                 {
00106                     Arguments = Arguments + " " + Args[i];
00107                 }
00108             }
00109 
00110             serialPort.DiscardInBuffer();
00111             String sendString = "/" + Name + "/" + Method + Arguments + "\r\n";
00112             String reply = RawRpcCall(sendString);
00113 
00114             return reply;
00115         }*/
00116 
00117         /*
00118         public override void delete()
00119         {
00120             //Close the serial port
00121             if (serialPort != null) serialPort.Close();
00122         }
00123         */
00124 
00125         private void serialPort_DataReceived(object sender, EventArgs e)
00126         {
00127             Debug.Print("Serial Port event");
00128             //String Interrupt = serialPort.ReadLine();
00129             String str = serialPort.ReadExisting();
00130         }
00131 
00132         private void serialPort_ErrorReceived(object sender, EventArgs e)
00133         {
00134             Debug.Print("Serial Port error");
00135         }
00136 
00137 
00138 
00139 
00140         //
00141         // Serial Byte Collection Thread
00142         //
00143         /*private BackgroundWorker bw = new BackgroundWorker();
00144         private void InitThread()
00145         {
00146             bw.WorkerReportsProgress = true;
00147             bw.WorkerSupportsCancellation = true;
00148             bw.DoWork += new DoWorkEventHandler(bw_DoWork);
00149             bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
00150             bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
00151         }
00152         private void StartThread()
00153         {
00154             if (bw.IsBusy != true)
00155             {
00156                 bw.RunWorkerAsync();
00157             }
00158         }
00159         private void StopThread()
00160         {
00161             if (bw.WorkerSupportsCancellation == true)
00162             {
00163                 bw.CancelAsync();
00164             }
00165         }
00166         private void bw_DoWork(object sender, DoWorkEventArgs e)
00167         {
00168             BackgroundWorker worker = sender as BackgroundWorker;
00169 
00170             for (int i = 1; (i <= 10); i++)
00171             {
00172                 if ((worker.CancellationPending == true))
00173                 {
00174                     e.Cancel = true;
00175                     break;
00176                 }
00177                 else
00178                 {
00179                     // Perform a time consuming operation and report progress.
00180                     System.Threading.Thread.Sleep(500);
00181                     worker.ReportProgress((i * 10));
00182                 }
00183             }
00184         }*/
00185 
00186         public override string RawRpcCall(string request, bool reply)
00187         {
00188             if (rawRpcRequest != null) rawRpcRequest(request);
00189             //String reply = myHID.RPC_Call(request);
00190             serialPort.WriteLine(request);
00191             if (reply == false) return "";
00192 
00193             string replyStr;
00194             //replyStr = serialPort.ReadLine();
00195             int ch;
00196             int lastCh;
00197             bool gotCR = false;
00198             bool gotLF = false;
00199             lastCh = 0;
00200             ch = serialPort.ReadByte();
00201             StringBuilder sb = new StringBuilder();
00202             StringBuilder sbDebug = new StringBuilder();
00203             while (ch != -1)
00204             {
00205                 //sbDebug.Append("0x" + ch.ToString("X2") + " ");
00206                 if (ch != '\n') 
00207                     sb.Append((char)ch);
00208                 if (lastCh == '\r' && ch == '\n') break;
00209                 lastCh = ch;
00210                 ch = serialPort.ReadByte();
00211             }
00212             replyStr = sb.ToString();
00213             if (rawRpcReply != null) rawRpcReply(sbDebug.ToString());
00214             if (rawRpcReply != null) rawRpcReply(replyStr);
00215             return replyStr;
00216         }
00217 
00218         public override string RawRpcCallBinary(string request, bool reply, out bool allOnes)
00219         {
00220             if (rawRpcRequest != null) rawRpcRequest(request);
00221             //String reply = myHID.RPC_Call(request);
00222             serialPort.WriteLine(request);
00223             //if (reply == false) return "";
00224 
00225             byte[] buffer = new byte[256];
00226             int bytesToRead = 256;
00227             while (bytesToRead != 0)
00228             {
00229                 int bytesRead = serialPort.Read(buffer, 256 - bytesToRead, bytesToRead);
00230                 bytesToRead -= bytesRead;
00231             }
00232             allOnes = true;
00233             for (int i = 0; i < 256; i++) {
00234                 if (buffer[i] != 0xFF) allOnes = false;
00235             }
00236             string replyStr;
00237             StringBuilder sb = new StringBuilder();
00238             for (int i = 0; i < 256; i++)
00239             {
00240                 sb.Append(buffer[i].ToString("X2") + " ");
00241             }
00242             //replyStr = serialPort.ReadLine();
00243             replyStr = sb.ToString().Trim();
00244             String otherStr = serialPort.ReadLine();
00245             if (rawRpcReply != null) rawRpcReply(replyStr);
00246             return replyStr;
00247         }
00248 
00249         public override void SendSingleByte(char ch)
00250         {
00251             if (rawRpcRequest != null) rawRpcRequest("<" + ch + ">");
00252             char[] chArray = new char[1];
00253             chArray[0] = ch;
00254             serialPort.Write(chArray, 0, 1);
00255         }
00256         public override string ReadString()
00257         {
00258             return serialPort.ReadExisting();
00259         }
00260         public override int Read(char[] buffer, int offset, int count)
00261         {
00262             int val = 0;
00263             try
00264             {
00265                 val = serialPort.Read(buffer, offset, count);
00266             }
00267             catch (Exception)
00268             {
00269             }
00270             return val;
00271         }
00272         public override void Discard()
00273         {
00274             serialPort.DiscardInBuffer();
00275         }
00276     }
00277 }