commit cc7b8e863e7198a9781787f4d00889d4618e4f2a Author: Rane Date: Tue Jul 29 15:08:26 2025 +0700 (init): Print const label diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1928440 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "godex500.h": "c" + } +} \ No newline at end of file diff --git a/GODEX500/Godex500.c b/GODEX500/Godex500.c new file mode 100644 index 0000000..d9504bb --- /dev/null +++ b/GODEX500/Godex500.c @@ -0,0 +1,72 @@ +#include "Godex500.h" +const int OFFSET = 0; +int GODEX500_setup_serial(const char* device) { + int serial_port = open(device, O_RDWR); + if (serial_port < 0) { + perror("Ошибка открытия последовательного порта"); + return -1; + } + + struct termios tty; + memset(&tty, 0, sizeof tty); + + if (tcgetattr(serial_port, &tty) != 0) { + perror("Ошибка получения атрибутов TTY"); + close(serial_port); + return -1; + } + + cfsetospeed(&tty, B9600); + cfsetispeed(&tty, B9600); + + tty.c_cflag &= ~PARENB; + tty.c_cflag &= ~CSTOPB; + tty.c_cflag &= ~CSIZE; + tty.c_cflag |= CS8; + + tty.c_cflag |= CREAD | CLOCAL; + + if (tcsetattr(serial_port, TCSANOW, &tty) != 0) { + perror("Ошибка установки атрибутов TTY"); + close(serial_port); + return -1; + } + + return serial_port; +} + +void GODEX500_send_to_printer(int serial_port, const char* data) { + write(serial_port, data, strlen(data)); + tcdrain(serial_port); +} + +void GODEX500_print_label(int serial_port, const char* id, const char* model) { + char buffer[1024]; + char datamatrix_data[200]; + + snprintf(datamatrix_data, sizeof(datamatrix_data), "%s", id); + int datamatrix_length = strlen(datamatrix_data); + + snprintf(buffer, sizeof(buffer), + "^Q10,2\n" + "^W25\n" + "^H14\n" + "^P1\n" + "^S2\n" + "^AT\n" + "^C1\n" + "^R0\n~Q+0\n" + "^O0\n^D0\n^E30\n~R255\n" + "^L\n" + "Dy2-me-dd\nTh:m:s\n" + "AB,%d,10,1,1,0,0E,%s\n" + "AB,%d,45,1,1,0,0E,%s\n" + "XRB%d,14,4,0,%d\n" + "%s\n" + "E\n", + 5 + OFFSET, id, + 40 + OFFSET, model, + 130 + OFFSET, datamatrix_length, datamatrix_data); + + GODEX500_send_to_printer(serial_port, buffer); +} \ No newline at end of file diff --git a/GODEX500/Godex500.h b/GODEX500/Godex500.h new file mode 100644 index 0000000..bf6ab9e --- /dev/null +++ b/GODEX500/Godex500.h @@ -0,0 +1,16 @@ +#ifndef GODEX500_H +#define GODEX500_H + +#include +#include +#include +#include +#include +#include + +// Прототипы функций +int GODEX500_setup_serial(const char* device); +void GODEX500_send_to_printer(int serial_port, const char* data); +void GODEX500_print_label(int serial_port, const char* id, const char* model); + +#endif /* GODEX500_H */ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1790fb3 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +# Переменные +CC = gcc +CFLAGS = -Wall -g +LDFLAGS = +TARGET = program + +SRCS = \ +main.c \ +GODEX500/Godex500.c \ + +OBJS = $(SRCS:.c=.o) + +# Цель по умолчанию +all: $(TARGET) clean launch + +# Правило для создания исполняемого файла +$(TARGET): $(OBJS) + $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS) + +# Правило для компиляции .c файлов в .o файлы +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +# Чистка проекта от скомпилированных файлов +clean: + rm -f $(OBJS) + +launch: + ./$(TARGET) diff --git a/main.c b/main.c new file mode 100644 index 0000000..01c4c17 --- /dev/null +++ b/main.c @@ -0,0 +1,10 @@ +#include "./GODEX500/Godex500.h" +#define SerialDevice "/dev/ttyUSB0" +int main() { + const char* id = "2E000504"; + const char* model = "LS3"; + int serial_port = GODEX500_setup_serial(SerialDevice); + GODEX500_print_label(serial_port, id, model); + close(serial_port); + return 0; +} \ No newline at end of file