insert your printer using usb cable
# lsusb
...
Bus 008 Device 002: ID 28e9:0289 GDMicroelectronics micro-printer
Bus 009 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
# dmesg
[ 70.532011] usb 8-1: new full-speed USB device number 2 using xhci_hcd
[ 70.688254] usb 8-1: New USB device found, idVendor=28e9, idProduct=0289, bcdDevice= 2.00
[ 70.688270] usb 8-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 70.688276] usb 8-1: Product: micro-printer
[ 70.688282] usb 8-1: Manufacturer: GEZHI
[ 70.688286] usb 8-1: SerialNumber: 000000000004
[ 70.730721] usblp 8-1:1.0: usblp1: USB Bidirectional printer dev 2 if 0 alt 0 proto 2 vid 0x28E9 pid 0x0289
[ 70.730783] usbcore: registered new interface driver usblp
use nl80211
# ls -al /dev/usb/
total 0
drwxr-xr-x 2 root root 80 May 31 14:31 .
drwxr-xr-x 20 root root 3840 May 31 14:31 ..
crw------- 1 root root 180, 0 May 31 14:30 hiddev0
crw-rw---- 1 root lp 180, 1 May 31 14:31 lp1
# ls -al /dev/usb/lp1
crw-rw---- 1 root lp 180, 1 May 31 14:31 /dev/usb/lp1
we found the device known as usb lp1 i.e. /dev/usb/lp1. test to print something.
# printf '\x1b\x40Hello World\n\n\n' > /dev/usb/lp1
your printer should print "Hello World"
add your account or linux user as lp group
# usermod -aG lp [username]
you need to sign out and re sign in, then try to print as [username]
$ printf '\x1b\x40Hello World\n\n\n' > /dev/usb/lp1
if everything work, now you can work as [username]. this is simple netbeans 30 - maven, to test your printer.
This is pom.xml for your project.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dedetok</groupId>
<artifactId>EnumComPort</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>21</maven.compiler.release>
<exec.mainClass>com.dedetok.enumcomport.EnumComPort</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.11.0</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.dedetok.enumcomport.EnumComPort</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This is the java code
package com.dedetok.enumcomport;
import com.fazecast.jSerialComm.SerialPort;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
*
* @author dedetok
*/
public class EnumComPort {
public static void main(String[] args) throws IOException {
System.out.println("Hello World!");
System.out.println(System.getProperty("user.name"));
Process p = Runtime.getRuntime().exec("id");
p.getInputStream().transferTo(System.out);
SerialPort[] ports = SerialPort.getCommPorts();
if (ports.length == 0) {
System.out.println("No serial ports detected.");
}
System.out.println("Available Serial Ports:");
System.out.println("------------------------");
for (SerialPort port : ports) {
System.out.println("System Port Name : " + port.getSystemPortName());
System.out.println("Descriptive Name: " + port.getDescriptivePortName());
System.out.println("Port Description: " + port.getPortDescription());
System.out.println("System Location : " + port.getSystemPortPath());
System.out.println("------------------------");
}
String devicePath = "/dev/usb/lp1";
// Open file in read-write mode for raw bidirectional access
RandomAccessFile deviceFile = new RandomAccessFile(devicePath, "rw");
FileDescriptor fd = deviceFile.getFD();
try (OutputStream out = new FileOutputStream(fd)) {
// ESC @ (initialize)
out.write(new byte[]{0x1B, 0x40});
out.write("Hello World\n".getBytes());
// Feed
out.write("\n\n".getBytes());
// Cut
out.write(new byte[]{0x1D, 0x56, 0x00});
out.flush();
out.close(); // NO NEED
} catch (FileNotFoundException ex) {
System.getLogger(EnumComPort.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
}
System.out.println("End");
}
}
In my test, you can run inside your netbeans, Run -> Build Project or Run -> Clean and Run Project. to run the jar, you go to project root and run like this
$ java -jar ./target/EnumComPort-1.0.jar
Hello World!
[username]
uid=1000([username]) gid=1000([username]) groups=1000([username]),7(lp),20(dialout),24(cdrom),25(floppy),29(audio),30(dip),33(www-data),44(video),46(plugdev),100(users),106(netdev),111(bluetooth),113(lpadmin),116(scanner),124(libvirt)
No serial ports detected.
Available Serial Ports:
------------------------
End
your print should print "Hello World". "No serial ports" means, you can use com.fazecast.jSerialComm library for lp printer.
Credit: https://www.freeformatter.com/html-escape.html for free html escape