├── .gitattributes ├── .gitignore ├── Arduino ├── arduino_serial_example │ └── arduino_serial_example.ino └── arduino_serial_example_bidirectional │ └── arduino_serial_example_bidirectional.ino ├── README.md ├── c ├── README.md ├── c_serial_example.c └── c_serial_example_bidirectional.c ├── java ├── Java_serial_example.java ├── Java_serial_example_bidirectional.java └── README.md ├── php ├── README.md ├── index.php ├── index_bidirectional.php ├── light.png └── php_serial.class.php └── python ├── README.md ├── python_serial_example.py ├── python_serial_example_bidirectional.py └── python_serial_example_bidirectional_threads.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must end with two \r 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /Arduino/arduino_serial_example/arduino_serial_example.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | // Attach a LED to Pin 13 22 | int led = 13; 23 | 24 | char inChar; 25 | 26 | // the setup routine runs once when you press reset: 27 | void setup() { 28 | // initialize the digital pin as an output and turn off 29 | pinMode(led, OUTPUT); 30 | digitalWrite(led, LOW); 31 | 32 | // initialize serial port at a baud rate of 115200 bps 33 | Serial.begin(115200); 34 | delay(100); 35 | Serial.println("start"); 36 | } 37 | 38 | void loop() { 39 | 40 | inChar = '\0'; 41 | 42 | while (Serial.available()) { 43 | 44 | // get the new byte: 45 | inChar = (char)Serial.read(); 46 | } 47 | 48 | //Serial.print("inChar= ");Serial.println(inChar); 49 | //char byteParsed = parseResponse(inChar); 50 | 51 | if (inChar == '1') { // compare received data 52 | digitalWrite(led, HIGH); // turn on light 53 | } else if (inChar == '0') { 54 | digitalWrite(led, LOW); // turn off light 55 | } 56 | 57 | delay(10); 58 | } 59 | 60 | // the characters sent to Arduino as String are interpreted as ASCII, 61 | // we decrease 48 to return to ASCII range 62 | char parseResponse(char received) { 63 | return received - 48; 64 | } 65 | 66 | 67 | -------------------------------------------------------------------------------- /Arduino/arduino_serial_example_bidirectional/arduino_serial_example_bidirectional.ino: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | byte inByte; 22 | 23 | boolean received = false; // store if Arduino received something 24 | 25 | // the setup routine runs once when you press reset: 26 | void setup() { 27 | // initialize serial port at a baud rate of 115200 bps 28 | Serial.begin(115200); 29 | delay(100); 30 | Serial.println("start"); 31 | } 32 | 33 | #define RCVSIZE 2 34 | 35 | void loop() { 36 | // put your main code here, to run repeatedly: 37 | byte msg[RCVSIZE]; 38 | int count = 0; 39 | 40 | while (Serial.available() > 0) { 41 | 42 | // get the new byte: 43 | byte inByte = (byte)Serial.read(); 44 | //Serial.println(inByte); 45 | msg[count] = inByte; 46 | count++; 47 | 48 | received = true; 49 | } 50 | 51 | if(received) { 52 | 53 | pinMode(msg[0], OUTPUT); 54 | digitalWrite(msg[0], msg[1]); 55 | if (msg[1] == 1) { 56 | Serial.print(msg[0]);Serial.println(" ON"); 57 | } else if (msg[1] == 0) { 58 | Serial.print(msg[0]);Serial.println(" OFF"); 59 | } 60 | 61 | received = false; 62 | } 63 | 64 | delay(10); 65 | } 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | UDOO Serial Libraries Examples 2 | ========== 3 | 4 | 5 | Serial Libraries Communication Samples for [UDOO Board](http://www.udoo.org) 6 | 7 | Copyright (C) 2014 [Ekironji Solutions](ekironjisolutions@gmail.com) 8 | 9 | These example’s scripts are meant to demonstrate how to implement a uni\bidirectional communication between an Arduino sketch (running on SAM3X Arduino Compatible processor) and a binary application on iMX6 Linux processor. 10 | 11 | The Arduino sketch will remain the same no matter which programming language you’ll use to develop the binary on iMX6. 12 | 13 | There are two example scripts for each programming language: C, Java, PHP, Python. 14 | 15 | Each program is meant to be executed while the matching Arduino Sketch is running on SAM3X: 16 | 17 | arduino_serial_example.ino 18 | 19 | c_serial_example.c 20 | java_serial_example.java 21 | php_serial_example.php 22 | python_serial_example.py 23 | 24 | 25 | arduino_serial_example_bidirectional.ino 26 | 27 | c_serial_example_bidirectional.c 28 | java_serial_example_bidirectional.java 29 | php_serial_example_bidirectional.php 30 | python_serial_example_bidirectional.py 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /c/README.md: -------------------------------------------------------------------------------- 1 | C Serial Libraries for UDOO 2 | --------- 3 | 4 | This file describes how to compile and run the C examples contained in this folder. 5 | 6 | 1 - Open a terminal and navigate to this folder: 7 | 8 | cd serial_libraries_examples/c/ 9 | 10 | 2 - Compile the C file: 11 | 12 | for c_serial_example.c: 13 | 14 | gcc -o c_serial_example c_serial_example.c 15 | 16 | for c_serial_example_bidirectional.c: 17 | 18 | gcc -o c_serial_example_bidirectional c_serial_example_bidirectional.c 19 | 20 | 3 - Run the C program: 21 | 22 | for c_serial_example.c: 23 | 24 | ./c_serial_example 25 | 26 | for c_serial_example_bidirectional.c: 27 | 28 | ./c_serial_example_bidirectional 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /c/c_serial_example.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | int 28 | set_interface_attribs (int fd, int speed, int parity) 29 | { 30 | struct termios tty; 31 | memset (&tty, 0, sizeof tty); 32 | if (tcgetattr (fd, &tty) != 0) 33 | { 34 | printf ("error from tcgetattr", errno); 35 | return -1; 36 | } 37 | 38 | cfsetospeed (&tty, speed); 39 | cfsetispeed (&tty, speed); 40 | 41 | tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars 42 | // disable IGNBRK for mismatched speed tests; otherwise receive break 43 | // as \000 chars 44 | tty.c_iflag &= ~IGNBRK; // ignore break signal 45 | tty.c_lflag = 0; // no signaling chars, no echo, 46 | // no canonical processing 47 | tty.c_oflag = 0; // no remapping, no delays 48 | tty.c_cc[VMIN] = 0; // read doesn't block 49 | tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout 50 | 51 | tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl 52 | 53 | tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, 54 | // enable reading 55 | tty.c_cflag &= ~(PARENB | PARODD); // shut off parity 56 | tty.c_cflag |= parity; 57 | tty.c_cflag &= ~CSTOPB; 58 | tty.c_cflag &= ~CRTSCTS; 59 | 60 | if (tcsetattr (fd, TCSANOW, &tty) != 0) 61 | { 62 | printf ("error %d from tcsetattr", errno); 63 | return -1; 64 | } 65 | return 0; 66 | } 67 | 68 | void 69 | set_blocking (int fd, int should_block) 70 | { 71 | struct termios tty; 72 | memset (&tty, 0, sizeof tty); 73 | if (tcgetattr (fd, &tty) != 0) 74 | { 75 | printf ("error %d from tggetattr", errno); 76 | return; 77 | } 78 | 79 | tty.c_cc[VMIN] = should_block ? 1 : 0; 80 | tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout 81 | 82 | if (tcsetattr (fd, TCSANOW, &tty) != 0) 83 | printf ("error %d setting term attributes", errno); 84 | } 85 | 86 | int main(void) { 87 | char *portname = "/dev/ttyS0"; 88 | 89 | int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); 90 | if (fd < 0) 91 | { 92 | printf("error %d opening %s: %s", errno, portname, strerror (errno)); 93 | return; 94 | } 95 | 96 | set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) 97 | set_blocking (fd, 0); // set no blocking 98 | 99 | write(fd, "1", 1); // send 1 character greeting 100 | 101 | sleep(1); 102 | 103 | write(fd, "0", 1); // send 1 character greeting 104 | } 105 | -------------------------------------------------------------------------------- /c/c_serial_example_bidirectional.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | int 28 | set_interface_attribs (int fd, int speed, int parity) 29 | { 30 | struct termios tty; 31 | memset (&tty, 0, sizeof tty); 32 | if (tcgetattr (fd, &tty) != 0) 33 | { 34 | printf ("error from tcgetattr", errno); 35 | return -1; 36 | } 37 | 38 | cfsetospeed (&tty, speed); 39 | cfsetispeed (&tty, speed); 40 | 41 | tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars 42 | // disable IGNBRK for mismatched speed tests; otherwise receive break 43 | // as \000 chars 44 | tty.c_iflag &= ~IGNBRK; // ignore break signal 45 | tty.c_lflag = 0; // no signaling chars, no echo, 46 | // no canonical processing 47 | tty.c_oflag = 0; // no remapping, no delays 48 | tty.c_cc[VMIN] = 0; // read doesn't block 49 | tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout 50 | 51 | tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl 52 | 53 | tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, 54 | // enable reading 55 | tty.c_cflag &= ~(PARENB | PARODD); // shut off parity 56 | tty.c_cflag |= parity; 57 | tty.c_cflag &= ~CSTOPB; 58 | tty.c_cflag &= ~CRTSCTS; 59 | 60 | if (tcsetattr (fd, TCSANOW, &tty) != 0) 61 | { 62 | printf ("error %d from tcsetattr", errno); 63 | return -1; 64 | } 65 | return 0; 66 | } 67 | 68 | void 69 | set_blocking (int fd, int should_block) 70 | { 71 | struct termios tty; 72 | memset (&tty, 0, sizeof tty); 73 | if (tcgetattr (fd, &tty) != 0) 74 | { 75 | printf ("error %d from tggetattr", errno); 76 | return; 77 | } 78 | 79 | tty.c_cc[VMIN] = should_block ? 1 : 0; 80 | tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout 81 | 82 | if (tcsetattr (fd, TCSANOW, &tty) != 0) 83 | printf ("error %d setting term attributes", errno); 84 | } 85 | 86 | int main(void) { 87 | char *portname = "/dev/ttyS0"; 88 | 89 | int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); 90 | if (fd < 0) 91 | { 92 | printf("error %d opening %s: %s", errno, portname, strerror (errno)); 93 | return; 94 | } 95 | 96 | set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) 97 | set_blocking (fd, 0); // set no blocking 98 | 99 | char buf [7]; 100 | int n; 101 | 102 | char buffON[2] = {13,1}; 103 | write(fd, buffON, 2); // send 2 character greeting 104 | 105 | sleep(1); // delay 1 second 106 | 107 | n = read (fd, buf, sizeof buf); // read up to 7 characters if ready to read 108 | if (n > 0) { 109 | //printf("quanti: %d\n", n); 110 | int i; 111 | for (i = 0; i < n; i++) { 112 | printf("%c",buf[i]); 113 | } 114 | } 115 | 116 | char buffOFF[2] = {13,0}; 117 | write(fd, buffOFF, 2); 118 | 119 | sleep(1); // delay 1 second 120 | 121 | n = read (fd, buf, sizeof buf); // read up to 7 characters if ready to read 122 | if (n > 0) { 123 | //printf("quanti: %d\n", n); 124 | int i; 125 | for (i = 0; i < n; i++) { 126 | printf("%c",buf[i]); 127 | } 128 | } 129 | 130 | } 131 | -------------------------------------------------------------------------------- /java/Java_serial_example.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | import gnu.io.CommPort; 22 | import gnu.io.CommPortIdentifier; 23 | import gnu.io.SerialPort; 24 | 25 | import java.io.IOException; 26 | import java.io.OutputStream; 27 | 28 | public class Java_serial_example { 29 | 30 | static Serial UDOOserial; 31 | 32 | public static void main(String[] args) { 33 | 34 | UDOOserial = new Serial(); 35 | 36 | System.out.println("connecting to serial port..."); 37 | try { 38 | UDOOserial.connect("/dev/ttyS0"); 39 | System.out.println("Connected!"); 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | System.exit(0); 43 | } 44 | 45 | try{ 46 | System.out.println("write chars to Arduino..."); 47 | UDOOserial.writeChar('1'); 48 | Thread.sleep(1000); 49 | UDOOserial.writeChar('0'); 50 | } catch(IOException ioe) { 51 | ioe.printStackTrace(); 52 | } catch(InterruptedException ie) { 53 | ie.printStackTrace(); 54 | } finally { 55 | UDOOserial.disconnect(); 56 | } 57 | 58 | System.out.println("Finished!"); 59 | System.exit(0); 60 | } 61 | 62 | 63 | public static class Serial { 64 | 65 | private SerialPort mSerialPort; 66 | private OutputStream out; 67 | 68 | public Serial() { 69 | } 70 | 71 | public void connect(String portName) throws Exception { 72 | CommPortIdentifier mCommPortIdentifier = CommPortIdentifier.getPortIdentifier(portName); 73 | if (mCommPortIdentifier.isCurrentlyOwned()) { 74 | System.out.println("Error: Port is currently in use!"); 75 | } else { 76 | CommPort mCommPort = mCommPortIdentifier.open(this.getClass().getName(), 2000); 77 | if(mCommPort instanceof SerialPort) { 78 | mSerialPort = (SerialPort) mCommPort; 79 | mSerialPort.setSerialPortParams(115200, 80 | SerialPort.DATABITS_8, 81 | SerialPort.STOPBITS_1, 82 | SerialPort.PARITY_NONE); 83 | mSerialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); 84 | 85 | out = mSerialPort.getOutputStream(); 86 | } else { 87 | System.out.println("Error: Only serial ports are handled by this example!"); 88 | } 89 | } 90 | } 91 | 92 | public void writeChar(char msg) throws IOException { 93 | out.write(msg); 94 | } 95 | 96 | public void disconnect() { 97 | try { 98 | if (out != null) { 99 | out.close(); 100 | out = null; 101 | } 102 | if (mSerialPort != null) { 103 | mSerialPort.close(); 104 | mSerialPort = null; 105 | } 106 | } catch (Exception e) { 107 | System.out.println("Error: Port not closed!"); 108 | } finally { 109 | out = null; 110 | mSerialPort = null; 111 | } 112 | } 113 | 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /java/Java_serial_example_bidirectional.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | */ 20 | 21 | import gnu.io.CommPort; 22 | import gnu.io.CommPortIdentifier; 23 | import gnu.io.SerialPort; 24 | 25 | import java.io.IOException; 26 | import java.io.OutputStream; 27 | import java.io.InputStream; 28 | 29 | public class Java_serial_example_bidirectional { 30 | 31 | static Serial UDOOserial; 32 | 33 | public static void main(String[] args) { 34 | 35 | UDOOserial = new Serial(); 36 | 37 | System.out.println("connecting to serial port..."); 38 | try { 39 | UDOOserial.connect("/dev/ttyS0"); 40 | System.out.println("Connected!"); 41 | } catch (Exception e) { 42 | e.printStackTrace(); 43 | System.exit(0); 44 | } 45 | 46 | byte[] outBuff = new byte[2]; 47 | 48 | try{ 49 | UDOOserial.read_startBufferThread(); 50 | outBuff[0] = (byte)13; 51 | outBuff[1] = (byte)1; 52 | UDOOserial.writeByteArray(outBuff); 53 | 54 | Thread.sleep(1000); 55 | 56 | outBuff[0] = (byte)13; 57 | outBuff[1] = (byte)0; 58 | UDOOserial.writeByteArray(outBuff); 59 | 60 | Thread.sleep(1000); 61 | } catch(IOException ioe) { 62 | ioe.printStackTrace(); 63 | } catch(InterruptedException ie) { 64 | ie.printStackTrace(); 65 | } 66 | 67 | System.exit(0); 68 | } 69 | 70 | 71 | public static class Serial { 72 | 73 | private SerialPort mSerialPort; 74 | private OutputStream out; 75 | private InputStream in; 76 | 77 | public Serial() { 78 | } 79 | 80 | public void connect(String portName) throws Exception { 81 | CommPortIdentifier mCommPortIdentifier = CommPortIdentifier.getPortIdentifier(portName); 82 | if (mCommPortIdentifier.isCurrentlyOwned()) { 83 | System.out.println("Error: Port is currently in use!"); 84 | } else { 85 | CommPort mCommPort = mCommPortIdentifier.open(this.getClass().getName(), 2000); 86 | if(mCommPort instanceof SerialPort) { 87 | mSerialPort = (SerialPort) mCommPort; 88 | mSerialPort.setSerialPortParams(115200, 89 | SerialPort.DATABITS_8, 90 | SerialPort.STOPBITS_1, 91 | SerialPort.PARITY_NONE); 92 | mSerialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); 93 | 94 | out = mSerialPort.getOutputStream(); 95 | in = mSerialPort.getInputStream(); 96 | } else { 97 | System.out.println("Error: Only serial ports are handled by this example!"); 98 | } 99 | } 100 | } 101 | 102 | public void writeByteArray(byte[] msg) throws IOException { 103 | out.write(msg); 104 | } 105 | 106 | public void read_startBufferThread() { 107 | (new Thread(new SerialReader(in))).start(); 108 | } 109 | 110 | public void disconnect() { 111 | try { 112 | if (out != null) { 113 | out.close(); 114 | out = null; 115 | } 116 | if (mSerialPort != null) { 117 | mSerialPort.close(); 118 | mSerialPort = null; 119 | } 120 | } catch (Exception e) { 121 | System.out.println("Error: Port not closed!"); 122 | } finally { 123 | out = null; 124 | mSerialPort = null; 125 | } 126 | } 127 | 128 | public static class SerialReader implements Runnable { 129 | 130 | InputStream in; 131 | 132 | public SerialReader(InputStream in) { 133 | this.in = in; 134 | } 135 | 136 | public void run() { 137 | byte[] buffer = new byte[7]; 138 | int len = -1; 139 | String mStringReceived = ""; 140 | try { 141 | while((len = this.in.read(buffer)) > -1) { 142 | mStringReceived += new String(buffer, 0, len); 143 | if (buffer[len-1] == '\n') { 144 | System.out.println(mStringReceived); 145 | mStringReceived = ""; 146 | } 147 | } 148 | } catch (IOException ioe) { 149 | ioe.printStackTrace(); 150 | } 151 | } 152 | } 153 | 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /java/README.md: -------------------------------------------------------------------------------- 1 | JAVA Serial libraries for UDOO 2 | ----------------- 3 | 4 | This file describes how to compile and run the Java examples contained in this folder. 5 | 6 | 1 - Starting from UDOObuntu2 all the libraries and link should be preinstalled. Otherwise install the java serial library and create the needed serial link with these commands: 7 | 8 | sudo apt-get install librxtx-java 9 | sudo ln -s /dev/ttymxc3 /dev/ttyS0 10 | 11 | 2 - Open a terminal and navigate to this folder: 12 | 13 | cd serial_libraries_examples/java/ 14 | 15 | 3 - Compile the Java file: 16 | 17 | for Java_serial_example.java: 18 | 19 | javac -cp /usr/share/java/RXTXcomm.jar:. Java_serial_example.java 20 | 21 | for Java_serial_example_bidirectional.java: 22 | 23 | javac -cp /usr/share/java/RXTXcomm.jar:. Java_serial_example_bidirectional.java 24 | 25 | 4 - Run the Java program: 26 | 27 | for Java_serial_example.java: 28 | 29 | java -Djava.library.path=/usr/lib/jni -cp /usr/share/java/RXTXcomm.jar:. Java_serial_example 30 | 31 | for Java_serial_example_bidirectional.java: 32 | 33 | java -Djava.library.path=/usr/lib/jni -cp /usr/share/java/RXTXcomm.jar:. Java_serial_example_bidirectional 34 | -------------------------------------------------------------------------------- /php/README.md: -------------------------------------------------------------------------------- 1 | PHP Serial Libraries for UDOO 2 | -------- 3 | 4 | This file describes how to run the PHP examples contained in this folder. 5 | 6 | To run these PHP examples, we will use the PHP interpreter and its embedded Web Server. The same examples can be executed over Apache or nginx too. 7 | 8 | 1 - Flash the Arduino Sketch using the Arduino IDE 9 | 10 | 2 - Create the needed serial link [substitute `/dev/ttyREAL` with: `/dev/ttymxc3` for the UDOO QUAD/DUAL - `/dev/ttyMCC` for the UDOO NEO]: 11 | 12 | sudo ln -s /dev/ttyREAL /dev/ttyS0 13 | 14 | 3 - Navigate in this folder: 15 | 16 | cd serial_libraries_examples/php/ 17 | 18 | 19 | 4 - Start the embedded web server: 20 | 21 | php -S 0.0.0.0:8080 22 | 23 | 24 | 5 - Open a browser (on your UDOO or on your computer) and run the examples writing in the address bar: 25 | 26 | For the basic serial example: 27 | 28 | http://127.0.0.1:8080/index.php 29 | 30 | 31 | For the bidirectional serial example: 32 | 33 | http://127.0.0.1:8080/index_bidirectional.php 34 | 35 | Please note, use `127.0.0.1` if you open the browser directly on the UDOO; If you want to connect from an external PC WiFi/Ethernet IP address are supported too. 36 | If you are working on UDOO NEO connected with USB connection you can use `192.168.7.2`. 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /php/index.php: -------------------------------------------------------------------------------- 1 | 4 | * 5 | * This file is part of serial libraries examples for UDOO 6 | * 7 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This libraries are distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | * 20 | */ 21 | 22 | define("PORT","/dev/ttyS0"); 23 | $ledOn = false; 24 | 25 | if (isset($_GET['action'])) { 26 | include "php_serial.class.php"; 27 | 28 | $serial = new phpSerial; 29 | $serial->deviceSet(PORT); 30 | $serial->confBaudRate(115200); 31 | $serial->confParity("none"); 32 | $serial->confStopBits(1); 33 | $serial->confFlowControl("none"); 34 | $serial->deviceOpen(); 35 | 36 | if ($_GET['action'] == "on") { 37 | $serial->sendMessage("1"); 38 | $ledOn = true; 39 | } elseif ($_GET['action'] == "off") { 40 | $serial->sendMessage("0"); 41 | $ledOn = false; 42 | } 43 | } 44 | ?> 45 | 46 | 47 | 48 | 49 | LED 13 example 50 | 51 | 52 |
53 | 54 |
55 | LED is ON, turn it OFF 56 | 57 |
58 | LED is OFF, turn it ON 59 | 60 |
61 | 62 | 63 | -------------------------------------------------------------------------------- /php/index_bidirectional.php: -------------------------------------------------------------------------------- 1 | 4 | * 5 | * This file is part of serial libraries examples for UDOO 6 | * 7 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 8 | * it under the terms of the GNU General Public License as published by 9 | * the Free Software Foundation, either version 3 of the License, or 10 | * (at your option) any later version. 11 | * 12 | * This libraries are distributed in the hope that it will be useful, 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | * GNU General Public License for more details. 16 | * 17 | * You should have received a copy of the GNU General Public License 18 | * along with this program. If not, see . 19 | * 20 | */ 21 | 22 | define("PORT","/dev/ttyMCC"); 23 | $ledOn = false; 24 | 25 | if (isset($_GET['action'])) { 26 | include "php_serial.class.php"; 27 | 28 | $serial = new phpSerial; 29 | $serial->deviceSet(PORT); 30 | $serial->confBaudRate(115200); 31 | $serial->confParity("none"); 32 | $serial->confStopBits(1); 33 | $serial->confFlowControl("none"); 34 | $serial->deviceOpen(); 35 | 36 | if ($_GET['action'] == "on") { 37 | $serial->sendMessage(chr(13).chr(1)); 38 | $ledOn = true; 39 | } elseif ($_GET['action'] == "off") { 40 | $serial->sendMessage(chr(13).chr(0)); 41 | $ledOn = false; 42 | } 43 | 44 | $read = $serial->readPort(); 45 | echo "Arduino response is: $read"; 46 | } 47 | ?> 48 | 49 | 50 | 51 | 52 | LED 13 example 53 | 54 | 55 |
56 | 57 |
58 | LED is ON, turn it OFF 59 | 60 |
61 | LED is OFF, turn it ON 62 | 63 |
64 | 65 | 66 | -------------------------------------------------------------------------------- /php/light.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UDOOboard/serial_libraries_examples/a81d6e05f1e8e951130c8238ce5744525cad7524/php/light.png -------------------------------------------------------------------------------- /php/php_serial.class.php: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/UDOOboard/serial_libraries_examples/a81d6e05f1e8e951130c8238ce5744525cad7524/php/php_serial.class.php -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | Python serial libraries for UDOO 2 | ------------ 3 | 4 | This file describes how to run the python examples contained in this folder. 5 | 6 | 1 - Edit the port name inside the example, or create the needed symlink. Replace `/dev/ttyREAL` with: `/dev/ttymxc3` for the UDOO QUAD/DUAL - `/dev/ttyMCC` for the UDOO NEO: 7 | 8 | sudo ln -s /dev/ttyREAL /dev/ttyS0 9 | 10 | 2 - Run the python example: 11 | 12 | for the base example: 13 | 14 | python python_serial_example.py 15 | 16 | for the bidirectional one: 17 | 18 | python python_serial_example_bidirectional.py 19 | 20 | a bidirectional example using thread to read/write can be run with: 21 | 22 | python python_serial_example_bidirectional_threads.py 23 | -------------------------------------------------------------------------------- /python/python_serial_example.py: -------------------------------------------------------------------------------- 1 | ''' 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | ''' 20 | 21 | import serial 22 | import time 23 | 24 | ser = serial.Serial('/dev/ttyS0',115200,timeout=1) 25 | ser.flushOutput() 26 | 27 | print 'Serial connected' 28 | 29 | ser.write("1") # write to Arduino to turn ON the LED 30 | 31 | time.sleep(1) # delay for 1 second 32 | 33 | ser.write("0") # write to Arduino to turn OFF the LED 34 | 35 | time.sleep(1) # delay for 1 second 36 | -------------------------------------------------------------------------------- /python/python_serial_example_bidirectional.py: -------------------------------------------------------------------------------- 1 | ''' 2 | * Copyright (C) 2014 Ekironji 3 | * 4 | * This file is part of serial libraries examples for UDOO 5 | * 6 | * Serial libraries examples for UDOO is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This libraries are distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program. If not, see . 18 | * 19 | ''' 20 | 21 | import serial 22 | import time 23 | 24 | ser = serial.Serial('/dev/ttyS0',115200,timeout=1) 25 | ser.flushOutput() 26 | 27 | print 'Serial connected' 28 | 29 | ser.write(chr(13)) 30 | ser.write(chr(1)) # write to Arduino to turn ON the LED 31 | r = ser.read(7) 32 | 33 | print r 34 | 35 | time.sleep(1) # delay for 1 second 36 | 37 | ser.write(chr(13)) 38 | ser.write(chr(0)) # write to Arduino to turn OFF the LED 39 | r = ser.read(7) 40 | 41 | print r 42 | 43 | time.sleep(1) # delay for 1 second 44 | -------------------------------------------------------------------------------- /python/python_serial_example_bidirectional_threads.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | import threading 4 | 5 | class SerialPortManager: 6 | 7 | def __init__(self): 8 | self.port = serial.Serial('/dev/ttyMCC',115200,timeout=1) 9 | 10 | def write(self, message): 11 | print("Writing data to serial port: " + message) 12 | self.port.write(message) 13 | self.port.flushOutput() 14 | 15 | def readx(self): 16 | return self.port.readline() 17 | 18 | def close(self): 19 | return self.port.close() 20 | 21 | 22 | 23 | runSerialThread = True 24 | serialMgr = SerialPortManager() 25 | 26 | def read_from_port(serial_port): 27 | while runSerialThread: 28 | line = serial_port.readx() 29 | if line: 30 | print("Read from serial port: " + line) 31 | serial_port.close() 32 | 33 | 34 | serialthread = threading.Thread(target=read_from_port, args=(serialMgr,)) 35 | serialthread.daemon = True 36 | serialthread.start() 37 | print("Serial thread started. Press Ctrl+C to close it.") 38 | 39 | serialMgr.write("aaaa" + chr(13)) 40 | 41 | while True: 42 | try: 43 | time.sleep(1) 44 | except KeyboardInterrupt: 45 | print("Closing the connection") 46 | runSerialThread = False 47 | serialthread.join() 48 | exit(0) 49 | --------------------------------------------------------------------------------