OpenSRX 0.1.0
C++ library for interfacing with Keyence SR series barcode readers
Loading...
Searching...
No Matches
Scanner.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iomanip>
4#include <sstream>
5#include <stdexcept>
6
7#include "OpenSRX/Code.hpp"
11#include "OpenSRX/Timestamp.hpp"
12
13namespace OpenSRX {
14
15class TestScanner;
16
23
37
46
55
61
66std::tuple<std::string, std::string> parseVersionInfo(const std::string& raw);
67
75 bool readOK = true;
76 bool verificationNG = false;
77 bool readError = false;
78 bool unstable = false;
79 bool capture = false;
80};
81
90class Scanner {
91 public:
102 ~Scanner() = default;
103
104 // Allow test fixture access to private methods
105 friend class TestScanner;
106
117
125
128
131
134
140
143
148 void readingRateTest(int bank);
149
152
157 void readTimeTest(int bank);
158
161
167 bool getInputTerminalState(int terminalNumber);
168
173 void turnOnOutputTerminal(int terminalNumber);
174
179 void turnOffOutputTerminal(int terminalNumber);
180
183
186
188 void reset();
189
192
198 std::string captureImage(int bank);
199
202
212 std::tuple<bool, TuningAdvice, TuningFailureReason> startTuning(int bank);
213
216
218 std::string getModel() const { return model; }
219
221 std::string getFirmwareVersion() const { return firmwareVersion; }
222
224 std::string getMacAddress() const { return macAddress; }
225
228
231
236 void setTime(const Timestamp& timestamp);
237
243
249
255
261
267 void copyBankConfiguration(int sourceBank, int targetBank);
268
271
274
277
282 void saveBackupSettings(int backupNumber);
283
288 void loadBackupSettings(int backupNumber);
289
292
295
296 // ─── Image server ───────────────────────────────────────────────────────
297
315 void startImageServer(const std::string& localIP, ImageSaveConfig saveConfig = {},
316 uint16_t port = 0, const std::string& username = "opensrx",
317 const std::string& password = "opensrx");
318
321
329
336 bool tryGetImage(Image& image);
337
341 std::deque<Image> getImages();
342
347 void setImageCallback(std::function<void(const Image&)> cb);
348
353
354 // ─── Bank parameters (WB/RB) ───────────────────────────────────────────
355
362 template <BankParam P>
363 auto getParam(int bank) -> ParamCppT<paramType(P)> {
364 std::ostringstream cmd;
365 cmd << "RB," << std::setw(2) << std::setfill('0') << bank << std::setw(3)
366 << std::setfill('0') << static_cast<int>(P);
367 std::string raw = checkResponse(comm.sendCommand(cmd.str()));
368 return parseParam<paramType(P)>(raw);
369 }
370
377 template <BankParam P>
378 void setParam(int bank, const ParamCppT<paramType(P)>& value) {
379 std::ostringstream cmd;
380 cmd << "WB," << std::setw(2) << std::setfill('0') << bank << std::setw(3)
381 << std::setfill('0') << static_cast<int>(P) << "," << formatParam<paramType(P)>(value);
382 checkResponse(comm.sendCommand(cmd.str()));
383 }
384
385 // ─── Tuning parameters (WC/RC) ─────────────────────────────────────────
386
392 template <TuningParam P>
394 std::string cmd = "RC," + std::to_string(static_cast<int>(P));
395 std::string raw = checkResponse(comm.sendCommand(cmd));
396 return parseParam<paramType(P)>(raw);
397 }
398
404 template <TuningParam P>
405 void setParam(const ParamCppT<paramType(P)>& value) {
406 std::string cmd =
407 "WC," + std::to_string(static_cast<int>(P)) + "," + formatParam<paramType(P)>(value);
408 checkResponse(comm.sendCommand(cmd));
409 }
410
411 // ─── Operation parameters (WP/RP) ──────────────────────────────────────
412
418 template <OperationParam P>
420 std::string cmd = "RP," + std::to_string(static_cast<int>(P));
421 std::string raw = checkResponse(comm.sendCommand(cmd));
422 return parseParam<paramType(P)>(raw);
423 }
424
430 template <OperationParam P>
431 void setParam(const ParamCppT<paramType(P)>& value) {
432 std::string cmd =
433 "WP," + std::to_string(static_cast<int>(P)) + "," + formatParam<paramType(P)>(value);
434 checkResponse(comm.sendCommand(cmd));
435 }
436
437 // ─── Communication parameters (WN/RN) ──────────────────────────────────
438
444 template <CommParam P>
446 std::string cmd = "RN," + std::to_string(static_cast<int>(P));
447 std::string raw = checkResponse(comm.sendCommand(cmd));
448 return parseParam<paramType(P)>(raw);
449 }
450
456 template <CommParam P>
457 void setParam(const ParamCppT<paramType(P)>& value) {
458 std::string cmd =
459 "WN," + std::to_string(static_cast<int>(P)) + "," + formatParam<paramType(P)>(value);
460 checkResponse(comm.sendCommand(cmd));
461 }
462
463 private:
473 Code parseReadResult(const std::string& raw);
474
486 std::string checkResponse(const std::string& response);
487
489 std::string model;
490 std::string firmwareVersion;
491 std::string macAddress;
492 std::unique_ptr<ImageServer> imageServer;
493
502 std::unique_ptr<SavedImageDests> savedImageDests;
503};
504
505} // namespace OpenSRX
Data returned from a barcode read operation.
Abstract interface for communicating with a scanner.
Definition ICommInterface.hpp:45
Embedded FTP server for receiving images from the scanner.
Definition ImageServer.hpp:25
Image waitForImage()
Block until the scanner delivers an image via FTP, decode it, and return the raw pixel data.
std::string model
Scanner model string.
Definition Scanner.hpp:489
void enablePointer()
Enable the laser aiming pointer.
std::string getModel() const
Get the scanner model string (e.g. "SR-X300").
Definition Scanner.hpp:218
void saveBackupSettings(int backupNumber)
Save settings to a backup slot.
auto getParam() -> ParamCppT< paramType(P)>
Read a tuning parameter.
Definition Scanner.hpp:393
Code startReading()
Start a blocking read operation.
void setImageCallback(std::function< void(const Image &)> cb)
Set a callback invoked on the watcher thread each time a new image is decoded.
BusyStatus getBusyStatus()
Query the scanner's busy status.
void loadSavedSettings()
Load settings from non-volatile memory.
void finishQuickSetupCodeReading()
Exit quick-setup code reading mode (RCOFF).
void quitTestMode()
Quit test mode (reading rate or read time).
std::string checkResponse(const std::string &response)
Parse and validate a scanner response string.
ErrorStatus getErrorStatus()
Query the scanner's error status.
void readTimeTest(int bank)
Start a read time test on a specific bank.
void clearPLCLinkError()
Clear the PLC link error flag.
void reset()
Reset the scanner.
void clearSendBuffer()
Clear the scanner's send buffer.
void stopReading()
Cancel an in-progress read operation.
CommandStatus getCommandStatus()
Query the scanner's command processing status.
std::deque< Image > getImages()
Return all images currently queued.
void adjustFocus()
Run auto-focus adjustment (blocks until complete).
void turnOnOutputTerminal(int terminalNumber)
Turn on a specific output terminal.
void readingRateTest(int bank)
Start a reading rate test on a specific bank.
std::tuple< bool, TuningAdvice, TuningFailureReason > startTuning(int bank)
Start tuning on a specific bank (blocks until complete).
Code startReading(int bank)
Start a blocking read operation on a specific bank.
std::string captureImage(int bank)
Capture an image and return the remote file path.
void disablePointer()
Disable the laser aiming pointer.
void turnOffAllOutputTerminals()
Turn off all output terminals.
void setTime(const Timestamp &timestamp)
Set the scanner's internal clock.
bool getInputTerminalState(int terminalNumber)
Get the state of an input terminal.
std::string getMacAddress() const
Get the scanner MAC address string.
Definition Scanner.hpp:224
void setParam(int bank, const ParamCppT< paramType(P)> &value)
Write a bank parameter.
Definition Scanner.hpp:378
std::unique_ptr< SavedImageDests > savedImageDests
Definition Scanner.hpp:502
Scanner(ICommInterface &comm)
Connect to a scanner on the given communication interface.
void readTimeTest()
Start a read time test on all banks.
std::string firmwareVersion
Scanner firmware version string.
Definition Scanner.hpp:490
ICommInterface & comm
Communication interface to the scanner.
Definition Scanner.hpp:488
ImageServer * getImageServer()
Access the underlying ImageServer (nullptr if not started).
Definition Scanner.hpp:352
void copyBankConfiguration(int sourceBank, int targetBank)
Copy configuration from one bank to another.
void stopImageServer()
void setParam(const ParamCppT< paramType(P)> &value)
Write a tuning parameter.
Definition Scanner.hpp:405
auto getParam(int bank) -> ParamCppT< paramType(P)>
Read a bank parameter.
Definition Scanner.hpp:363
void resetToFactorySettings()
Reset all settings to factory defaults.
void turnOffOutputTerminal(int terminalNumber)
Turn off a specific output terminal.
Code parseReadResult(const std::string &raw)
Parse a raw read-result string into a Code struct.
bool tryGetImage(Image &image)
Return the next image if one is available, without blocking.
std::unique_ptr< ImageServer > imageServer
Embedded FTP image server (if started).
Definition Scanner.hpp:492
friend class TestScanner
Definition Scanner.hpp:105
void startImageServer(const std::string &localIP, ImageSaveConfig saveConfig={}, uint16_t port=0, const std::string &username="opensrx", const std::string &password="opensrx")
Start an embedded FTP server and configure the scanner to send images to it.
Timestamp getTime()
Read the scanner's internal clock.
std::string checkQuickSetupCodeResult()
Check the result of a quick-setup code reading.
~Scanner()=default
void startQuickSetupCodeReading()
Enter quick-setup code reading mode (RCON).
std::string macAddress
Scanner MAC address string.
Definition Scanner.hpp:491
std::string getFirmwareVersion() const
Get the scanner firmware version string.
Definition Scanner.hpp:221
void readingRateTest()
Start a reading rate test on all banks.
void saveSettings()
Save all current settings to non-volatile memory.
void stopTuning()
Cancel an in-progress tuning operation.
void turnOnAllOutputTerminals()
Turn on all output terminals.
void clearFTPCommsError()
Clear the FTP communication error flag.
void loadBackupSettings(int backupNumber)
Load settings from a backup slot.
Simple date/time representation used by the scanner's clock.
Definition Timestamp.hpp:11
Definition AsioInterface.hpp:5
ParamCppT< PT > parseParam(const std::string &raw)
Parse a raw response string into the C++ type for a given ParamType.
TuningFailureReason
Reason for a tuning failure.
Definition Scanner.hpp:57
@ UNSTABLE_READING
Reading is unstable.
Definition Scanner.hpp:59
@ CODE_DETECTION_IMPOSSIBLE
Code detection is impossible.
Definition Scanner.hpp:58
typename ParamCppType< PT >::type ParamCppT
Shorthand: the C++ type for a given ParamType.
Definition ParamTraits.hpp:306
std::string formatParam(const ParamCppT< PT > &value)
Format a C++ value into the string to send as a parameter value.
TuningAdvice
Advice returned by tuning on how to improve results.
Definition Scanner.hpp:48
@ BRIGHTNESS_INSUFFICIENT
Brightness is insufficient.
Definition Scanner.hpp:53
@ USE_AN_IMAGE_FILTER
Try using an image filter.
Definition Scanner.hpp:50
@ CONSIDER_INSTALLATION_LIGHTING_PRINTING_CONDITIONS
Check installation/lighting/printing.
Definition Scanner.hpp:51
BusyStatus
Scanner busy status.
Definition Scanner.hpp:39
@ UPDATE_PROCESSING
Update processing in progress.
Definition Scanner.hpp:42
@ AUTO_FOCUSING
Auto-focus in progress.
Definition Scanner.hpp:44
@ IDLE
Not busy.
Definition Scanner.hpp:40
@ TRG_BUSY
Trigger processing in progress.
Definition Scanner.hpp:41
@ SAVING_FILE
Saving a file.
Definition Scanner.hpp:43
std::tuple< std::string, std::string > parseVersionInfo(const std::string &raw)
Parse a raw version info string ("MODEL,FIRMWARE") into components.
@ NONE
Definition ParamValues.hpp:151
CommandStatus
Scanner command processing status.
Definition Scanner.hpp:18
@ NO_PROCESSING
No command is being processed.
Definition Scanner.hpp:19
@ WAIT_FOR_SETTING
Waiting for a setting to be applied.
Definition Scanner.hpp:20
@ UPDATING
A setting update is in progress.
Definition Scanner.hpp:21
constexpr ParamType paramType(BankParam p)
Definition ParamTypeMap.hpp:91
ImageSavingDestination
Image saving destination (commands 500–504).
Definition ParamValues.hpp:211
ErrorStatus
Scanner error status codes.
Definition Scanner.hpp:25
@ PROFINET_ERROR
PROFINET error.
Definition Scanner.hpp:33
@ SET_VALUE_ERROR
Set value error.
Definition Scanner.hpp:29
@ PLC_LINK_ERROR
PLC link error.
Definition Scanner.hpp:32
@ DUPLICATE_IP_ERROR
Duplicate IP address detected.
Definition Scanner.hpp:30
@ UPDATE_ERROR
Update error.
Definition Scanner.hpp:28
@ LUA_SCRIPT_ERROR
Lua script error.
Definition Scanner.hpp:34
@ CONNECTION_ERROR
Host connection error.
Definition Scanner.hpp:35
@ NO_ERROR
No error.
Definition Scanner.hpp:26
@ BUFF_OVERFLOW_ERROR
Buffer overflow error.
Definition Scanner.hpp:31
@ SYSTEM_ERROR
System error.
Definition Scanner.hpp:27
Result of a barcode read operation.
Definition Code.hpp:42
Configuration for which image types to save via FTP.
Definition Scanner.hpp:74
bool capture
SAVE_DEST_CAPTURE (504)
Definition Scanner.hpp:79
bool verificationNG
SAVE_DEST_VERIFICATION_NG (501)
Definition Scanner.hpp:76
bool readError
SAVE_DEST_READ_ERROR (502)
Definition Scanner.hpp:77
bool readOK
SAVE_DEST_READ_OK (500)
Definition Scanner.hpp:75
bool unstable
SAVE_DEST_UNSTABLE (503)
Definition Scanner.hpp:78
Raw decoded image data.
Definition Image.hpp:18
Previous SAVE_DEST values saved by startImageServer, restored on stop.
Definition Scanner.hpp:495
ImageSavingDestination verificationNG
Definition Scanner.hpp:497
ImageSavingDestination readOK
Definition Scanner.hpp:496
ImageSavingDestination unstable
Definition Scanner.hpp:499
ImageSavingDestination capture
Definition Scanner.hpp:500
ImageSavingDestination readError
Definition Scanner.hpp:498