Skip to content

Reference

Module: nexgraph.py Description: NexGraph Python library for Nextech force gauges. Author: Shawn Myratchapon Website: https://simplyshawn.co.th NexGraph: https://nexgraphapp.com Version: 2.1.0

NexGraph

NexGraph Python Library - Finds and connects to Nextech devices over USB serial port - Send and receive data to and from the device - Force Mode: - Download saved values - Get device information - Query different force value formats - Send commands Print, Reset, Zero, Unit, and Mode - Torque Mode: - Read data from the device

Attributes: - device_info: string: Device model, and details. - device_path: string: Path or port device is connected to. - device_command: dict: Commands to send to the device. - usb_serial: serial.Serial: Serial connection to the device. - force_mode: bool: Current force mode (True for tension, False for compression).

Methods: - find(): Searches for connected USB serial devices. - connect(): Tries to connect to the device on device_path. - get_info(): Returns the device information.
- zero(): Zeroes the current value on the device. - mode(): Changes the mode (track and peak) on the device. - unit(): Changes the unit of measurement on the device. - reset(): Resets the value on the device. - print_value(): Returns the current value or max value from the device. - peak_tension(): Returns the current peak tension value. - peak_compression(): Returns the current peak compression value.
- mini_output(): Returns the current value in mini format. - short_output(): Returns the current value in short format. - long_output(): Returns the current value in long format. - download(out_format="raw"): Return the data stored on the device memory. - read_torque_data(out_format="raw"): Reads torque data from the device. - disconnect(): Disconnects from the Nextech Gauge.

