我必须制作一个程序,从某个服务器获取串口命令,我必须将此命令转发给三个设备。其中一个设备将发送答案,其他所有设备都不会发送任何内容。问题是,如何检查新邮件是否来自服务器?据我所知,如果我将不断采取零消息,我将加载CPU?或不?
这是代码,它重载了注释,但整个系统很容易。并且read()函数不再起作用,因为我试图分别读取每个字符,直到\ n
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#define HE_BAUDRATE B19200
#define HE_BUF 1024
#define ROO_TEMP ROO_TEMP
#define ROO_DESI ROO_DESI
#define ROO_DESI1 "09 SET ROO_DESI D 200"
#define ROO_DESI2 "09 SET SYS_DATE 17:52 18.04.14"
struct termios *he_tio_save;
struct termios tio;
int open_serial(const char *dev);
void close_serial(int fd);
int send_msg(int fd, unsigned char *data, int len);
int read_msg(int fd, unsigned char *buf, int len);
const char *device0 = "/dev/ttyS0";
const char *device1 = "/dev/ttyS1";
const char *device2 = "/dev/ttyS2";
const char *device3 = "/dev/ttyS3";
int main() {
int fd0,fd1,fd2,fd3, ret = 0, ret1 = 0, ret2 = 0;
char data[50], data_back[50];
char command;
unsigned char buf[HE_BUF], buf1[HE_BUF], buf2[HE_BUF];
//Serielle Schnittschtellen oeffnen
fd0 = open_serial(device0);
fd1 = open_serial(device1);
fd2 = open_serial(device2);
if (fd0 < 0) {
printf("Unable to open device %s\n", device1);
}
printf("Device0 opened\n");
while (1){
memset(buf, 0, HE_BUF);
// Lesen vom server ANFANG
ret = read_msg(fd0, buf, HE_BUF);
if (ret == -1) {
printf("communication problem\n");
ret = -1;
}
// Lesen vom server ANFANG
strcpy(data,buf);
// Senden an device1 ANFANG
//fd1 = open_serial(device1);
printf("1datatosend: %s\n", data);
ret = send_msg(fd1, data, strlen(data));
if (ret < 0) {
ret = -1;
printf("1Error while sending message\n");
}
// Senden an device1 ENDE
// Lesen vom decvice1 ANFANG
ret1 = read_msg(fd1, buf1, HE_BUF);
if (ret1 == -1) {
printf("1communication problem\n");
ret1 = -1;
}
//Lesen vom device1 ENDE
//close_serial(fd1);
// Senden an device2 ANFANG
printf("2datatosend: %s\n", data);
ret = send_msg(fd2, data, strlen(data));
if (ret < 0) {
ret = -1;
printf("2Error while sending message\n");
}
// Senden an device2 ENDE
// Lesen vom decvice2 ANFANG
ret2 = read_msg(fd2, buf2, HE_BUF);
if (ret2 == -1) {
printf("2communication problem\n");
ret2 = -1;
}
//Lesen vom device2 ENDE
memset(data_back, 0, 50);
if (ret1 >= 0 ){
strcpy(data_back,buf1);
}
if (ret2 >= 0 ){
strcpy(data_back,buf2);
}
// Senden an server ANFANG
printf("datatosend_back: %s\n", data_back);
ret = send_msg(fd0, data_back, strlen(data_back));
if (ret < 0) {
ret = -1;
printf("Error while sending message\n");
}
// Senden an server ENDE
usleep(300000);
fflush(stdout);
}
// Serial schliessen
close_serial(fd0);
close_serial(fd1);
close_serial(fd2);
return 0;
}
int open_serial(const char *dev) {
int fd;
//struct termios tio;
// Geraet oeffnen
fd = open(dev, O_RDWR|O_NONBLOCK);
// Alte Einstellungen sichern
if (he_tio_save == NULL) { // 4.4.2009 J. Brenner
he_tio_save = (struct termios *)malloc(sizeof(struct termios) + 4);
// 4.4.2009 J. Brenner: 4 Bytes mehr alloziert, da valgrind feststellte
// das tcgetattr() in einen nicht allozierten Bereich schreiben wollte.
tcgetattr(fd, he_tio_save);
}
/* Neue Einstellungen setzen
*
* HE_BAUDRATE: Baudrate
* CS8: 8 Datenbit, 1 Stopbit
* PARENB: Gerade Paritaet
* CREAD: Lesen ermoeglichen
* CLOCAL: Modemkontrolle deaktivieren
*
* IGNPAR: Parityfehler ignorieren
* IGNBRK: Break ignorieren
*/
memset(&tio, 0, sizeof(struct termios));
tio.c_cflag = HE_BAUDRATE | CS8 | CREAD | CLOCAL;
tio.c_iflag = IGNPAR | IGNBRK;
tio.c_oflag = 0;
tio.c_lflag = 0;
tio.c_cc[VTIME] = 0;
tio.c_cc[VMIN] = 1;
tcflush(fd, TCIOFLUSH);
tcsetattr(fd, TCSANOW, &tio);
return fd;
}
void close_serial(int fd) {
if (fd < 0)
return;
// Alte Einstellungen wiederherstellen
tcsetattr(fd, TCSANOW, he_tio_save);
tcflush(fd, TCIOFLUSH);
close(fd);
free(he_tio_save);
he_tio_save = NULL; // 4.4.2009 J. Brenner
}
int send_msg(int fd, unsigned char *data, int len) {
int ret = 0;
// Daten schreiben
ret = write(fd, data, len);
printf("we send:#%s# length of %i\n",data,len);
tcdrain(fd);
return ret;
}
int read_msg(int fd, unsigned char *buf, int len) {
int count, ret = 0;
char c;
// Daten lesen
count = 0;
do{
ret = read(fd, &c, len);
buf[count] = c;
count += ret;
} while (ret > 0 && buf[count] != '\n');
ret = count;
if (ret == -1) {
printf("Error reading from Serial\n");
printf("buf is empty\n");
printf("buf:%s length of %i\n",buf,strlen(buf));
printf("ret is %i\n",ret);
}
else {
printf("Message received:\n");
printf("buf:%s length of %i\n",buf,strlen(buf));
printf("ret is %i\n",ret);
}
return ret;
}