ReadPrinter does not work any suggestion or example code please share #824
-
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
Is there any example code, please share |
Beta Was this translation helpful? Give feedback.
-
Can you also share the code you use to call the |
Beta Was this translation helpful? Give feedback.
-
I use this code - https://github.com/dart-windows/win32/blob/main/example/printer_raw.dart Future<String> readFromPrinterAsync(
String printerName, int bufferSize) async {
final completer = Completer<String>();
// Start a new isolate to perform the blocking operation
final phPrinter = calloc<HANDLE>();
await Isolate.spawn((_) {
try {
// Open the printer
final pPrinterName = printerName.toNativeUtf16();
final success = OpenPrinter(pPrinterName, phPrinter, nullptr);
if (success == 0) {
completer.completeError(
WindowsException(HRESULT_FROM_WIN32(GetLastError())));
return;
}
final pReadBuf = calloc<Uint8>(bufferSize);
final pBytesRead = calloc<Uint32>();
const command = '<S92>'; // Command to read print status
final res = _printRawData(phPrinter, command);
if (!res) {
throw Exception('Failed to send command to read print status');
}
try {
// Read from the printer
final success =
ReadPrinter(phPrinter.value, pReadBuf, bufferSize, pBytesRead);
if (success == 0) {
completer.completeError(
WindowsException(HRESULT_FROM_WIN32(GetLastError())));
return;
}
// Convert the read buffer to a Dart string
final readData = String.fromCharCodes(
pReadBuf.cast<Uint8>().asTypedList(pBytesRead.value));
completer.complete(readData);
} finally {
calloc.free(pReadBuf);
calloc.free(pBytesRead);
}
} finally {
// Close the printer handle
if (phPrinter.value != NULL) {
ClosePrinter(phPrinter.value);
}
calloc.free(phPrinter);
}
}, null);
return completer.future;
}
|
Beta Was this translation helpful? Give feedback.
-
The documentation for the ReadPrinter function says:
This implies that you must provide both the name of the printer and a job number when calling the I also couldn't find an example online that uses |
Beta Was this translation helpful? Give feedback.
-
Hi @halildurmus |
Beta Was this translation helpful? Give feedback.
-
The same question is posted here - https://stackoverflow.com/q/78080791/5766072 |
Beta Was this translation helpful? Give feedback.
-
I'm sorry, I can't help you as I don't have access to a printer that supports bi-directional communication. I'm turning this into a discussion as I don't believe there is a problem with |
Beta Was this translation helpful? Give feedback.
-
The solution here, |
Beta Was this translation helpful? Give feedback.
The solution here,
To properly read from the printer, pass "$portname, Port" as the parameter in the ReadPrinter function. This should resolve the issue and allow you to successfully read from the printer.