Source code in nexgraphpy\nexgraph.py
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
class NexGraph:
    """NexGraph Python Library
        - Finds and connects to Nextech devices over USB serial port
        - Send and receive data to and from the device
            - Force Mode:
                - Download saved values
                - Get device information
                - Query different force value formats
                - Send commands Print, Reset, Zero, Unit, and Mode
            - Torque Mode:
                - Read data from the device

        Attributes:
        - `device_info`: string: Device model, and details.
        - `device_path`: string: Path or port device is connected to.
        - `device_command`: dict: Commands to send to the device.
        - `usb_serial`: serial.Serial: Serial connection to the device.
        - `force_mode`: bool: Current force mode (True for tension, False for compression).

        Methods:
        - `find()`: Searches for connected USB serial devices.
        - `connect()`: Tries to connect to the device on device_path.
        - `get_info()`: Returns the device information.        
        - `zero()`: Zeroes the current value on the device.
        - `mode()`: Changes the mode (track and peak) on the device.
        - `unit()`: Changes the unit of measurement on the device.
        - `reset()`: Resets the value on the device.
        - `print_value()`: Returns the current value or max value from the device.
        - `peak_tension()`: Returns the current peak tension value.
        - `peak_compression()`: Returns the current peak compression value.        
        - `mini_output()`: Returns the current value in mini format.
        - `short_output()`: Returns the current value in short format.
        - `long_output()`: Returns the current value in long format.
        - `download(out_format="raw")`: Return the data stored on the device memory.
        - `read_torque_data(out_format="raw")`: Reads torque data from the device.
        - `disconnect()`: Disconnects from the Nextech Gauge.
    """

    def __init__(self, device_path=""):
        self.device_info: str = ""
        self.device_path: str = device_path if device_path else ""
        self.device_command: dict = {
            "!": '!'.encode(),  # device info
            "x": 'x'.encode(),  # print
            "p": 'p'.encode(),  # peak tension
            "c": 'c'.encode(),  # peak compression
            "l": 'l'.encode(),  # long output
            "v": 'v'.encode(),  # short output
            "L": 'L'.encode(),  # mini output
            "d": 'd'.encode(),  # download memory
            "z": 'z'.encode(),  # zero/tare
            "r": 'r'.encode(),  # reset values
            "u": 'u'.encode(),  # change unit
            "m": 'm'.encode()  # change mode
        }
        self.usb_serial = None
        self.force_mode: bool = True

    def find(self) -> bool:
        """Finds USB serial devices.

        Returns: boolean: "True" if a device was found.
        """
        usb_ports = serial.tools.list_ports.comports(include_links=True)
        device_found: bool = False

        for port in usb_ports:
            if port.manufacturer is None:
                continue
            if platform.system() == 'Windows':
                if "COM" in port.device and "FTDI" in port.manufacturer:
                    device_found = True
                    self.device_path = port.device
            elif platform.system() == 'Darwin':
                if "usbserial" in port.device and "FTDI" in port.manufacturer:
                    self.device_path = f"/dev/tty.{port.device.split('.')[1]}"
                    device_found = True
            elif platform.system() == 'Linux':
                if "ttyUSB" in port.device and "FTDI" in port.manufacturer:
                    self.device_path = port.device
                    device_found = True

        return device_found

    def connect(self, force_mode=True, rate="high") -> bool:
        """Connect to a force gauge or torque tester.

        Args:
            force_mode (bool): If True, connect in force mode; if False, connect in torque mode.
            rate (str): The baud rate ("high" or "low").

        Returns: boolean: "True" if connection was successful.
        """
        status: bool = False
        if self.device_path:
            if rate == "high":
                baud_rate = 38400
            elif rate == "low":
                baud_rate = 9600
            else:
                print("Invalid rate specified. Use 'high' or 'low'.")
                return status

            if force_mode:
                self.force_mode = True
            else:
                self.force_mode = False

            self.usb_serial = serial.Serial(port=self.device_path,
                                            bytesize=8,
                                            baudrate=baud_rate,
                                            timeout=2,
                                            stopbits=serial.STOPBITS_ONE)
            try:
                if self.force_mode:
                    self.usb_serial.write(self.device_command['!'])
                    time.sleep(0.1)

                    if self.usb_serial.in_waiting:
                        self.device_info += self.usb_serial.readline().decode("Ascii")
                        arr_info = self.device_info.split()

                        if arr_info and len(arr_info) > 2:
                            status = True

                else:
                    status = True

                return status
            except (OSError, serial.SerialException, PermissionError) as e:
                print(str(e))
                return status
        else:
            print("The device connection path is empty.")
            return status

    def get_info(self) -> str:
        """Gets the device details.        

        Returns:
            string: Model number, Current offset, Overload counter."""
        if self.force_mode:
            return self.device_info

        return ""

    def zero(self) -> bool:
        """Send the zero/tare command to the device.

        Returns:
            boolean: "True" if successful."""
        if self.force_mode:
            return self._send_command('z')

        return False

    def mode(self) -> bool:
        """Send the change mode command to the device.

        Returns:
            boolean: "True" if successful."""
        if self.force_mode:
            return self._send_command('m')

        return False

    def unit(self) -> bool:
        """Send the change unit command to the device.

        Returns:
            boolean: "True" if successful."""
        if self.force_mode:
            return self._send_command('u')

        return False

    def reset(self) -> bool:
        """Send the reset values command to the device.

        Returns:
            boolean: "True" if successful."""
        if self.force_mode:
            return self._send_command('r')

        return False

    def _send_command(self, command: str) -> bool:
        """Helper method to send a command to the Nextech Gauge.

        Args:
            command (str): The command to send to the device.

        Returns:
            bool: True if the command was sent successfully, False otherwise.
        """
        try:
            if not self.usb_serial:
                print("Error: Device is not connected.")
                return False

            self.usb_serial.write(self.device_command[command])
            return True
        except (OSError, serial.SerialException) as e:
            print(str(e))
            return False

    def print_value(self) -> str:
        """Send a print command to the device.

        Returns:
            boolean: "True" if successful."""
        if self.force_mode:
            return self._get_output('x')

        return ""

    def peak_tension(self) -> str:
        """Get the current peak tension value from the device.

        Returns:
            string: Current peak tension value."""
        if self.force_mode:
            return self._get_output('p')

        return ""

    def peak_compression(self) -> str:
        """Get the current peak compression value from the device.

        Returns:
            string: Current peak compression value."""
        if self.force_mode:
            return self._get_output('c')

        return ""

    def mini_output(self) -> str:
        """Get mini output from Nextech Gauge.

        Returns:
            str: Current value from the device. Mini format.
        """
        if self.force_mode:
            return self._get_output('L')

        return ""

    def short_output(self) -> str:
        """Get short output from Nextech Gauge.

        Returns:
            str: Current value from the device. Short format.
        """
        if self.force_mode:
            return self._get_output('v')

        return ""

    def long_output(self) -> str:
        """Get the long output from Nextech Gauge.

        Returns:
            str: Current value from the device. Long format.
        """
        if self.force_mode:
            return self._get_output('l')

        return ""

    def _get_output(self, command_key: str) -> str:
        """Helper method to get output from Nextech Gauge.

        Args:
            command_key (str): The command key to send to the device.

        Returns:
            str: Current value from the device.
        """
        try:
            if not self.usb_serial:
                print("Error: Device is not connected.")
                return ""

            self.usb_serial.write(self.device_command[command_key])
            time.sleep(0.1)
            s_output: str = ""
            while self.usb_serial.in_waiting:
                s_output += self.usb_serial.readline().decode("Ascii")
            return s_output
        except (OSError, serial.SerialException) as e:
            print(str(e))
            return ""

    def download(self, out_format="raw", gen_chart=False) -> str:
        """Download stored data from device memory.

        Args:
            out_format (str): The format of the output data. Values: ['raw', 'csv'].
            gen_chart (bool): Whether to generate a chart from the data.

        Returns:
            string: Stored tension and compression values."""
        try:
            if out_format not in ["raw", "csv"]:
                print(
                    "Error: Unsupported format. Use 'raw' (default) or 'csv'")
                return ""

            if not self.force_mode:
                return ""

            if not self.usb_serial:
                print("Error: Device is not connected.")
                return ""

            mem_data: str = ""
            self.usb_serial.write(self.device_command['d'])
            time.sleep(0.1)

            while self.usb_serial.in_waiting > 0:
                try:
                    raw_data = self.usb_serial.read_all()
                    if raw_data:
                        mem_data += raw_data.decode("Ascii")
                    time.sleep(0.1)
                except UnicodeDecodeError:
                    continue

            if gen_chart:
                self._save_mem_chart(mem_data)

            if out_format == "csv":
                csv_data = ""
                for line in mem_data.splitlines():
                    csv_data += line.strip().replace(' ', ',') + "\n"
                mem_data = csv_data

            return mem_data
        except (OSError, serial.SerialException) as e:
            print(str(e))
            return ""

    def _save_mem_chart(self, chart_data: str):
        """
        Generate a bar chart and save the image to a file.

        Args:
            chart_data (str): Memory data to create a chart from.
        """
        try:
            # Parse the chart data
            lines = chart_data.strip().split('\n')
            values = []
            labels = []

            for line in lines:
                parts = line.split(' ')
                if len(parts) > 3:
                    labels.append(parts[0].strip())
                    values.append(float(parts[3].strip()))

            # Create a bar chart
            x = np.arange(len(labels))
            plt.figure(figsize=(10, 6))
            plt.bar(x, values, color='blue')
            plt.xlabel('Labels')
            plt.ylabel('Values')
            plt.title('Memory Data Chart')
            plt.xticks(x, labels, rotation=45)

            # Save the chart to a file
            plt.tight_layout()
            imagefile = f"memory-data-{datetime.now().strftime('%Y%m%d-%H%M%S')}.png"
            plt.savefig(imagefile)
            plt.close()
        except (IndexError, PermissionError, OSError) as e:
            print(str(e))

    def read_torque_data(self, out_format="raw", gen_chart=False) -> str:
        """Read torque data from the device.

        Args:
            out_format (str): The format of the output data. Values: ['raw', 'csv'].
            gen_chart (bool): Whether to generate a chart from the data.

        Returns:
            string: Torque values."""
        try:
            if out_format not in ["raw", "csv"]:
                print(
                    "Error: Unsupported format. Use 'raw' (default) or 'csv'.")
                return ""

            if self.force_mode:
                return ""

            if not self.usb_serial:
                print("Error: Device is not connected.")
                return ""

            stop_event = threading.Event()
            torque_data = []

            def serial_reader():
                while not stop_event.is_set():
                    if self.usb_serial and self.usb_serial.in_waiting > 0:
                        line = self.usb_serial.read_all()
                        if line:
                            torque_data.append(line.decode("Ascii").strip())
                    else:
                        time.sleep(0.1)

            reader_thread = threading.Thread(target=serial_reader)
            reader_thread.start()

            input("Press Enter to stop reading torque data...\n")
            stop_event.set()
            reader_thread.join()

            tor_data = ""
            tmp_data = ""
            dts_pattern = re.compile(
                r"^\s*(\w+)\s*([+-])\s*([\d\.]+)\s*([^\d\s].+)$")
            dtt_pattern = re.compile(
                r"^\s*\(?\s*([\w ]+)\s*,\s*([+-])\s*,\s*([\d\.]+)\s*,\s*([^\d,]+)\s*,*,\s*([\d\/ :]+)\s*,?\s*([^\)]*)\)?$")
            for line in torque_data:
                if dts_pattern.match(tmp_data) or dtt_pattern.match(tmp_data):
                    tor_data += tmp_data + "\n"
                    tmp_data = ""
                if dts_pattern.match(line) or dtt_pattern.match(line):
                    tor_data += line + "\n"
                else:
                    tmp_data += line

            if gen_chart:
                self._save_tor_chart(tor_data)

            if out_format == "csv":
                csv_data = ""
                for line in tor_data.splitlines():
                    if '(' in line:
                        csv_data += line.strip().replace('(', '').replace(')', '') + "\n"
                    else:
                        csv_data += line.replace(' ', ',') + "\n"

                tor_data = csv_data

            return tor_data

        except (OSError, serial.SerialException) as e:
            print(str(e))
            return ""

    def _save_tor_chart(self, chart_data: str):
        """
        Generate a bar chart and save the image to a file.

        Args:
            chart_data (str): Memory data to create a chart from.
        """
        try:
            # Parse the chart data
            lines = chart_data.strip().split('\n')
            if len(lines) == 0:
                return

            values = []
            labels = []

            counter = 0
            v_idx = 3
            s_char = ' '
            if '(' in lines[0]:
                v_idx = 2
                s_char = ','

            for line in lines:
                parts = line.split(s_char)
                if len(parts) > 3:
                    labels.append(counter)
                    values.append(float(parts[v_idx].strip()))
                    counter += 1

            # Create a bar chart
            x = np.arange(len(labels))
            plt.figure(figsize=(10, 6))
            plt.bar(x, values, color='blue')
            plt.xlabel('Labels')
            plt.ylabel('Values')
            plt.title('Torque Data Chart')
            plt.xticks(x, labels, rotation=45)

            # Save the chart to a file
            plt.tight_layout()
            imagefile = f"torque-data-{datetime.now().strftime('%Y%m%d-%H%M%S')}.png"
            plt.savefig(imagefile)
            plt.close()
        except (IndexError, PermissionError, OSError) as e:
            print(str(e))

    def disconnect(self) -> bool:
        """Disconnect from the Nextech Gauge.

        Returns:
            boolean: "True" if garcefully disconnected."""
        status: bool = False
        try:
            if not self.usb_serial:
                print("Error: Device is not connected.")
                return False

            self.usb_serial.close()
            self.usb_serial = None
            status = True
            return status
        except (OSError, serial.SerialException) as e:
            print(str(e))
            return status

connect(force_mode=True, rate='high')

Connect to a force gauge or torque tester.

Parameters:

Name Type Description Default
force_mode bool

If True, connect in force mode; if False, connect in torque mode.

True
rate str

The baud rate ("high" or "low").

'high'

Returns: boolean: "True" if connection was successful.

Source code in nexgraphpy\nexgraph.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
def connect(self, force_mode=True, rate="high") -> bool:
    """Connect to a force gauge or torque tester.

    Args:
        force_mode (bool): If True, connect in force mode; if False, connect in torque mode.
        rate (str): The baud rate ("high" or "low").

    Returns: boolean: "True" if connection was successful.
    """
    status: bool = False
    if self.device_path:
        if rate == "high":
            baud_rate = 38400
        elif rate == "low":
            baud_rate = 9600
        else:
            print("Invalid rate specified. Use 'high' or 'low'.")
            return status

        if force_mode:
            self.force_mode = True
        else:
            self.force_mode = False

        self.usb_serial = serial.Serial(port=self.device_path,
                                        bytesize=8,
                                        baudrate=baud_rate,
                                        timeout=2,
                                        stopbits=serial.STOPBITS_ONE)
        try:
            if self.force_mode:
                self.usb_serial.write(self.device_command['!'])
                time.sleep(0.1)

                if self.usb_serial.in_waiting:
                    self.device_info += self.usb_serial.readline().decode("Ascii")
                    arr_info = self.device_info.split()

                    if arr_info and len(arr_info) > 2:
                        status = True

            else:
                status = True

            return status
        except (OSError, serial.SerialException, PermissionError) as e:
            print(str(e))
            return status
    else:
        print("The device connection path is empty.")
        return status

