๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[์๋์ฐจ ๋ทํธ์ LCDํ์ผ๋ก ์ํฉ ์ ๋ฌํ๊ธฐ 5] client ์์ ๋ฐ์ message ๋ฅผ serial ํต์ ํตํด arduino ๋ก ์ ์ก by.termios ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/Arduino
[์๋์ฐจ ๋ทํธ์ LCDํ์ผ๋ก ์ํฉ ์ ๋ฌํ๊ธฐ 5] client ์์ ๋ฐ์ message ๋ฅผ serial ํต์ ํตํด arduino ๋ก ์ ์ก by.termios
์ง์ง์ํ์นด 2023. 11. 10. 12:12728x90
๋ฐ์ํ
โญ client ์์ ๋ฐ์ message ๋ฅผ serial ํต์ ํตํด arduino ๋ก ์ ์ก
โญ server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 16
void error_handling(char *message);
int main(int argc, char *argv[])
{
// ์๋ฒ, ํด๋ผ์ด์ธํธ ์์ผ ํ์ผ๋์คํฌ๋ฆฝํฐ ๋ณ์ ์ ์ธ
int serv_sock, clnt_sock;
char message[BUF_SIZE];
int str_len, i;
// sockaddr_in ๊ตฌ์กฐ์ฒด ๋ณ์ ์ ์ธ
struct sockaddr_in serv_adr, clnt_adr;
socklen_t clnt_adr_sz;
if(argc != 2)
{
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
// ----------- 1. Create socket object ------------------
// socket() : socket ์์ฑ & socket discriptor
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socket() error");
// ----------- 2. Bind the socket file ------------------
// serv_sock์ bind ๋ก ์ฃผ์ ๋ฃ๊ธฐ ์ํ ๋ฐ์์
memset(&serv_adr, 0, sizeof(serv_adr));
// Prepare the address
serv_adr.sin_family = AF_INET; // type : IPv4
serv_adr.sin_addr.s_addr = htonl(INADDR_ANY); // ip์ฃผ์
serv_adr.sin_port = htons(atoi(argv[1])); // ํฌํธ๋ฒํธ
// bind()๋ก ์๋ฒ ์์ผ์ ์ฃผ์์ ๋ณด ํ ๋น
if(bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1)
error_handling("bind() error");
// ----------- 3. Prepare backlog ------------------
// listen()์ผ๋ก ์๋ฒ์์ผ์ผ๋ก ์ค๋ ํด๋ผ์ด์ธํธ ์์ฒญ ๋๊ธฐ
if(listen(serv_sock, 5) == -1)
error_handling("listen() error");
clnt_adr_sz = sizeof(clnt_adr);
for(i = 0; i < 5; i++)
{
printf("Listen....\n");
// ----------- 4. Start accepting clients ---------
// ํด๋ผ์ด์ธํธ ์ ์ ์์ฒญ ๋๊ธฐ ๋ฐ ์๋ฝ, ํด๋ผ์ด์ธํธ์์ ํต์ ์ ์ํ ์ socket ์์ฑ
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_sz);
if(clnt_sock == -1)
error_handling("accept() error");
else
printf("Connected client %d \n", i + 1);
// ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ์ ์ก๋ ์๋ฃ ์์
// read(int fd, void *buff, size_t nbytes)
while((str_len = read(clnt_sock, message, BUF_SIZE)) != 0)
// ํด๋ผ์ด์ธํธ๋ก ์๋ฃ ์ก์
// write(int fd, const void *buf, size_t nbytes);
write(clnt_sock, message, str_len);
// ํต์ ์ ์๋ฃํ๋ฉด socket ์ ์๋ฉธ
close(clnt_sock);
}
// ํต์ ์ ์๋ฃํ๋ฉด socket ์ ์๋ฉธ
close(serv_sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
โญ client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <termios.h>
#define BUF_SIZE 16
void error_handling(char *message);
typedef struct
{
// ํ์ผ ๋์คํฌ๋ฆฝํฐ : ์ด๋ ํ ํต์ ํฌํธ๋ฅผ ์ด์์ ๋ ์ปดํจํฐ๊ฐ ์ฐพ์๊ฐ๊ธฐ ์ฝ๊ฒ ์ ์๋ก ์ซ์ ๋งค๊น
int fd;
} SerialPort;
// device : ์ด๊ณ ์ถ์ ์๋ฆฌ์ผ ํฌํธ์ ์ด๋ฆ
int serial_open(SerialPort *port, const char *device)
{
// open : ๋ฌธ์์ด๋ก ์ ์ธ๋ ๊ฒฝ๋ก(device)๋ฅผ ๋ฐ์์ ํ์ผ ๋์คํฌ๋ฆฝํฐ(fd)๋ก ๋ฐํํ๋ ํจ์
port->fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (port->fd == -1)
{
perror("Unable to open port");
return -1;
}
// ์๋ฆฌ์ผ ํต์ ํ๊ฒฝ์ ์ค์ ํ๊ธฐ ์ํด termios ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ธ
struct termios options;
memset(&options, 0, sizeof(options));
// ์๋ฆฌ์ผ ํฌํธ ์ด๊ธฐํ
tcflush(port->fd, TCIFLUSH);
// Terminer Control ๋ชจ๋ ์ค์ ์ ์ํด์ Get
tcgetattr(port->fd, &options);
cfsetispeed(&options, B9600); // Set baud rate to 9600 (adjust as needed)
cfsetospeed(&options, B9600);
options.c_cflag |= CS8; // 8 data bits -> ๋ฌธ์ ํฌ๊ธฐ ๋ง์คํฌ
options.c_cflag &= ~PARENB; // No parity -> PARENB : ์ถ๋ ฅ ์ ํจ๋ฆฌํฐ ์์ฑ์ ํ์ฑํํ๊ณ ์
๋ ฅ
options.c_cflag &= ~CSTOPB; // One stop bit -> CSTOPB : Stop bit ์ 1๊ฐ
// options.c_cflag &= ~CSIZE; // Mask the character size bits
// Serial Port์ ์ ์ค์ Set
// ์๋ฆฌ์ผ ํฌํธ์ ์ค์ ์
๋ ฅ (TCSANOW : ์ฆ์ ์์ฑ์ ๋ณ๊ฒฝ์์ผ๋ผ)
tcsetattr(port->fd, TCSANOW, &options);
return 0;
}
void serial_close(SerialPort *port)
{
close(port->fd);
port->fd = -1;
}
int serial_read(SerialPort *port, char *buffer, size_t size)
{
// Serial Port๋ก๋ถํฐ ๋ฐ์ดํฐ ์์
// read์ ์ํด์ ์ค์ ๋ก ์ฝํ์ง ๋ฌธ์์ ๊ฐ์๋ฅผ ๊ฐ๊ฒ ๋จ
return read(port->fd, buffer, size);
}
ssize_t serial_write(SerialPort *port, char *buffer, size_t size)
{
// Serial Port์ผ๋ก ๋ฐ์ดํฐ ์ก์
return write(port->fd, buffer, size);
}
int main(int argc, char *argv[])
{
int sock;
char message[BUF_SIZE] = {0, };
int str_len;
// sockaddr_in ๊ตฌ์กฐ์ฒด ๋ณ์ ์ ์ธ
struct sockaddr_in serv_adr;
if (argc != 3)
{
printf("Usage : %s <IP> <port> \n", argv[0]);
exit(1);
}
// ----------- 1. Create socket object ------------------
// socket() : socket ์์ฑ & socket discriptor
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1)
error_handling("socket() error");
// serial port
SerialPort port;
if (serial_open(&port, "/dev/ttyACM0") == -1)
{
return -1;
}
// ------------ 2. Connect to server-- ------------------
// serv_sock์ bind ๋ก ์ฃผ์ ๋ฃ๊ธฐ ์ํ ๋ฐ์์
memset(&serv_adr, 0, sizeof(serv_adr));
// Prepare the address
serv_adr.sin_family = AF_INET; // type : IPv4
serv_adr.sin_addr.s_addr = inet_addr(argv[1]); // ip์ฃผ์
serv_adr.sin_port = htons(atoi(argv[2])); // ํฌํธ๋ฒํธ
// ๋์ ์๋ฒ ์์ผ์ ์ฃผ์ ํ ๋น
// ์ฃผ์ ์ ๋ณด์ ์๋ฒ์ ์ฃผ์์ ํฌํธ ๋ฒํธ๋ฅผ ์ง์ ํ๊ณ ์๋ฒ์ ์ฐ๊ฒฐ ์๋
if (connect(sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr)) == -1)
error_handling("connect() error!");
else
puts("Connect........");
while (1)
{
fputs("Input message(Q to quit) : ", stdout);
fgets(message, BUF_SIZE, stdin);
if (!strcmp(message, "q\n") || !strcmp(message, "Q\n"))
break;
// ํด๋ผ์ด์ธํธ๋ก ์๋ฃ๋ฅผ ์ก์ ํ๋ค
// write(int fd, const void *buf, size_t nbytes);
write(sock, message, strlen(message));
// ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ์ ์ก๋ ์๋ฃ ์์ ํ๋ค
// read(int fd, void *buff, size_t nbytes)
str_len = read(sock, message, BUF_SIZE - 1);
message[str_len] = 0;
printf("Message form server : %s", message);
// serial port ๋ก message ์ ๋ฌ
serial_write(&port, message, sizeof(message));
}
// ํต์ ํฌํธ๋ฅผ ๋ซ์ ์ฌ์ฉ์ ์ค์ง
serial_close(&port);
close(sock);
return 0;
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}
728x90
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > Arduino' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments