Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SerialWrapPipeline.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 using SerialWrap; 00047 00048 namespace RPCSupport 00049 { 00050 public class SerialWrapPipeline : Pipeline { 00051 protected SerialPortIo serialPortIo; 00052 SerialPortConfig serialPortConfig; 00053 00054 public SerialWrapPipeline() 00055 { 00056 //serialPort = new SerialPort(); 00057 } 00058 00059 public int ReadTimeout 00060 { 00061 set 00062 { 00063 serialPortIo.ReadTimeout = value; 00064 } 00065 get 00066 { 00067 return serialPortIo.ReadTimeout; 00068 } 00069 } 00070 00071 public override void Connect(String PortName) 00072 { 00073 serialPortConfig = new SerialPortConfig(PortName, 9600, 8, StopBits.One, Parity.None, true, true); 00074 00075 /* 00076 // Set the read/write timeouts 00077 serialPort.ReadTimeout = 90000; 00078 serialPort.WriteTimeout = 90000; 00079 00080 serialPort.PortName = PortName; 00081 serialPort.BaudRate = 9600; 00082 00083 // always the same 00084 serialPort.DataBits = 8; 00085 serialPort.Parity = Parity.None; 00086 serialPort.StopBits = StopBits.One; 00087 serialPort.Handshake = Handshake.None; 00088 00089 serialPort.DtrEnable = true; 00090 serialPort.RtsEnable = true; 00091 */ 00092 00093 serialPortIo = new SerialPortIo(serialPortConfig); 00094 00095 serialPortIo.ReadTimeout = 1300; 00096 //Thread.Sleep(10); 00097 //serialPort.Open(); 00098 00099 //serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); 00100 //serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived); 00101 } 00102 00103 public override void Disconnect() 00104 { 00105 serialPortIo.Dispose(); 00106 } 00107 00108 public override bool IsConnected() 00109 { 00110 return serialPortIo.IsOpen; 00111 } 00112 00113 public override String[] ScanAvailablePortNames() 00114 { 00115 return SerialPort.GetPortNames(); 00116 } 00117 00118 /* 00119 public override String SendRPC(String Name, String Method, params String[] Args) 00120 { 00121 //write to serial port and receive result 00122 String Response; 00123 String Arguments = ""; 00124 00125 if(Args != null) 00126 { 00127 for(int i = 0; i < Args.Length; i++) 00128 { 00129 Arguments = Arguments + " " + Args[i]; 00130 } 00131 } 00132 00133 serialPort.DiscardInBuffer(); 00134 String sendString = "/" + Name + "/" + Method + Arguments + "\r\n"; 00135 String reply = RawRpcCall(sendString); 00136 00137 return reply; 00138 }*/ 00139 00140 /* 00141 public override void delete() 00142 { 00143 //Close the serial port 00144 if (serialPort != null) serialPort.Close(); 00145 } 00146 */ 00147 00148 private void serialPort_DataReceived(object sender, EventArgs e) 00149 { 00150 Debug.Print("Serial Port event"); 00151 //String Interrupt = serialPort.ReadLine(); 00152 String str = serialPortIo.ReadExisting(); 00153 } 00154 00155 private void serialPort_ErrorReceived(object sender, EventArgs e) 00156 { 00157 Debug.Print("Serial Port error"); 00158 } 00159 00160 public override string RawRpcCall(string request, bool reply) 00161 { 00162 if (rawRpcRequest != null) rawRpcRequest(request); 00163 //String reply = myHID.RPC_Call(request); 00164 serialPortIo.WriteLine(request); 00165 if (reply == false) return ""; 00166 00167 string replyStr; 00168 replyStr = serialPortIo.ReadLine(); 00169 if (rawRpcReply != null) rawRpcReply(replyStr); 00170 return replyStr; 00171 } 00172 00173 public override void SendSingleByte(char ch) 00174 { 00175 char[] chArray = new char[1]; 00176 chArray[0] = ch; 00177 serialPortIo.Write(chArray, 0, 1); 00178 } 00179 public override string ReadString() 00180 { 00181 return serialPortIo.ReadExisting(); 00182 } 00183 public override int Read(char[] buffer, int offset, int count) 00184 { 00185 int val = 0; 00186 try 00187 { 00188 val = serialPortIo.Read(buffer, offset, count); 00189 } 00190 catch (Exception) 00191 { 00192 } 00193 return val; 00194 } 00195 public override void Discard() 00196 { 00197 serialPortIo.DiscardInBuffer(); 00198 } 00199 00200 public override string RawRpcCallBinary(string request, bool reply, out bool allOnes) 00201 { 00202 if (rawRpcRequest != null) rawRpcRequest(request); 00203 //String reply = myHID.RPC_Call(request); 00204 serialPortIo.WriteLine(request); 00205 //if (reply == false) return ""; 00206 00207 byte[] buffer = new byte[256]; 00208 int bytesToRead = 256; 00209 while (bytesToRead != 0) 00210 { 00211 int bytesRead = serialPortIo.Read(buffer, 256 - bytesToRead, bytesToRead); 00212 bytesToRead -= bytesRead; 00213 } 00214 allOnes = true; 00215 for (int i = 0; i < 256; i++) 00216 { 00217 if (buffer[i] != 0xFF) allOnes = false; 00218 } 00219 string replyStr; 00220 StringBuilder sb = new StringBuilder(); 00221 for (int i = 0; i < 256; i++) 00222 { 00223 sb.Append((buffer[i]).ToString("X2") + " "); 00224 } 00225 //replyStr = serialPort.ReadLine(); 00226 replyStr = sb.ToString().Trim(); 00227 String otherStr = serialPortIo.ReadLine(); 00228 if (rawRpcReply != null) rawRpcReply(replyStr); 00229 return replyStr; 00230 } 00231 } 00232 }
Generated on Tue Jul 12 2022 21:52:40 by
1.7.2