disconnect()

Disconnect from the Nextech Gauge.

Returns:

Name Type Description
boolean bool

"True" if garcefully disconnected.

Source code in nexgraphpy\nexgraph.py
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
def disconnect(self) -> bool:
    """Disconnect from the Nextech Gauge.

    Returns:
        boolean: "True" if garcefully disconnected."""
    status: bool = False
    try:
        if not self.usb_serial:
            print("Error: Device is not connected.")
            return False

        self.usb_serial.close()
        self.usb_serial = None
        status = True
        return status
    except (OSError, serial.SerialException) as e:
        print(str(e))
        return status

download(out_format='raw', gen_chart=False)

Download stored data from device memory.

Parameters:

Name Type Description Default
out_format str

The format of the output data. Values: ['raw', 'csv'].

'raw'
gen_chart bool

Whether to generate a chart from the data.

False

Returns:

Name Type Description
string str

Stored tension and compression values.

Source code in nexgraphpy\nexgraph.py
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
def download(self, out_format="raw", gen_chart=False) -> str:
    """Download stored data from device memory.

    Args:
        out_format (str): The format of the output data. Values: ['raw', 'csv'].
        gen_chart (bool): Whether to generate a chart from the data.

    Returns:
        string: Stored tension and compression values."""
    try:
        if out_format not in ["raw", "csv"]:
            print(
                "Error: Unsupported format. Use 'raw' (default) or 'csv'")
            return ""

        if not self.force_mode:
            return ""

        if not self.usb_serial:
            print("Error: Device is not connected.")
            return ""

        mem_data: str = ""
        self.usb_serial.write(self.device_command['d'])
        time.sleep(0.1)

        while self.usb_serial.in_waiting > 0:
            try:
                raw_data = self.usb_serial.read_all()
                if raw_data:
                    mem_data += raw_data.decode("Ascii")
                time.sleep(0.1)
            except UnicodeDecodeError:
                continue

        if gen_chart:
            self._save_mem_chart(mem_data)

        if out_format == "csv":
            csv_data = ""
            for line in mem_data.splitlines():
                csv_data += line.strip().replace(' ', ',') + "\n"
            mem_data = csv_data

        return mem_data
    except (OSError, serial.SerialException) as e:
        print(str(e))
        return ""

