MCPcopy Create free account
hub / github.com/curiores/SerialAnalyzer / serialSetup

Function serialSetup

src/Utils/SerialData.js:104–205  ·  view source on GitHub ↗
(port)

Source from the content-addressed store, hash-verified

102
103var decIndex = 1; // Determines which data points to keep
104function serialSetup(port) {
105 // Notify the user that the serial port is being started
106 let toastId = toast("Starting Serial on " + GetPortShortName(port)
107 + " with baud rate: " + SerialDataObject.baudRate);
108
109
110 // Set the global port object
111 SerialDataObject.port = port;
112
113 // Starts the serial port
114 SerialDataObject.serialObj = new SerialPort({
115 path: port.path,
116 baudRate: SerialDataObject.baudRate,
117 autoOpen:true,
118 },(err)=>{
119 if(err){
120 toast.dismiss(toastId);
121 toast.error("Error starting serial port on " + GetPortShortName(port) + "\n\n" + err);
122 console.log(err);
123 }
124 else{
125 console.log("Successful connection");
126 }
127 }
128 );
129
130
131 // Parser
132 const parser = SerialDataObject.serialObj.pipe(new ReadlineParser({ delimiter: '\r\n' }))
133 // Run the parser to collect data
134 var onVal = parser.on('data', addData)
135
136 function addData(data) {
137 // If there is decimation, don't add the data until the decimation index is reached
138 if (decIndex >= GlobalSettings.global.decimation) {
139 decIndex = 1;
140 } else {
141 decIndex += 1;
142 return; // Dont add the data if the user is decimating
143 }
144 //--------------------- regular update --------------------- //
145 SerialDataObject.Iter += 1;// Iterate
146 // Computations for the sample rate (only do this once every NsampleRateUpdate samples)
147 if (SerialDataObject.Iter % SerialDataObject.NsampleRateUpdate === 0) {
148 if (SerialDataObject.sampleHistory.length > 1) {
149 var deltaT = SerialDataObject.timeHistory[SerialDataObject.timeHistory.length - 1] - SerialDataObject.timeHistory[0];
150 var samples = SerialDataObject.sampleHistory[SerialDataObject.sampleHistory.length - 1] - SerialDataObject.sampleHistory[0];
151
152 SerialDataObject.sampleRate = samples / deltaT * 1000;
153 }
154 }
155
156 // Push the raw data (unless its NaN)
157 SerialDataObject.rawData.push(data);
158 if (SerialDataObject.rawData.length >= SerialDataObject.bufferSize) {
159 // If the buffer is full, remove the first line of the raw data
160 SerialDataObject.rawData.shift();
161 }

Callers 1

StartSerialFunction · 0.85

Calls 1

GetPortShortNameFunction · 0.85

Tested by

no test coverage detected