83 lines
1.8 KiB
C
83 lines
1.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <fcntl.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
#include "Printer_sys.h"
|
|
|
|
pthread_t Thread_Bluetooth;
|
|
int Bluetooth_fd;
|
|
|
|
|
|
|
|
static void *Uart_Receive_task(void *arg)
|
|
{
|
|
char rx_buffer[256];
|
|
printf("Uart_Receive_task create ok!\n");
|
|
while (1)
|
|
{
|
|
int bytes_read = read(Bluetooth_fd, rx_buffer, sizeof(rx_buffer));
|
|
if (bytes_read > 0) {
|
|
rx_buffer[bytes_read] = '\0';
|
|
printf("\rrx_buffer: \n %s ", rx_buffer);
|
|
} else {
|
|
printf("No data received.\n");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
int Bluetooth_Printer(const char* com)
|
|
{
|
|
char serial_port[15];
|
|
|
|
int res;
|
|
|
|
Bluetooth_fd = open(com, O_RDWR | O_NOCTTY);
|
|
if (Bluetooth_fd == -1) {
|
|
perror("Failed to open serial port");
|
|
return 1;
|
|
}
|
|
|
|
struct termios tty;
|
|
memset(&tty, 0, sizeof(tty));
|
|
|
|
if (tcgetattr(Bluetooth_fd, &tty) != 0) {
|
|
perror("Error from tcgetattr");
|
|
return 1;
|
|
}
|
|
|
|
cfsetospeed(&tty, B115200);
|
|
cfsetispeed(&tty, B115200);
|
|
|
|
tty.c_cflag &= ~PARENB;
|
|
tty.c_cflag &= ~CSTOPB;
|
|
tty.c_cflag &= ~CSIZE;
|
|
tty.c_cflag |= CS8;
|
|
|
|
if (tcsetattr(Bluetooth_fd, TCSANOW, &tty) != 0) {
|
|
perror("Error from tcsetattr");
|
|
return 1;
|
|
}
|
|
|
|
// char tx_buffer[] = "AT+COD?\n";
|
|
// ssize_t bytes_written = write(Bluetooth_fd, tx_buffer, sizeof(tx_buffer));
|
|
// if (bytes_written < 0) {
|
|
// perror("Error writing to serial port");
|
|
// close(Bluetooth_fd);
|
|
// return 1;
|
|
// }
|
|
// printf("\rtx_buffer: \n %s ", tx_buffer);
|
|
|
|
res = pthread_create(&Thread_Bluetooth, NULL, Uart_Receive_task, (void*)(unsigned long long)(520));
|
|
if(res != 0)
|
|
{
|
|
printf("create thread fail\n");
|
|
exit(res);
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
} |