repo time
Dependencies: mbed MAX14720 MAX30205 USBDevice
HspGuiSourceV301/GuiDLLs/RPCSupport/Pipelines/SerialPipeline.cs@20:6d2af70c92ab, 2021-04-06 (annotated)
- Committer:
- darienf
- Date:
- Tue Apr 06 06:41:40 2021 +0000
- Revision:
- 20:6d2af70c92ab
another repo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darienf | 20:6d2af70c92ab | 1 | /******************************************************************************* |
darienf | 20:6d2af70c92ab | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved. |
darienf | 20:6d2af70c92ab | 3 | * |
darienf | 20:6d2af70c92ab | 4 | * This software is protected by copyright laws of the United States and |
darienf | 20:6d2af70c92ab | 5 | * of foreign countries. This material may also be protected by patent laws |
darienf | 20:6d2af70c92ab | 6 | * and technology transfer regulations of the United States and of foreign |
darienf | 20:6d2af70c92ab | 7 | * countries. This software is furnished under a license agreement and/or a |
darienf | 20:6d2af70c92ab | 8 | * nondisclosure agreement and may only be used or reproduced in accordance |
darienf | 20:6d2af70c92ab | 9 | * with the terms of those agreements. Dissemination of this information to |
darienf | 20:6d2af70c92ab | 10 | * any party or parties not specified in the license agreement and/or |
darienf | 20:6d2af70c92ab | 11 | * nondisclosure agreement is expressly prohibited. |
darienf | 20:6d2af70c92ab | 12 | * |
darienf | 20:6d2af70c92ab | 13 | * The above copyright notice and this permission notice shall be included |
darienf | 20:6d2af70c92ab | 14 | * in all copies or substantial portions of the Software. |
darienf | 20:6d2af70c92ab | 15 | * |
darienf | 20:6d2af70c92ab | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
darienf | 20:6d2af70c92ab | 17 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
darienf | 20:6d2af70c92ab | 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
darienf | 20:6d2af70c92ab | 19 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
darienf | 20:6d2af70c92ab | 20 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
darienf | 20:6d2af70c92ab | 21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
darienf | 20:6d2af70c92ab | 22 | * OTHER DEALINGS IN THE SOFTWARE. |
darienf | 20:6d2af70c92ab | 23 | * |
darienf | 20:6d2af70c92ab | 24 | * Except as contained in this notice, the name of Maxim Integrated |
darienf | 20:6d2af70c92ab | 25 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
darienf | 20:6d2af70c92ab | 26 | * Products, Inc. Branding Policy. |
darienf | 20:6d2af70c92ab | 27 | * |
darienf | 20:6d2af70c92ab | 28 | * The mere transfer of this software does not imply any licenses |
darienf | 20:6d2af70c92ab | 29 | * of trade secrets, proprietary technology, copyrights, patents, |
darienf | 20:6d2af70c92ab | 30 | * trademarks, maskwork rights, or any other form of intellectual |
darienf | 20:6d2af70c92ab | 31 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
darienf | 20:6d2af70c92ab | 32 | * ownership rights. |
darienf | 20:6d2af70c92ab | 33 | ******************************************************************************* |
darienf | 20:6d2af70c92ab | 34 | */ |
darienf | 20:6d2af70c92ab | 35 | |
darienf | 20:6d2af70c92ab | 36 | using System; |
darienf | 20:6d2af70c92ab | 37 | using System.Collections.Generic; |
darienf | 20:6d2af70c92ab | 38 | using System.Linq; |
darienf | 20:6d2af70c92ab | 39 | using System.Text; |
darienf | 20:6d2af70c92ab | 40 | using System.IO.Ports; |
darienf | 20:6d2af70c92ab | 41 | using System.Diagnostics; |
darienf | 20:6d2af70c92ab | 42 | using RPCSupport.Pipelines; |
darienf | 20:6d2af70c92ab | 43 | using System.Threading; |
darienf | 20:6d2af70c92ab | 44 | using System.Collections; |
darienf | 20:6d2af70c92ab | 45 | |
darienf | 20:6d2af70c92ab | 46 | namespace RPCSupport |
darienf | 20:6d2af70c92ab | 47 | { |
darienf | 20:6d2af70c92ab | 48 | public class SerialPipeline : Pipeline { |
darienf | 20:6d2af70c92ab | 49 | protected SerialPort serialPort; |
darienf | 20:6d2af70c92ab | 50 | |
darienf | 20:6d2af70c92ab | 51 | public SerialPipeline() |
darienf | 20:6d2af70c92ab | 52 | { |
darienf | 20:6d2af70c92ab | 53 | serialPort = new SerialPort(); |
darienf | 20:6d2af70c92ab | 54 | } |
darienf | 20:6d2af70c92ab | 55 | |
darienf | 20:6d2af70c92ab | 56 | public override void Connect(String PortName) |
darienf | 20:6d2af70c92ab | 57 | { |
darienf | 20:6d2af70c92ab | 58 | // Set the read/write timeouts |
darienf | 20:6d2af70c92ab | 59 | serialPort.ReadTimeout = 90000; |
darienf | 20:6d2af70c92ab | 60 | serialPort.WriteTimeout = 90000; |
darienf | 20:6d2af70c92ab | 61 | |
darienf | 20:6d2af70c92ab | 62 | serialPort.PortName = PortName; |
darienf | 20:6d2af70c92ab | 63 | serialPort.BaudRate = 9600; |
darienf | 20:6d2af70c92ab | 64 | |
darienf | 20:6d2af70c92ab | 65 | // always the same |
darienf | 20:6d2af70c92ab | 66 | serialPort.DataBits = 8; |
darienf | 20:6d2af70c92ab | 67 | serialPort.Parity = Parity.None; |
darienf | 20:6d2af70c92ab | 68 | serialPort.StopBits = StopBits.One; |
darienf | 20:6d2af70c92ab | 69 | serialPort.Handshake = Handshake.None; |
darienf | 20:6d2af70c92ab | 70 | |
darienf | 20:6d2af70c92ab | 71 | serialPort.DtrEnable = true; |
darienf | 20:6d2af70c92ab | 72 | serialPort.RtsEnable = true; |
darienf | 20:6d2af70c92ab | 73 | Thread.Sleep(10); |
darienf | 20:6d2af70c92ab | 74 | serialPort.Open(); |
darienf | 20:6d2af70c92ab | 75 | |
darienf | 20:6d2af70c92ab | 76 | //serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); |
darienf | 20:6d2af70c92ab | 77 | //serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived); |
darienf | 20:6d2af70c92ab | 78 | } |
darienf | 20:6d2af70c92ab | 79 | |
darienf | 20:6d2af70c92ab | 80 | public override void Disconnect() |
darienf | 20:6d2af70c92ab | 81 | { |
darienf | 20:6d2af70c92ab | 82 | serialPort.Close(); |
darienf | 20:6d2af70c92ab | 83 | } |
darienf | 20:6d2af70c92ab | 84 | |
darienf | 20:6d2af70c92ab | 85 | public override bool IsConnected() |
darienf | 20:6d2af70c92ab | 86 | { |
darienf | 20:6d2af70c92ab | 87 | return serialPort.IsOpen; |
darienf | 20:6d2af70c92ab | 88 | } |
darienf | 20:6d2af70c92ab | 89 | |
darienf | 20:6d2af70c92ab | 90 | public override String[] ScanAvailablePortNames() |
darienf | 20:6d2af70c92ab | 91 | { |
darienf | 20:6d2af70c92ab | 92 | return SerialPort.GetPortNames(); |
darienf | 20:6d2af70c92ab | 93 | } |
darienf | 20:6d2af70c92ab | 94 | |
darienf | 20:6d2af70c92ab | 95 | /* |
darienf | 20:6d2af70c92ab | 96 | public override String SendRPC(String Name, String Method, params String[] Args) |
darienf | 20:6d2af70c92ab | 97 | { |
darienf | 20:6d2af70c92ab | 98 | //write to serial port and receive result |
darienf | 20:6d2af70c92ab | 99 | String Response; |
darienf | 20:6d2af70c92ab | 100 | String Arguments = ""; |
darienf | 20:6d2af70c92ab | 101 | |
darienf | 20:6d2af70c92ab | 102 | if(Args != null) |
darienf | 20:6d2af70c92ab | 103 | { |
darienf | 20:6d2af70c92ab | 104 | for(int i = 0; i < Args.Length; i++) |
darienf | 20:6d2af70c92ab | 105 | { |
darienf | 20:6d2af70c92ab | 106 | Arguments = Arguments + " " + Args[i]; |
darienf | 20:6d2af70c92ab | 107 | } |
darienf | 20:6d2af70c92ab | 108 | } |
darienf | 20:6d2af70c92ab | 109 | |
darienf | 20:6d2af70c92ab | 110 | serialPort.DiscardInBuffer(); |
darienf | 20:6d2af70c92ab | 111 | String sendString = "/" + Name + "/" + Method + Arguments + "\r\n"; |
darienf | 20:6d2af70c92ab | 112 | String reply = RawRpcCall(sendString); |
darienf | 20:6d2af70c92ab | 113 | |
darienf | 20:6d2af70c92ab | 114 | return reply; |
darienf | 20:6d2af70c92ab | 115 | }*/ |
darienf | 20:6d2af70c92ab | 116 | |
darienf | 20:6d2af70c92ab | 117 | /* |
darienf | 20:6d2af70c92ab | 118 | public override void delete() |
darienf | 20:6d2af70c92ab | 119 | { |
darienf | 20:6d2af70c92ab | 120 | //Close the serial port |
darienf | 20:6d2af70c92ab | 121 | if (serialPort != null) serialPort.Close(); |
darienf | 20:6d2af70c92ab | 122 | } |
darienf | 20:6d2af70c92ab | 123 | */ |
darienf | 20:6d2af70c92ab | 124 | |
darienf | 20:6d2af70c92ab | 125 | private void serialPort_DataReceived(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 126 | { |
darienf | 20:6d2af70c92ab | 127 | Debug.Print("Serial Port event"); |
darienf | 20:6d2af70c92ab | 128 | //String Interrupt = serialPort.ReadLine(); |
darienf | 20:6d2af70c92ab | 129 | String str = serialPort.ReadExisting(); |
darienf | 20:6d2af70c92ab | 130 | } |
darienf | 20:6d2af70c92ab | 131 | |
darienf | 20:6d2af70c92ab | 132 | private void serialPort_ErrorReceived(object sender, EventArgs e) |
darienf | 20:6d2af70c92ab | 133 | { |
darienf | 20:6d2af70c92ab | 134 | Debug.Print("Serial Port error"); |
darienf | 20:6d2af70c92ab | 135 | } |
darienf | 20:6d2af70c92ab | 136 | |
darienf | 20:6d2af70c92ab | 137 | |
darienf | 20:6d2af70c92ab | 138 | |
darienf | 20:6d2af70c92ab | 139 | |
darienf | 20:6d2af70c92ab | 140 | // |
darienf | 20:6d2af70c92ab | 141 | // Serial Byte Collection Thread |
darienf | 20:6d2af70c92ab | 142 | // |
darienf | 20:6d2af70c92ab | 143 | /*private BackgroundWorker bw = new BackgroundWorker(); |
darienf | 20:6d2af70c92ab | 144 | private void InitThread() |
darienf | 20:6d2af70c92ab | 145 | { |
darienf | 20:6d2af70c92ab | 146 | bw.WorkerReportsProgress = true; |
darienf | 20:6d2af70c92ab | 147 | bw.WorkerSupportsCancellation = true; |
darienf | 20:6d2af70c92ab | 148 | bw.DoWork += new DoWorkEventHandler(bw_DoWork); |
darienf | 20:6d2af70c92ab | 149 | bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); |
darienf | 20:6d2af70c92ab | 150 | bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); |
darienf | 20:6d2af70c92ab | 151 | } |
darienf | 20:6d2af70c92ab | 152 | private void StartThread() |
darienf | 20:6d2af70c92ab | 153 | { |
darienf | 20:6d2af70c92ab | 154 | if (bw.IsBusy != true) |
darienf | 20:6d2af70c92ab | 155 | { |
darienf | 20:6d2af70c92ab | 156 | bw.RunWorkerAsync(); |
darienf | 20:6d2af70c92ab | 157 | } |
darienf | 20:6d2af70c92ab | 158 | } |
darienf | 20:6d2af70c92ab | 159 | private void StopThread() |
darienf | 20:6d2af70c92ab | 160 | { |
darienf | 20:6d2af70c92ab | 161 | if (bw.WorkerSupportsCancellation == true) |
darienf | 20:6d2af70c92ab | 162 | { |
darienf | 20:6d2af70c92ab | 163 | bw.CancelAsync(); |
darienf | 20:6d2af70c92ab | 164 | } |
darienf | 20:6d2af70c92ab | 165 | } |
darienf | 20:6d2af70c92ab | 166 | private void bw_DoWork(object sender, DoWorkEventArgs e) |
darienf | 20:6d2af70c92ab | 167 | { |
darienf | 20:6d2af70c92ab | 168 | BackgroundWorker worker = sender as BackgroundWorker; |
darienf | 20:6d2af70c92ab | 169 | |
darienf | 20:6d2af70c92ab | 170 | for (int i = 1; (i <= 10); i++) |
darienf | 20:6d2af70c92ab | 171 | { |
darienf | 20:6d2af70c92ab | 172 | if ((worker.CancellationPending == true)) |
darienf | 20:6d2af70c92ab | 173 | { |
darienf | 20:6d2af70c92ab | 174 | e.Cancel = true; |
darienf | 20:6d2af70c92ab | 175 | break; |
darienf | 20:6d2af70c92ab | 176 | } |
darienf | 20:6d2af70c92ab | 177 | else |
darienf | 20:6d2af70c92ab | 178 | { |
darienf | 20:6d2af70c92ab | 179 | // Perform a time consuming operation and report progress. |
darienf | 20:6d2af70c92ab | 180 | System.Threading.Thread.Sleep(500); |
darienf | 20:6d2af70c92ab | 181 | worker.ReportProgress((i * 10)); |
darienf | 20:6d2af70c92ab | 182 | } |
darienf | 20:6d2af70c92ab | 183 | } |
darienf | 20:6d2af70c92ab | 184 | }*/ |
darienf | 20:6d2af70c92ab | 185 | |
darienf | 20:6d2af70c92ab | 186 | public override string RawRpcCall(string request, bool reply) |
darienf | 20:6d2af70c92ab | 187 | { |
darienf | 20:6d2af70c92ab | 188 | if (rawRpcRequest != null) rawRpcRequest(request); |
darienf | 20:6d2af70c92ab | 189 | //String reply = myHID.RPC_Call(request); |
darienf | 20:6d2af70c92ab | 190 | serialPort.WriteLine(request); |
darienf | 20:6d2af70c92ab | 191 | if (reply == false) return ""; |
darienf | 20:6d2af70c92ab | 192 | |
darienf | 20:6d2af70c92ab | 193 | string replyStr; |
darienf | 20:6d2af70c92ab | 194 | //replyStr = serialPort.ReadLine(); |
darienf | 20:6d2af70c92ab | 195 | int ch; |
darienf | 20:6d2af70c92ab | 196 | int lastCh; |
darienf | 20:6d2af70c92ab | 197 | bool gotCR = false; |
darienf | 20:6d2af70c92ab | 198 | bool gotLF = false; |
darienf | 20:6d2af70c92ab | 199 | lastCh = 0; |
darienf | 20:6d2af70c92ab | 200 | ch = serialPort.ReadByte(); |
darienf | 20:6d2af70c92ab | 201 | StringBuilder sb = new StringBuilder(); |
darienf | 20:6d2af70c92ab | 202 | StringBuilder sbDebug = new StringBuilder(); |
darienf | 20:6d2af70c92ab | 203 | while (ch != -1) |
darienf | 20:6d2af70c92ab | 204 | { |
darienf | 20:6d2af70c92ab | 205 | //sbDebug.Append("0x" + ch.ToString("X2") + " "); |
darienf | 20:6d2af70c92ab | 206 | if (ch != '\n') |
darienf | 20:6d2af70c92ab | 207 | sb.Append((char)ch); |
darienf | 20:6d2af70c92ab | 208 | if (lastCh == '\r' && ch == '\n') break; |
darienf | 20:6d2af70c92ab | 209 | lastCh = ch; |
darienf | 20:6d2af70c92ab | 210 | ch = serialPort.ReadByte(); |
darienf | 20:6d2af70c92ab | 211 | } |
darienf | 20:6d2af70c92ab | 212 | replyStr = sb.ToString(); |
darienf | 20:6d2af70c92ab | 213 | if (rawRpcReply != null) rawRpcReply(sbDebug.ToString()); |
darienf | 20:6d2af70c92ab | 214 | if (rawRpcReply != null) rawRpcReply(replyStr); |
darienf | 20:6d2af70c92ab | 215 | return replyStr; |
darienf | 20:6d2af70c92ab | 216 | } |
darienf | 20:6d2af70c92ab | 217 | |
darienf | 20:6d2af70c92ab | 218 | public override string RawRpcCallBinary(string request, bool reply, out bool allOnes) |
darienf | 20:6d2af70c92ab | 219 | { |
darienf | 20:6d2af70c92ab | 220 | if (rawRpcRequest != null) rawRpcRequest(request); |
darienf | 20:6d2af70c92ab | 221 | //String reply = myHID.RPC_Call(request); |
darienf | 20:6d2af70c92ab | 222 | serialPort.WriteLine(request); |
darienf | 20:6d2af70c92ab | 223 | //if (reply == false) return ""; |
darienf | 20:6d2af70c92ab | 224 | |
darienf | 20:6d2af70c92ab | 225 | byte[] buffer = new byte[256]; |
darienf | 20:6d2af70c92ab | 226 | int bytesToRead = 256; |
darienf | 20:6d2af70c92ab | 227 | while (bytesToRead != 0) |
darienf | 20:6d2af70c92ab | 228 | { |
darienf | 20:6d2af70c92ab | 229 | int bytesRead = serialPort.Read(buffer, 256 - bytesToRead, bytesToRead); |
darienf | 20:6d2af70c92ab | 230 | bytesToRead -= bytesRead; |
darienf | 20:6d2af70c92ab | 231 | } |
darienf | 20:6d2af70c92ab | 232 | allOnes = true; |
darienf | 20:6d2af70c92ab | 233 | for (int i = 0; i < 256; i++) { |
darienf | 20:6d2af70c92ab | 234 | if (buffer[i] != 0xFF) allOnes = false; |
darienf | 20:6d2af70c92ab | 235 | } |
darienf | 20:6d2af70c92ab | 236 | string replyStr; |
darienf | 20:6d2af70c92ab | 237 | StringBuilder sb = new StringBuilder(); |
darienf | 20:6d2af70c92ab | 238 | for (int i = 0; i < 256; i++) |
darienf | 20:6d2af70c92ab | 239 | { |
darienf | 20:6d2af70c92ab | 240 | sb.Append(buffer[i].ToString("X2") + " "); |
darienf | 20:6d2af70c92ab | 241 | } |
darienf | 20:6d2af70c92ab | 242 | //replyStr = serialPort.ReadLine(); |
darienf | 20:6d2af70c92ab | 243 | replyStr = sb.ToString().Trim(); |
darienf | 20:6d2af70c92ab | 244 | String otherStr = serialPort.ReadLine(); |
darienf | 20:6d2af70c92ab | 245 | if (rawRpcReply != null) rawRpcReply(replyStr); |
darienf | 20:6d2af70c92ab | 246 | return replyStr; |
darienf | 20:6d2af70c92ab | 247 | } |
darienf | 20:6d2af70c92ab | 248 | |
darienf | 20:6d2af70c92ab | 249 | public override void SendSingleByte(char ch) |
darienf | 20:6d2af70c92ab | 250 | { |
darienf | 20:6d2af70c92ab | 251 | if (rawRpcRequest != null) rawRpcRequest("<" + ch + ">"); |
darienf | 20:6d2af70c92ab | 252 | char[] chArray = new char[1]; |
darienf | 20:6d2af70c92ab | 253 | chArray[0] = ch; |
darienf | 20:6d2af70c92ab | 254 | serialPort.Write(chArray, 0, 1); |
darienf | 20:6d2af70c92ab | 255 | } |
darienf | 20:6d2af70c92ab | 256 | public override string ReadString() |
darienf | 20:6d2af70c92ab | 257 | { |
darienf | 20:6d2af70c92ab | 258 | return serialPort.ReadExisting(); |
darienf | 20:6d2af70c92ab | 259 | } |
darienf | 20:6d2af70c92ab | 260 | public override int Read(char[] buffer, int offset, int count) |
darienf | 20:6d2af70c92ab | 261 | { |
darienf | 20:6d2af70c92ab | 262 | int val = 0; |
darienf | 20:6d2af70c92ab | 263 | try |
darienf | 20:6d2af70c92ab | 264 | { |
darienf | 20:6d2af70c92ab | 265 | val = serialPort.Read(buffer, offset, count); |
darienf | 20:6d2af70c92ab | 266 | } |
darienf | 20:6d2af70c92ab | 267 | catch (Exception) |
darienf | 20:6d2af70c92ab | 268 | { |
darienf | 20:6d2af70c92ab | 269 | } |
darienf | 20:6d2af70c92ab | 270 | return val; |
darienf | 20:6d2af70c92ab | 271 | } |
darienf | 20:6d2af70c92ab | 272 | public override void Discard() |
darienf | 20:6d2af70c92ab | 273 | { |
darienf | 20:6d2af70c92ab | 274 | serialPort.DiscardInBuffer(); |
darienf | 20:6d2af70c92ab | 275 | } |
darienf | 20:6d2af70c92ab | 276 | } |
darienf | 20:6d2af70c92ab | 277 | } |