Commit 12d32a5a authored by Gunter Windau's avatar Gunter Windau
Browse files

added ledbox software

parent 83346aee
LDLIBS+=-lzmq -lpthread -lboost_system -lboost_regex -lboost_program_options
#INCLUDE+=RS-232
CXXFLAGS+=-Wall -std=c++11
all: ledd
This is the source code for the Raspberry PI part of the led driver.
ledd listens for incoming zmq connections on the network interface.
After connecting, ledd will pass commands received on the zmq socket to the
aduino mega, which takes care of controlling the led outputs.
On matlab this is connected to by ledcontroller_pi.m from the biofysia toolbox
on gitlab.science.ru.nl:/marcw/biofysica
File added
//
// Hello World server in C++ with azmq and boost
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
//#include <termios.h>
//#include <unistd.h>
#include <memory>
#include <iomanip>
#include <azmq/socket.hpp>
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/asio/serial_port.hpp>
#include <boost/program_options.hpp>
#include <unistd.h>
using namespace std;
using namespace boost;
const char* arduino_dev = "/dev/ttyACM0";
class arduino_serial
{
asio::io_service io;
asio::serial_port port;
public:
arduino_serial(const char *dev) : io(), port(boost::asio::serial_port(io))
{
port.open(dev);
port.set_option(asio::serial_port_base::baud_rate(115200));
::tcflush(port.lowest_layer().native_handle(), TCIOFLUSH);
}
void read(std::string& line)
{
asio::streambuf response;
asio::read_until(port, response, "\n");
istream s(&response) ;
getline(s, line);
}
void write(const std::string& line)
{
asio::write(port, boost::asio::buffer(line+'\n'));
}
};
static int verbose = 0;
void parse_options(int argc, const char* argv[])
{
namespace po = boost::program_options;
// Declare the supported options.
po::options_description desc("Usage:");
desc.add_options()
("help,h", "show this message")
("verbose,v", "enable verbose output")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(0);
}
if (vm.count("verbose")) {
verbose = 1;
}
}
std::string str2hex(const std::string& s)
{
const static char str2hex_lookup[] = "0123456789abcdef";
unsigned int i=0,leng=s.length();
std::stringstream r;
for(i=0; i<leng; i++)
{
r<< str2hex_lookup[ s[i] >> 4 ];
r<< str2hex_lookup[ s[i] & 0x0f ];
}
return r.str();
}
int main(int argc, const char* argv[])
{
parse_options(argc, argv);
// Prepare our context and socket
asio::io_service ios;
azmq::socket socket(ios, ZMQ_REP);
socket.bind("tcp://*:5555");
arduino_serial arduino(arduino_dev);
while (true)
{
std::array<char,4096> request;
// Wait for next request from client
socket.receive(asio::buffer(request));
std::string str(request.data());
if (verbose)
cout << "Received: " << str << endl;
// Send it to the Arduino
arduino.write(str);
// Wait for the reply from the Arduino
std::string result;
arduino.read(result);
if (verbose)
{
cout << "Arduino replies:: " << result << endl
<< str2hex(result) << endl;
}
// Send reply back to client
socket.send(asio::buffer(result));
}
return 0;
}
LDLIBS+=-lzmq -lpthread -lboost_system -lboost_regex -lboost_program_options
#INCLUDE+=RS-232
CXXFLAGS+=-Wall -std=c++11
all: ledclient
hwserver01:
arduinotalk02:
arduinotalk01:
arduinotalk00:
ledserver:
This diff is collapsed.
#
#
# Author: Teunis van Beelen
#
# email: teuniz@gmail.com
#
#
CC = gcc
CFLAGS = -Wall -Wextra -Wshadow -Wformat-nonliteral -Wformat-security -Wtype-limits -o2
objects = rs232.o
all: test_rx test_tx
test_rx : $(objects) demo_rx.o
$(CC) $(objects) demo_rx.o -o test_rx
test_tx : $(objects) demo_tx.o
$(CC) $(objects) demo_tx.o -o test_tx
demo_rx.o : demo_rx.c rs232.h
$(CC) $(CFLAGS) -c demo_rx.c -o demo_rx.o
demo_tx.o : demo_tx.c rs232.h
$(CC) $(CFLAGS) -c demo_tx.c -o demo_tx.o
rs232.o : rs232.h rs232.c
$(CC) $(CFLAGS) -c rs232.c -o rs232.o
clean :
$(RM) test_rx test_tx $(objects) demo_rx.o demo_tx.o rs232.o
#
#
#
#
/**************************************************
file: demo_rx.c
purpose: simple demo that receives characters from
the serial port and print them on the screen,
exit the program by pressing Ctrl-C
compile with the command: gcc demo_rx.c rs232.c -Wall -Wextra -o2 -o test_rx
**************************************************/
#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "rs232.h"
int main()
{
int i, n,
cport_nr=0, /* /dev/ttyS0 (COM1 on windows) */
bdrate=9600; /* 9600 baud */
unsigned char buf[4096];
char mode[]={'8','N','1',0};
if(RS232_OpenComport(cport_nr, bdrate, mode))
{
printf("Can not open comport\n");
return(0);
}
while(1)
{
n = RS232_PollComport(cport_nr, buf, 4095);
if(n > 0)
{
buf[n] = 0; /* always put a "null" at the end of a string! */
for(i=0; i < n; i++)
{
if(buf[i] < 32) /* replace unreadable control-codes by dots */
{
buf[i] = '.';
}
}
printf("received %i bytes: %s\n", n, (char *)buf);
}
#ifdef _WIN32
Sleep(100);
#else
usleep(100000); /* sleep for 100 milliSeconds */
#endif
}
return(0);
}
/**************************************************
file: demo_tx.c
purpose: simple demo that transmits characters to
the serial port and print them on the screen,
exit the program by pressing Ctrl-C
compile with the command: gcc demo_tx.c rs232.c -Wall -Wextra -o2 -o test_tx
**************************************************/
#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#include "rs232.h"
int main()
{
int i=0,
cport_nr=0, /* /dev/ttyS0 (COM1 on windows) */
bdrate=9600; /* 9600 baud */
char mode[]={'8','N','1',0},
str[2][512];
strcpy(str[0], "The quick brown fox jumped over the lazy grey dog.\n");
strcpy(str[1], "Happy serial programming!\n");
if(RS232_OpenComport(cport_nr, bdrate, mode))
{
printf("Can not open comport\n");
return(0);
}
while(1)
{
RS232_cputs(cport_nr, str[i]);
printf("sent: %s\n", str[i]);
#ifdef _WIN32
Sleep(1000);
#else
usleep(1000000); /* sleep for 1 Second */
#endif
i++;
i %= 2;
}
return(0);
}
Library: RS-232
Author: Teunis van Beelen
Url: http://www.teuniz.net/RS-232/
E-mail: teuniz@gmail.com
License: GPLv3
Last revision: July 10, 2016
int RS232_OpenComport(int comport_number, int baudrate, const char * mode)
Opens the comport, comportnumber starts with 0 (see the list of numbers).
Baudrate is expressed in baud per second i.e 115200 (see the list of possible baudrates).
Mode is a string in the form of "8N1", "7E2", etc.
8N1 means eight databits, no parity, one stopbit. If in doubt, use 8N1 (see the list of possible modes).
Returns 1 in case of an error.
In case the comport is already opened (by another process),
it will not open the port but raise an error instead.
int RS232_PollComport(int comport_number, unsigned char *buf, int size)
Gets characters from the serial port (if any). Buf is a pointer to a buffer
and size the size of the buffer in bytes.
Returns the amount of received characters into the buffer.
After succesfully opening the COM-port, connect this function to a timer.
The timer should have an interval of approx. 20 to 100 milliSeconds.
Do not forget to stop the timer before closing the COM-port.
Allways check the return value! The return value tells you how many bytes
are actually received and present in your buffer!
int RS232_SendByte(int comport_number, unsigned char byte)
Sends a byte via the serial port. Returns 1 in case of an error.
int RS232_SendBuf(int comport_number, unsigned char *buf, int size)
Sends multiple bytes via the serial port. Buf is a pointer to a buffer
and size the size of the buffer in bytes.
Returns -1 in case of an error, otherwise it returns the amount of bytes sent.
This function blocks (it returns after all the bytes have been processed).
void RS232_CloseComport(int comport_number)
Closes the serial port.
void RS232_cputs(int comport_number, const char *text)
Sends a string via the serial port. String must be null-terminated.
int RS232_GetPortnr(const char *devname)
Returns the comport number based on the device name e.g. "ttyS0" or "COM1".
(Doesn't mean the device actually exists!)
Returns -1 when not found.
The following functions are normally not needed but can be used to set or check the status of the control-lines:
================================================================================================================
void RS232_enableDTR(int comport_number)
Sets the DTR line high (active state).
void RS232_disableDTR(int comport_number)
Sets the DTR line low (non active state).
void RS232_enableRTS(int comport_number)
Sets the RTS line high (active state).
void RS232_disableRTS(int comport_number)
Sets the RTS line low (non active state).
int RS232_IsDSREnabled(int comport_number)
Checks the status of the DSR-pin. Returns 1 when the the DSR line is high (active state), otherwise 0.
int RS232_IsCTSEnabled(int comport_number)
Checks the status of the CTS-pin. Returns 1 when the the CTS line is high (active state), otherwise 0.
int RS232_IsDCDEnabled(int comport_number)
Checks the status of the DCD-pin. Returns 1 when the the DCD line is high (active state), otherwise 0.
The following functions are normally not needed but can be used to empty the rx/tx buffers:
===========================================================================================
("discards data written to the serial port but not transmitted, or data received but not read")
void RS232_flushRX(int comport_number)
Flushes data received but not read.
void RS232_flushTX(int comport_number)
Flushes data written but not transmitted.
void RS232_flushRXTX(int comport_number)
Flushes both data received but not read, and data written but not transmitted.
Notes:
You don't need to call RS232_PollComport() when you only want to send characters.
Sending and receiving do not influence eachother.
The os (kernel) has an internal buffer of 4096 bytes (for traditional onboard serial ports).
USB/Serial-converter drivers use much bigger buffers (multiples of 4096).
If this buffer is full and a new character arrives on the serial port,
the oldest character in the buffer will be overwritten and thus will be lost.
After a successfull call to RS232_OpenComport(), the os will start to buffer incoming characters.
tip: To get access to the serial port on Linux, you need to be a member of the group "dialout".
Note: Traditional (on-board) UART's usually have a speed limit of max. 115200 baud.
Special cards and USB to Serial converters can usually be set to higher baudrates.
List of comport numbers, possible baudrates and modes:
Linux windows
0 ttyS0 COM1
1 ttyS1 COM2
2 ttyS2 COM3
3 ttyS3 COM4
4 ttyS4 COM5
5 ttyS5 COM6
6 ttyS6 COM7
7 ttyS7 COM8
8 ttyS8 COM9
9 ttyS9 COM10
10 ttyS10 COM11
11 ttyS11 COM12
12 ttyS12 COM13
13 ttyS13 COM14
14 ttyS14 COM15
15 ttyS15 COM16
16 ttyUSB0 n.a.
17 ttyUSB1 n.a.
18 ttyUSB2 n.a.
19 ttyUSB3 n.a.
20 ttyUSB4 n.a.
21 ttyUSB5 n.a.
22 ttyAMA0 n.a.
23 ttyAMA1 n.a.
24 ttyACM0 n.a.
25 ttyACM1 n.a.
26 rfcomm0 n.a.
27 rfcomm1 n.a.
28 ircomm0 n.a.
29 ircomm1 n.a.
FreeBSD
30 cuau0 n.a.
31 cuau1 n.a.
32 cuau2 n.a.
33 cuau3 n.a.
34 cuaU0 n.a.
35 cuaU1 n.a.
36 cuaU2 n.a.
37 cuaU3 n.a.
Linux windows
50 n.a.
75 n.a.
110 110
134 n.a.
150 n.a.
200 n.a.
300 300
600 600
1200 1200
1800 n.a.
2400 2400
4800 4800
9600 9600
19200 19200
38400 38400
57600 57600
115200 115200
230400 128000
460800 256000
500000 500000
576000 n.a.
921600 n.a.
1000000 1000000
1152000 n.a.
1500000 n.a.
2000000 n.a.
2500000 n.a.
3000000 n.a.
3500000 n.a.
4000000 n.a.
Mode
8N1
8O1
8E1
8N2
8O2
8E2
7N1
7O1
7E1
7N2
7O2
7E2
6N1
6O1
6E1
6N2
6O2
6E2
5N1
5O1
5E1
5N2
5O2
5E2
This diff is collapsed.
Compiling the demo's can be done as follows:
gcc demo_rx.c rs232.c -Wall -Wextra -o2 -o test_rx
gcc demo_tx.c rs232.c -Wall -Wextra -o2 -o test_tx
or use the makefile by entering "make"
Run the demo by typing:
./test_rx
or
./test_tx
To include this library into your project:
Put the two files rs232.h and rs232.c in your project/sourcedirectory.
Write #include "rs232.h" in your sourcefiles that needs access to the library.
Add the file rs232.c to your project settings (in order to get it compiled and linked with your program).
/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Teunis van Beelen
*
* Email: teuniz@gmail.com
*
***************************************************************************
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
************************************************************************