-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcomm.c
More file actions
77 lines (59 loc) · 1.76 KB
/
Copy pathcomm.c
File metadata and controls
77 lines (59 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/************************************************************************
comm.c
Modul per a gestionar l'entrada/sortida sèrie a travers de linux
Smart Software 1993/99 (C)
Tomeu Capó i Capó 1999 (C)
Last revision: 28/09/1999
************************************************************************/
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include "comm.h"
#include "propies.h"
p_port defineix_port(char *disp,speed_t velocitat,int tdades)
{
p_port p;
if((p=(p_port) malloc(sizeof(s_port)+1))==NULL) {
printf("error al reservar la memoria\n");
return 0;
}
p->disp = duplica_cad(disp);
bzero(&p->cfg_nova,sizeof(p->cfg_nova));
p->cfg_nova.c_cflag = velocitat | CRTSCTS | tdades | CREAD;
p->cfg_nova.c_iflag = ICRNL; // Converteix el caracter CR->NULL
p->cfg_nova.c_oflag = 0; // Sortida tipus RAW
p->cfg_nova.c_lflag = ICANON;
}
void obre_port(p_port p)
{
p->descriptor = open(p->disp, O_RDWR | O_NOCTTY);
if (p->descriptor<0)
perror(p->disp);
else {
tcgetattr(p->descriptor,&p->cfg_vella);
tcflush(p->descriptor,TCIFLUSH);
tcsetattr(p->descriptor,TCSANOW,&p->cfg_nova);
}
}
void tanca_port(p_port p)
{
tcsetattr(p->descriptor,TCSANOW,&p->cfg_vella);
close(p->descriptor);
}
void envia_comanda(p_port p,char *comanda)
{
// bcopy("@00MS5E*\r\0",&p->out_buffer,10);
bcopy(comanda,&p->out_buffer,strlen(comanda));
if(fork()==0)
write(p->descriptor,p->out_buffer,strlen(comanda));
}
int rebre_comanda(p_port p)
{
int res;
if(fork()==0)
res = read(p->descriptor,p->in_buffer,255);
}
char *in_buffer(p_port p)
{
return p->in_buffer;
}