mirror of
https://github.com/paparazzi/paparazzi.git
synced 2026-05-27 08:55:51 +08:00
*** empty log message ***
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
|
||||
CC = gcc
|
||||
GLIB_CFLAGS = -Wall `pkg-config glib-2.0 --cflags`
|
||||
GLIB_LDFLAGS = `pkg-config glib-2.0 --libs` -lglibivy -lpcre
|
||||
|
||||
main: main.c serial_port.c
|
||||
$(CC) $(GLIB_CFLAGS) -o $@ main.c serial_port.c $(GLIB_LDFLAGS)
|
||||
|
||||
|
||||
clean:
|
||||
rm -f main
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <Ivy/ivy.h>
|
||||
#include <Ivy/ivyglibloop.h>
|
||||
|
||||
#include "serial_port.h"
|
||||
|
||||
static struct SerialPort* sp;
|
||||
static GIOChannel* ioc;
|
||||
|
||||
static void configure_term(struct termios *termios, speed_t *speed) {
|
||||
/* input modes */
|
||||
termios->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INPCK|ISTRIP|INLCR|IGNCR
|
||||
|ICRNL |IUCLC|IXON|IXANY|IXOFF|IMAXBEL);
|
||||
termios->c_iflag |= IGNPAR;
|
||||
/* control modes*/
|
||||
termios->c_cflag &= ~(CSIZE|PARENB|CRTSCTS|PARODD|HUPCL);
|
||||
termios->c_cflag |= CREAD|CS8|CSTOPB|CLOCAL;
|
||||
/* local modes */
|
||||
termios->c_lflag &= ~(ISIG|ICANON|IEXTEN|ECHO|FLUSHO|PENDIN);
|
||||
termios->c_lflag |= NOFLSH;
|
||||
/* speed */
|
||||
*speed = B9600;
|
||||
}
|
||||
|
||||
static void dump_buf ( int len, char* buf) {
|
||||
int i;
|
||||
for (i=0; i<len; i++)
|
||||
printf("%x", buf[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
#define BUF_SIZE 512
|
||||
static gboolean on_serial_data_received(GIOChannel *source,
|
||||
GIOCondition condition,
|
||||
gpointer data) {
|
||||
|
||||
// g_message("hello world");
|
||||
|
||||
|
||||
static gchar buf[BUF_SIZE];
|
||||
gsize len;
|
||||
g_io_channel_read_chars(source, buf, BUF_SIZE, &len, NULL);
|
||||
|
||||
// g_message("read %d %d %d %d", len, buf[0], buf[1], buf[2]);
|
||||
//dump_buf(len, buf);
|
||||
|
||||
g_message("read %s", buf);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean timeout_callback(gpointer data) {
|
||||
|
||||
// g_message("hello world");
|
||||
|
||||
const char* msg = "GT\n";
|
||||
gsize bw;
|
||||
g_io_channel_write_chars(ioc, msg, 3, &bw, NULL);
|
||||
g_io_channel_flush(ioc, NULL);// int foo = 42;
|
||||
// IvySendMsg("ME HELLO_WORLD 1234 5678 %d", foo);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int main ( int argc, char** argv) {
|
||||
|
||||
sp = serial_port_new();
|
||||
serial_port_open(sp, "/dev/ttyUSB0", configure_term);
|
||||
ioc = g_io_channel_unix_new(sp->fd);
|
||||
g_io_channel_set_encoding(ioc, NULL, NULL);
|
||||
g_io_channel_set_flags (ioc,G_IO_FLAG_NONBLOCK, NULL );
|
||||
g_io_add_watch (ioc, G_IO_IN, on_serial_data_received, NULL);
|
||||
|
||||
|
||||
GMainLoop *ml = g_main_loop_new(NULL, FALSE);
|
||||
|
||||
IvyInit ("Example2", "Example2 READY", NULL, NULL, NULL, NULL);
|
||||
IvyStart("127.255.255.255");
|
||||
|
||||
g_timeout_add(500, timeout_callback, NULL);
|
||||
|
||||
g_main_loop_run(ml);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
pc2rc serial port functions
|
||||
Copyright (C) 2001 Antoine Drouin
|
||||
|
||||
This file is part of paparazzi.
|
||||
|
||||
paparazzi 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 2, or (at your option)
|
||||
any later version.
|
||||
|
||||
paparazzi 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.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with paparazzi; see the file COPYING. If not, write to
|
||||
the Free Software Foundation, 59 Temple Place - Suite 330,
|
||||
Boston, MA 02111-1307, USA.
|
||||
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "serial_port.h"
|
||||
|
||||
#define TRACE(type,fmt,args...)
|
||||
#define TRACE_ERROR 1
|
||||
|
||||
struct SerialPort* serial_port_new() {
|
||||
struct SerialPort* this = malloc(sizeof(struct SerialPort));
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* opens serial port and setup the terminal
|
||||
*/
|
||||
guint
|
||||
serial_port_open(struct SerialPort* this, const char* device,
|
||||
void(*term_conf_callback)(struct termios*, speed_t*)) {
|
||||
speed_t speed;
|
||||
if ((this->fd = open(device, O_RDWR)) < 0) {
|
||||
TRACE(TRACE_ERROR,"opening %s (%s)\n", device, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (tcgetattr(this->fd, &this->orig_termios) < 0) {
|
||||
TRACE(TRACE_ERROR,"getting term settings (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
this->cur_termios = this->orig_termios;
|
||||
term_conf_callback(&this->cur_termios, &speed);
|
||||
if (cfsetispeed(&this->cur_termios, speed)) {
|
||||
TRACE(TRACE_ERROR,"setting term speed (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (tcsetattr(this->fd, TCSADRAIN, &this->cur_termios)) {
|
||||
TRACE(TRACE_ERROR,"setting term attributes (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* closes serial port and restore term settings
|
||||
*/
|
||||
guint
|
||||
serial_port_close(struct SerialPort* this) {
|
||||
if (tcflush(this->fd, TCIOFLUSH)) {
|
||||
TRACE(TRACE_ERROR,"flushing (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (tcsetattr(this->fd, TCSADRAIN, &this->orig_termios)) { // Restore modes.
|
||||
TRACE(TRACE_ERROR,"restoring term attributes (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (close(this->fd)) {
|
||||
TRACE(TRACE_ERROR,"closing (%s)\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef SERIAL_PORT_H
|
||||
#define SERIAL_PORT_H
|
||||
|
||||
#include <termios.h>
|
||||
#include <glib.h>
|
||||
|
||||
struct SerialPort {
|
||||
int fd; /* serial device fd */
|
||||
struct termios orig_termios; /* saved tty state structure */
|
||||
struct termios cur_termios; /* tty state structure */
|
||||
};
|
||||
|
||||
|
||||
struct SerialPort* serial_port_new();
|
||||
guint serial_port_open(struct SerialPort* this, const char* device,
|
||||
void(*term_conf_callback)(struct termios*, speed_t*));
|
||||
guint serial_port_close(struct SerialPort* this);
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user