find()

Finds USB serial devices.

Returns: boolean: "True" if a device was found.

Source code in nexgraphpy\nexgraph.py
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def find(self) -> bool:
    """Finds USB serial devices.

    Returns: boolean: "True" if a device was found.
    """
    usb_ports = serial.tools.list_ports.comports(include_links=True)
    device_found: bool = False

    for port in usb_ports:
        if port.manufacturer is None:
            continue
        if platform.system() == 'Windows':
            if "COM" in port.device and "FTDI" in port.manufacturer:
                device_found = True
                self.device_path = port.device
        elif platform.system() == 'Darwin':
            if "usbserial" in port.device and "FTDI" in port.manufacturer:
                self.device_path = f"/dev/tty.{port.device.split('.')[1]}"
                device_found = True
        elif platform.system() == 'Linux':
            if "ttyUSB" in port.device and "FTDI" in port.manufacturer:
                self.device_path = port.device
                device_found = True

    return device_found

get_info()

Gets the device details.

Returns:

Name Type Description
string str

Model number, Current offset, Overload counter.

Source code in nexgraphpy\nexgraph.py
158
159
160
161
162
163
164
165
166
def get_info(self) -> str:
    """Gets the device details.        

    Returns:
        string: Model number, Current offset, Overload counter."""
    if self.force_mode:
        return self.device_info

    return ""

