(init): Print const label

main
Evgeniy Mashtarо́v 2025-07-29 15:08:26 +07:00
commit cc7b8e863e
5 changed files with 132 additions and 0 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"godex500.h": "c"
}
}

72
GODEX500/Godex500.c Normal file
View File

@ -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);
}

16
GODEX500/Godex500.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef GODEX500_H
#define GODEX500_H
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
// Прототипы функций
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 */

29
Makefile Normal file
View File

@ -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)

10
main.c Normal file
View File

@ -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;
}