2011-09-30 18:56:35 +02:00
|
|
|
// Copyright 2011 Juri Glass, Mathias Runge, Nadim El Sayed
|
2011-06-17 12:12:24 +02:00
|
|
|
// DAI-Labor, TU-Berlin
|
2011-09-30 18:56:35 +02:00
|
|
|
//
|
2011-06-17 12:12:24 +02:00
|
|
|
// This file is part of libSML.
|
2011-09-30 18:56:35 +02:00
|
|
|
//
|
2011-06-17 12:12:24 +02:00
|
|
|
// libSML is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2011-09-30 18:56:35 +02:00
|
|
|
//
|
2011-06-17 12:12:24 +02:00
|
|
|
// libSML is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2011-09-30 18:56:35 +02:00
|
|
|
//
|
2011-06-17 12:12:24 +02:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with libSML. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2011-07-13 16:43:40 +02:00
|
|
|
#include <termios.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
2011-06-27 17:28:32 +02:00
|
|
|
#include <sml/sml_file.h>
|
2011-06-17 12:12:24 +02:00
|
|
|
#include <sml/sml_transport.h>
|
|
|
|
|
2011-07-13 16:43:40 +02:00
|
|
|
int serial_port_open(const char* device) {
|
|
|
|
int bits;
|
|
|
|
struct termios config;
|
|
|
|
memset(&config, 0, sizeof(config));
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 16:43:40 +02:00
|
|
|
int fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("error: open(%s): %s\n", device, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 16:43:40 +02:00
|
|
|
// set RTS
|
|
|
|
ioctl(fd, TIOCMGET, &bits);
|
|
|
|
bits |= TIOCM_RTS;
|
|
|
|
ioctl(fd, TIOCMSET, &bits);
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 16:43:40 +02:00
|
|
|
tcgetattr( fd, &config ) ;
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 16:43:40 +02:00
|
|
|
// set 8-N-1
|
2011-07-13 18:15:39 +02:00
|
|
|
config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
|
2011-07-13 16:43:40 +02:00
|
|
|
config.c_oflag &= ~OPOST;
|
|
|
|
config.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
|
|
|
|
config.c_cflag &= ~(CSIZE | PARENB | PARODD | CSTOPB);
|
|
|
|
config.c_cflag |= CS8;
|
|
|
|
|
|
|
|
// set speed to 9600 baud
|
|
|
|
cfsetispeed( &config, B9600);
|
|
|
|
cfsetospeed( &config, B9600);
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 16:43:40 +02:00
|
|
|
tcsetattr(fd, TCSANOW, &config);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2011-06-17 12:12:24 +02:00
|
|
|
void transport_receiver(unsigned char *buffer, size_t buffer_len) {
|
2011-07-13 18:15:39 +02:00
|
|
|
// the buffer contains the whole message, with transport escape sequences.
|
2011-09-30 18:56:35 +02:00
|
|
|
// these escape sequences are stripped here.
|
2011-07-13 18:15:39 +02:00
|
|
|
sml_file *file = sml_file_parse(buffer + 8, buffer_len - 16);
|
|
|
|
// the sml file is parsed now
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 18:15:39 +02:00
|
|
|
// read here some values ..
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 18:15:39 +02:00
|
|
|
// this prints some information about the file
|
|
|
|
sml_file_print(file);
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 18:15:39 +02:00
|
|
|
// free the malloc'd memory
|
|
|
|
sml_file_free(file);
|
2011-06-17 12:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2011-07-13 18:15:39 +02:00
|
|
|
// this example assumes that a EDL21 meter sending SML messages via a
|
|
|
|
// serial device. Adjust as needed.
|
|
|
|
char *device = "/dev/cu.usbserial";
|
2011-07-13 16:43:40 +02:00
|
|
|
int fd = serial_port_open(device);
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 18:17:20 +02:00
|
|
|
if (fd > 0) {
|
2011-07-13 18:15:39 +02:00
|
|
|
// listen on the serial device, this call is blocking.
|
|
|
|
sml_transport_listen(fd, &transport_receiver);
|
|
|
|
close(fd);
|
|
|
|
}
|
2011-09-30 18:56:35 +02:00
|
|
|
|
2011-07-13 18:15:39 +02:00
|
|
|
return 0;
|
2011-06-17 12:12:24 +02:00
|
|
|
}
|
2011-10-01 22:55:34 +02:00
|
|
|
|