long_output()

Get the long output from Nextech Gauge.

Returns:

Name Type Description
str str

Current value from the device. Long format.

Source code in nexgraphpy\nexgraph.py
280
281
282
283
284
285
286
287
288
289
def long_output(self) -> str:
    """Get the long output from Nextech Gauge.

    Returns:
        str: Current value from the device. Long format.
    """
    if self.force_mode:
        return self._get_output('l')

    return ""

mini_output()

Get mini output from Nextech Gauge.

Returns:

Name Type Description
str str

Current value from the device. Mini format.

Source code in nexgraphpy\nexgraph.py
258
259
260
261
262
263
264
265
266
267
def mini_output(self) -> str:
    """Get mini output from Nextech Gauge.

    Returns:
        str: Current value from the device. Mini format.
    """
    if self.force_mode:
        return self._get_output('L')

    return ""

mode()

Send the change mode command to the device.

Returns:

Name Type Description
boolean bool

"True" if successful.

Source code in nexgraphpy\nexgraph.py
178
179
180
181
182
183
184
185
186
def mode(self) -> bool:
    """Send the change mode command to the device.

    Returns:
        boolean: "True" if successful."""
    if self.force_mode:
        return self._send_command('m')

    return False

peak_compression()

Get the current peak compression value from the device.

Returns:

Name Type Description
string str

Current peak compression value.

Source code in nexgraphpy\nexgraph.py
248
249
250
251
252
253
254
255
256
def peak_compression(self) -> str:
    """Get the current peak compression value from the device.

    Returns:
        string: Current peak compression value."""
    if self.force_mode:
        return self._get_output('c')

    return ""

peak_tension()

Get the current peak tension value from the device.

Returns:

Name Type Description
string str

Current peak tension value.

Source code in nexgraphpy\nexgraph.py
238
239
240
241
242
243
244
245
246
def peak_tension(self) -> str:
    """Get the current peak tension value from the device.

    Returns:
        string: Current peak tension value."""
    if self.force_mode:
        return self._get_output('p')

    return ""

print_value()

Send a print command to the device.

Returns:

Name Type Description
boolean str

"True" if successful.

Source code in nexgraphpy\nexgraph.py
228
229
230
231
232
233
234
235
236
def print_value(self) -> str:
    """Send a print command to the device.

    Returns:
        boolean: "True" if successful."""
    if self.force_mode:
        return self._get_output('x')

    return ""

read_torque_data(out_format='raw', gen_chart=False)

Read torque data from the device.

Parameters:

Name Type Description Default
out_format str

The format of the output data. Values: ['raw', 'csv'].

'raw'
gen_chart bool

Whether to generate a chart from the data.

False

Returns:

Name Type Description
string str

Torque values.

