I’m working on a React Native application that sends print jobs to an Intermec PC43d printer using TCP and ZPL commands. The issue I’m encountering is that the first print job is processed correctly, but any subsequent print jobs are only processed when I restart the application.
Here’s what happens:
First Print Job: The label prints correctly.
Subsequent Print Jobs: No labels are printed until the application is closed and restarted. Upon restart, the queued print jobs are processed.
Current Implementation:
import React, { useState } from 'react';
import { SafeAreaView, useColorScheme, Button, TextInput, Alert } from 'react-native';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import TcpSocket from 'react-native-tcp-socket';
const PrintTestScreen = () => {
const isDarkMode = useColorScheme() === 'dark';
const [state, setState] = useState({
text: `
^XA
^FX Top section with logo, name and address.
^CF0,60
^FO50,50^GB100,100,100^FS
^FO75,75^FR^GB100,100,100^FS
^FO93,93^GB40,40,40^FS
^FO220,50^FDTesting.^FS
^XZ
`,
});
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
};
const onPress = async () => {
const ip = '192.168.1.114';
const port = 9100;
console.log(`Attempting to create connection to ${ip}:${port}`);
let client;
try {
client = TcpSocket.createConnection({ port, host: ip }, () => {
console.log('Successfully created connection');
const zplCommand = state.text.trim();
const printCommand = zplCommand;
console.log(`Sending ZPL command: ${printCommand}`);
client.write(printCommand, (writeErr) => {
if (writeErr) {
console.error(`Error sending data: ${writeErr.message}`);
cleanup(client);
return;
}
console.log(`Sent print job to ${ip}:${port} at ${new Date().toISOString()}`);
// Ensure the command is fully sent before ending and destroying the connection
setTimeout(() => {
cleanup(client);
}, 3000); // Increased delay to ensure the command is fully processed
});
client.on('error', (error) => {
console.error(`Printing failed due to: ${error.message}`);
cleanup(client);
});
client.on('close', () => {
console.log('Connection closed');
});
});
} catch (error) {
console.error(`Error in sendToPrinter: ${error.message}`);
Alert.alert('Printing Error', error.message);
if (client) {
cleanup(client);
}
}
};
const cleanup = (client) => {
if (client) {
client.end(() => {
console.log('Connection ended');
client.destroy();
console.log('Connection explicitly destroyed after printing');
});
}
};
return (
<SafeAreaView style={backgroundStyle}>
<TextInput
value={state.text}
onChangeText={(text) => setState((prev) => ({ ...prev, text }))}
style={{ padding: 10, borderWidth: 1, margin: 10 }}
/>
<Button
title="Click here to Print label!"
color="#841584"
onPress={onPress}
/>
</SafeAreaView>
);
};
export default PrintTestScreen;
Here is the output from my console log. It appears that the connection is never explicitly destroyed, but I don’t believe this is the root cause of the issue.
LOG Attempting to create connection to 192.168.1.114:9100
LOG Successfully created connection
LOG Sending ZPL command: ^XA
^FX Top section with logo, name and address.
^CF0,60
^FO50,50^GB100,100,100^FS
^FO75,75^FR^GB100,100,100^FS
^FO93,93^GB40,40,40^FS
^FO220,50^FDTesting.^FS
^XZ
Any insights or suggestions on how to ensure the subsequent print jobs are processed without restarting the application would be greatly appreciated!
I implemented TCP printing in a React Native application using the react-native-tcp-socket library to send ZPL commands to an Intermec PC43d printer. The expected behavior was that each print job would be processed immediately upon sending the command.
Here is a summary of what I tried:
Connection Management: I ensured the TCP connection was properly closed after each print job by using client.end and client.destroy.
Delays: I added delays to ensure the commands were fully processed before closing the connection.
What actually happened?
The first print job prints correctly, but subsequent print jobs are only processed when the application is restarted. This means that while the commands are sent and acknowledged, the printer does not print them until the app is restarted.
What did you expect to happen?
I expected each print job to be processed immediately without the need to restart the application.