Skip to content

Commit 5d5121f

Browse files
author
Robert J. Ennis
committed
clean up, integrate BinDeps, update arduino-serial-lib
Signed-off-by: Robert J. Ennis <[email protected]>
1 parent e7094c1 commit 5d5121f

File tree

7 files changed

+209
-111
lines changed

7 files changed

+209
-111
lines changed

Examples/tut1/tut1.jl

-11
This file was deleted.

README.md

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
At the moment, this is a set of basic functions to communicate with the Arduino
22
(http://www.arduino.cc). It should probably cover 98% of use cases. It uses the
3-
Arduino-serial library made by Tod E. Kurt
4-
(http://todbot.com/blog/2006/12/06/arduino-serial-c-code-to-talk-to-arduino) and the
3+
MIT licensed Arduino-serial library, made by Tod E. Kurt,
4+
(https://github.com/todbot/arduino-serial) and the
55
GetC macro of Jasper den Ouden (https://github.com/o-jasper).
66

7-
A simple example is included.
7+
#Installation
88

9-
#Usage notes
9+
```julia
10+
Pkg.add("Arduino")
11+
```
1012

11-
Compile the C file in the 'src' directory according to the instructions in the
12-
file.
13+
#Usage
14+
15+
```julia
16+
using Arduino
17+
18+
fd = init("/dev/tty.usbserial",9600) # open usb port with 9600 baud rate and get back FD to communicate with Arduino on
19+
20+
writebyte(fd,uint8(1)) # send a byte to the Arduino
21+
22+
write(fd,"string") # send a string to the Arduino
23+
24+
buf = Array(Char, 256)
25+
read_until(fd, buf, '\n', , ) # read Arduino data into buf until a newline char is reached
26+
27+
flush(fd) # clear all contents of the receiving buffer
28+
```
1329

1430
#Credit
1531

deps/build.jl

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using BinDeps
2+
3+
@BinDeps.setup
4+
5+
deps = [ arduino = library_dependency("Arduino", aliases = ["libarduino"]) ]
6+
7+
@linux_only begin
8+
prefix = joinpath(BinDeps.depsdir(arduino), "usr")
9+
ardsrcdir = joinpath(BinDeps.depsdir(arduino), "src", "arduino-serial-lib")
10+
ardbuilddir = joinpath(BinDeps.depsdir(arduino), "builds", "arduino-serial-lib")
11+
provides(BuildProcess,
12+
(@build_steps begin
13+
CreateDirectory(ardbuilddir)
14+
@build_steps begin
15+
ChangeDirectory(ardbuilddir)
16+
FileRule(joinpath(prefix, "lib", "libarduino.so"), @build_steps begin
17+
`g++ -c -O -Wall -fpic -std=gnu99 $ardsrcdir/arduino-serial-lib.c`
18+
`g++ -shared -o libarduino.so arduino-serial-lib.o`
19+
`cp libarduino.so $prefix/lib`
20+
`cp arduino-serial-lib.h $prefix/include`
21+
end)
22+
end
23+
end), arduino, os = :Linux)
24+
run()
25+
end
26+
27+
@osx_only begin
28+
prefix = joinpath(BinDeps.depsdir(arduino), "usr")
29+
ardsrcdir = joinpath(BinDeps.depsdir(arduino), "src", "arduino-serial-lib")
30+
ardbuilddir = joinpath(BinDeps.depsdir(arduino), "builds", "arduino-serial-lib")
31+
provides(BuildProcess,
32+
(@build_steps begin
33+
CreateDirectory(ardbuilddir)
34+
@build_steps begin
35+
ChangeDirectory(ardbuilddir)
36+
FileRule(joinpath(prefix, "lib", "libarduino.dylib"), @build_steps begin
37+
`g++ -c -arch i386 -arch x86_64 -mmacosx-version-min=10.6 -O -Wall -fpic -std=gnu99 $ardsrcdir/arduino-serial-lib.c`
38+
`g++ -dynamiclib -undefined suppress -flat_namespace arduino-serial-lib.o -o libarduino.dylib`
39+
`cp libarduino.dylib $prefix/lib`
40+
`cp arduino-serial-lib.h $prefix/include`
41+
end)
42+
end
43+
end), arduino, os = :Darwin)
44+
run()
45+
end
46+
47+
@windows_only begin
48+
prefix = joinpath(BinDeps.depsdir(arduino), "usr")
49+
ardsrcdir = joinpath(BinDeps.depsdir(arduino), "src", "arduino-serial-lib")
50+
ardbuilddir = joinpath(BinDeps.depsdir(arduino), "builds", "arduino-serial-lib")
51+
provides(BuildProcess,
52+
(@build_steps begin
53+
CreateDirectory(ardbuilddir)
54+
@build_steps begin
55+
ChangeDirectory(ardbuilddir)
56+
FileRule(joinpath(prefix, "lib", "libarduino.dll"), @build_steps begin
57+
`g++ -c -O -Wall -std=gnu99 $ardsrcdir/arduino-serial-lib.c`
58+
`g++ -shared -o libarduino.dll arduino-serial-lib.o -Wl,--out-implib,libarduino.a
59+
`cp libarduino.dll $prefix/lib`
60+
`cp libarduino.a $prefix/lib`
61+
`cp arduino-serial-lib.h $prefix/include`
62+
end)
63+
end
64+
end), arduino, os = :Windows)
65+
run()
66+
end
67+
68+
@BinDeps.install
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,21 @@
1-
/*
2-
* Arduino-serial
3-
* --------------
4-
*
5-
* A simple command-line example program showing how a computer can
6-
* communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC)
7-
*
8-
*
9-
* Compile with something like:
10-
* gcc -o arduino-serial arduino-serial.c
11-
*
12-
* Created 5 December 2006
13-
* Copyleft (c) 2006, Tod E. Kurt, [email protected]
14-
* http://todbot.com/blog/
15-
*
16-
*
17-
* Updated 8 December 2006:
18-
* Justin McBride discoevered B14400 & B28800 aren't in Linux's termios.h.
19-
* I've included his patch, but commented out for now. One really needs a
20-
* real make system when doing cross-platform C and I wanted to avoid that
21-
* for this little program. Those baudrates aren't used much anyway. :)
22-
*
23-
* Updated 26 December 2007:
24-
* Added ability to specify a delay (so you can wait for Arduino Diecimila)
25-
* Added ability to send a binary byte number
26-
*
27-
* Update 31 August 2008:
28-
* Added patch to clean up odd baudrates from Andy at hexapodia.org
29-
*
30-
*/
31-
32-
#include <stdio.h> /* Standard input/output definitions */
33-
#include <stdlib.h>
34-
#include <stdint.h> /* Standard types */
35-
#include <string.h> /* String function definitions */
36-
#include <unistd.h> /* UNIX standard function definitions */
37-
#include <fcntl.h> /* File control definitions */
38-
#include <errno.h> /* Error number definitions */
39-
#include <termios.h> /* POSIX terminal control definitions */
40-
#include <sys/ioctl.h>
41-
#include <getopt.h>
42-
43-
int serialport_init(const char* serialport, int baud);
44-
int serialport_writebyte(int fd, uint8_t b);
45-
int serialport_write(int fd, const char* str);
46-
int serialport_read_until(int fd, char* buf, char until);
47-
48-
int serialport_writebyte( int fd, uint8_t b)
49-
{
50-
int n = write(fd,&b,1);
51-
if( n!=1)
52-
return -1;
53-
return 0;
54-
}
1+
//
2+
// arduino-serial-lib -- simple library for reading/writing serial ports
3+
//
4+
// 2006-2013, Tod E. Kurt, http://todbot.com/blog/
5+
//
556

56-
int serialport_write(int fd, const char* str)
57-
{
58-
int len = strlen(str);
59-
int n = write(fd, str, len);
60-
if( n!=len )
61-
return -1;
62-
return 0;
63-
}
7+
#include "arduino-serial-lib.h"
648

65-
int serialport_read_until(int fd, char* buf, char until)
66-
{
67-
char b[1];
68-
int i=0;
69-
do {
70-
int n = read(fd, b, 1); // read a char at a time
71-
if( n==-1) return -1; // couldn't read
72-
if( n==0 ) {
73-
usleep( 10 * 1000 ); // wait 10 msec try again
74-
continue;
75-
}
76-
buf[i] = b[0]; i++;
77-
} while( b[0] != until );
9+
#include <stdio.h> // Standard input/output definitions
10+
#include <unistd.h> // UNIX standard function definitions
11+
#include <fcntl.h> // File control definitions
12+
#include <errno.h> // Error number definitions
13+
#include <termios.h> // POSIX terminal control definitions
14+
#include <string.h> // String function definitions
15+
#include <sys/ioctl.h>
7816

79-
buf[i] = 0; // null terminate the string
80-
return 0;
81-
}
17+
// uncomment this to debug reads
18+
//#define SERIALPORTDEBUG
8219

8320
// takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
8421
// and a baud rate (bps) and connects to that port at that speed and 8N1.
@@ -89,17 +26,20 @@ int serialport_init(const char* serialport, int baud)
8926
struct termios toptions;
9027
int fd;
9128

92-
//fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
93-
// serialport,baud);
94-
95-
fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
29+
//fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
30+
fd = open(serialport, O_RDWR | O_NONBLOCK );
31+
9632
if (fd == -1) {
97-
perror("init_serialport: Unable to open port ");
33+
perror("serialport_init: Unable to open port ");
9834
return -1;
9935
}
10036

37+
//int iflags = TIOCM_DTR;
38+
//ioctl(fd, TIOCMBIS, &iflags); // turn on DTR
39+
//ioctl(fd, TIOCMBIC, &iflags); // turn off DTR
40+
10141
if (tcgetattr(fd, &toptions) < 0) {
102-
perror("init_serialport: Couldn't get term attributes");
42+
perror("serialport_init: Couldn't get term attributes");
10343
return -1;
10444
}
10545
speed_t brate = baud; // let you override switch below if needed
@@ -128,6 +68,8 @@ int serialport_init(const char* serialport, int baud)
12868
// no flow control
12969
toptions.c_cflag &= ~CRTSCTS;
13070

71+
//toptions.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset
72+
13173
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
13274
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
13375

@@ -136,12 +78,72 @@ int serialport_init(const char* serialport, int baud)
13678

13779
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
13880
toptions.c_cc[VMIN] = 0;
139-
toptions.c_cc[VTIME] = 20;
81+
toptions.c_cc[VTIME] = 0;
82+
//toptions.c_cc[VTIME] = 20;
14083

141-
if( tcsetattr(fd, TCSANOW, &toptions) < 0) {
84+
tcsetattr(fd, TCSANOW, &toptions);
85+
if( tcsetattr(fd, TCSAFLUSH, &toptions) < 0) {
14286
perror("init_serialport: Couldn't set term attributes");
14387
return -1;
14488
}
14589

14690
return fd;
14791
}
92+
93+
//
94+
int serialport_close( int fd )
95+
{
96+
return close( fd );
97+
}
98+
99+
//
100+
int serialport_writebyte( int fd, uint8_t b)
101+
{
102+
int n = write(fd,&b,1);
103+
if( n!=1)
104+
return -1;
105+
return 0;
106+
}
107+
108+
//
109+
int serialport_write(int fd, const char* str)
110+
{
111+
int len = strlen(str);
112+
int n = write(fd, str, len);
113+
if( n!=len ) {
114+
perror("serialport_write: couldn't write whole string\n");
115+
return -1;
116+
}
117+
return 0;
118+
}
119+
120+
//
121+
int serialport_read_until(int fd, char* buf, char until, int buf_max, int timeout)
122+
{
123+
char b[1]; // read expects an array, so we give it a 1-byte array
124+
int i=0;
125+
do {
126+
int n = read(fd, b, 1); // read a char at a time
127+
if( n==-1) return -1; // couldn't read
128+
if( n==0 ) {
129+
usleep( 1 * 1000 ); // wait 1 msec try again
130+
timeout--;
131+
continue;
132+
}
133+
#ifdef SERIALPORTDEBUG
134+
printf("serialport_read_until: i=%d, n=%d b='%c'\n",i,n,b[0]); // debug
135+
#endif
136+
buf[i] = b[0];
137+
i++;
138+
} while( b[0] != until && i < buf_max && timeout>0 );
139+
140+
buf[i] = 0; // null terminate the string
141+
return 0;
142+
}
143+
144+
//
145+
int serialport_flush(int fd)
146+
{
147+
sleep(2); //required to make flush work, for some reason
148+
return tcflush(fd, TCIOFLUSH);
149+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// arduino-serial-lib -- simple library for reading/writing serial ports
3+
//
4+
// 2006-2013, Tod E. Kurt, http://todbot.com/blog/
5+
//
6+
7+
8+
#ifndef __ARDUINO_SERIAL_LIB_H__
9+
#define __ARDUINO_SERIAL_LIB_H__
10+
11+
#include <stdint.h> // Standard types
12+
13+
int serialport_init(const char* serialport, int baud);
14+
int serialport_close(int fd);
15+
int serialport_writebyte( int fd, uint8_t b);
16+
int serialport_write(int fd, const char* str);
17+
int serialport_read_until(int fd, char* buf, char until, int buf_max,int timeout);
18+
int serialport_flush(int fd);
19+
20+
#endif
21+

src/.libarduino.c.swp

-16 KB
Binary file not shown.

src/Arduino.jl

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
require("GetC")
2-
31
module Arduino
42

5-
import GetC.@get_c_fun
3+
import GetC.@getCFun
64

7-
arduino = dlopen("libarduino.so")
5+
const ardLib = "libarduino"
86

9-
@get_c_fun arduino init serialport_init(serialport::Ptr{Uint8}, baud::Int32)::Int32
7+
@getCFun ardLib init serialport_init(serialport::Ptr{Uint8}, baud::Int32)::Int32
108
export init
11-
@get_c_fun arduino writebyte serialport_writebyte(fd::Int32, b::Uint8)::Int32
9+
@getCFun ardLib writebyte serialport_writebyte(fd::Int32, b::Uint8)::Int32
1210
export writebyte
13-
@get_c_fun arduino write serialport_write(fd::Int32, str::Ptr{Uint8})::Int32
11+
@getCFun ardLib write serialport_write(fd::Int32, str::Ptr{Uint8})::Int32
1412
export write
15-
@get_c_fun arduino read_until serialport_read_until(fd::Int32, buf::Ptr{Uint8}, until::Char)::Int32
13+
@getCFun ardLib read_until serialport_read_until(fd::Int32, buf::Ptr{Uint8}, until::Char, buf_max::Int32, timeout::Int32)::Int32
1614
export read_until
15+
@getCFun ardLib flush serialport_flush(fd::Int32)::Int32
16+
export flush
17+
@getCFun ardLib close serialport_close(fd::Int32)::Int32
18+
export close
1719

1820
end

0 commit comments

Comments
 (0)