Source code in nexgraphpy\nexgraph.py
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
def read_torque_data(self, out_format="raw", gen_chart=False) -> str:
    """Read torque data from the device.

    Args:
        out_format (str): The format of the output data. Values: ['raw', 'csv'].
        gen_chart (bool): Whether to generate a chart from the data.

    Returns:
        string: Torque values."""
    try:
        if out_format not in ["raw", "csv"]:
            print(
                "Error: Unsupported format. Use 'raw' (default) or 'csv'.")
            return ""

        if self.force_mode:
            return ""

        if not self.usb_serial:
            print("Error: Device is not connected.")
            return ""

        stop_event = threading.Event()
        torque_data = []

        def serial_reader():
            while not stop_event.is_set():
                if self.usb_serial and self.usb_serial.in_waiting > 0:
                    line = self.usb_serial.read_all()
                    if line:
                        torque_data.append(line.decode("Ascii").strip())
                else:
                    time.sleep(0.1)

        reader_thread = threading.Thread(target=serial_reader)
        reader_thread.start()

        input("Press Enter to stop reading torque data...\n")
        stop_event.set()
        reader_thread.join()

        tor_data = ""
        tmp_data = ""
        dts_pattern = re.compile(
            r"^\s*(\w+)\s*([+-])\s*([\d\.]+)\s*([^\d\s].+)$")
        dtt_pattern = re.compile(
            r"^\s*\(?\s*([\w ]+)\s*,\s*([+-])\s*,\s*([\d\.]+)\s*,\s*([^\d,]+)\s*,*,\s*([\d\/ :]+)\s*,?\s*([^\)]*)\)?$")
        for line in torque_data:
            if dts_pattern.match(tmp_data) or dtt_pattern.match(tmp_data):
                tor_data += tmp_data + "\n"
                tmp_data = ""
            if dts_pattern.match(line) or dtt_pattern.match(line):
                tor_data += line + "\n"
            else:
                tmp_data += line

        if gen_chart:
            self._save_tor_chart(tor_data)

        if out_format == "csv":
            csv_data = ""
            for line in tor_data.splitlines():
                if '(' in line:
                    csv_data += line.strip().replace('(', '').replace(')', '') + "\n"
                else:
                    csv_data += line.replace(' ', ',') + "\n"

            tor_data = csv_data

        return tor_data

    except (OSError, serial.SerialException) as e:
        print(str(e))
        return ""

reset()

Send the reset values command to the device.

Returns:

Name Type Description
boolean bool

"True" if successful.

Source code in nexgraphpy\nexgraph.py
198
199
200
201
202
203
204
205
206
def reset(self) -> bool:
    """Send the reset values command to the device.

    Returns:
        boolean: "True" if successful."""
    if self.force_mode:
        return self._send_command('r')

    return False

short_output()

Get short output from Nextech Gauge.

Returns:

Name Type Description
str str

Current value from the device. Short format.

Source code in nexgraphpy\nexgraph.py
269
270
271
272
273
274
275
276
277
278
def short_output(self) -> str:
    """Get short output from Nextech Gauge.

    Returns:
        str: Current value from the device. Short format.
    """
    if self.force_mode:
        return self._get_output('v')

    return ""

unit()

Send the change unit command to the device.

Returns:

Name Type Description
boolean bool

"True" if successful.

Source code in nexgraphpy\nexgraph.py
188
189
190
191
192
193
194
195
196
def unit(self) -> bool:
    """Send the change unit command to the device.

    Returns:
        boolean: "True" if successful."""
    if self.force_mode:
        return self._send_command('u')

    return False

zero()

Send the zero/tare command to the device.

Returns:

Name Type Description
boolean bool

"True" if successful.

Source code in nexgraphpy\nexgraph.py
168
169
170
171
172
173
174
175
176
def zero(self) -> bool:
    """Send the zero/tare command to the device.

    Returns:
        boolean: "True" if successful."""
    if self.force_mode:
        return self._send_command('z')

    return False