Skip to content

Commit ec4d77c

Browse files
authored
Merge pull request #1 from LJ3D/SerialCopyTimeout
Serial copy timeout into main
2 parents a54597f + cb035aa commit ec4d77c

File tree

7 files changed

+586
-154
lines changed

7 files changed

+586
-154
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
main.cpp
1+
22
*.out
3-
arduinoCode/
43
stuff/
54
.vscode/settings.json

arduinoCode/arduinoCode.ino

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// C++ code
2+
//
3+
4+
void setup() {
5+
pinMode(LED_BUILTIN, OUTPUT);
6+
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
7+
}
8+
9+
void loop() {
10+
Serial.print(".32.23.21.sex");
11+
delay(1000);
12+
}

arduinoSerial.cpp

+441
Large diffs are not rendered by default.

arduinoSerial.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <string>
2+
#include <vector>
3+
#include <array>
4+
#include <termios.h>
5+
6+
/*
7+
* ArduinoSerial
8+
*
9+
* This class enables communication with an Arduino over a serial port.
10+
* It is based on the Arduino Serial class, but is not a direct copy.
11+
* Only works on good operating systems.
12+
* This version implements timeout functionality.
13+
*
14+
* Features which will not be implemented (in this version):
15+
* - AvailableForWrite()
16+
* - Peek()
17+
*
18+
* Notes:
19+
* - The configuration of the serial port may not be correct.
20+
* - Flush is set up to behave as it did prior to arduino 1.0, it removes incoming data.
21+
*/
22+
23+
24+
class arduinoSerial {
25+
private:
26+
int fd; // The file descriptor for the serial port (Usually /dev/ttyACM0)
27+
std::string ttyName; // The name of the serial port (Usually /dev/ttyACM0). This string exists mainly for debugging purposes
28+
bool debug; // If true, debug messages will be printed to the console
29+
// Every single baud rate defined in termios.h or termios-baud.h:
30+
const std::array<unsigned long, 31> acceptableBaudRates = {B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000};
31+
unsigned long timeout = 1000; // The timeout for read operations. This is in milliseconds. Default is 1000ms (1 second)
32+
public:
33+
arduinoSerial(std::string port, bool debug=false);
34+
~arduinoSerial();
35+
unsigned int available();
36+
//unsigned int availableForWrite();
37+
void begin(unsigned long baudRate);
38+
void end();
39+
bool find(char target);
40+
bool find(std::string targetStr);
41+
bool findUntil(char target, char terminator);
42+
bool findUntil(std::string targetStr, char terminator);
43+
void flush();
44+
float parseFloat();
45+
long parseInt();
46+
//int peek();
47+
void print(std::string str);
48+
void print(char c);
49+
void print(int num);
50+
void print(float num);
51+
void println(std::string str);
52+
void println(char c);
53+
void println(int num);
54+
void println(float num);
55+
int read_s(); // read_s() is used instead of read() because read() is already taken by the C library. >:(
56+
size_t readBytes(char *buffer, size_t length);
57+
size_t readBytesUntil(char terminator, char *buffer, size_t length);
58+
std::string readString();
59+
std::string readStringUntil(char terminator);
60+
void setTimeout(unsigned long timeout);
61+
size_t write_s(char byte); // Named like read_s for the same reason
62+
size_t write_s(char *buffer, size_t size);
63+
};

arduinoSerialIO.hpp

-152
This file was deleted.

distanceMeasure/distanceMeasure.ino

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Ultrasonic sensor Pins:
3+
VCC: +5VDC
4+
Trig : Trigger (INPUT) - Pin11
5+
Echo: Echo (OUTPUT) - Pin 12
6+
GND: GND
7+
*/
8+
9+
#define trigPin 11
10+
#define echoPin 12
11+
12+
long duration;
13+
int cm;
14+
15+
void setup() {
16+
//Serial Port begin
17+
Serial.begin (9600);
18+
//Define inputs and outputs
19+
pinMode(trigPin, OUTPUT);
20+
pinMode(echoPin, INPUT);
21+
pinMode(LED_BUILTIN, OUTPUT);
22+
digitalWrite(LED_BUILTIN, LOW);
23+
}
24+
25+
26+
void measureDistance(){
27+
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
28+
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
29+
digitalWrite(trigPin, LOW);
30+
delayMicroseconds(5);
31+
digitalWrite(trigPin, HIGH);
32+
delayMicroseconds(10);
33+
digitalWrite(trigPin, LOW);
34+
35+
// Read the signal from the sensor: a HIGH pulse whose
36+
// duration is the time (in microseconds) from the sending
37+
// of the ping to the reception of its echo off of an object.
38+
duration = pulseIn(echoPin, HIGH);
39+
40+
// Convert the time into a distance
41+
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
42+
Serial.println(cm);
43+
}
44+
45+
void loop() {
46+
measureDistance();
47+
delay(50);
48+
}

main.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <chrono>
2+
#include <thread>
3+
4+
#include <iostream>
5+
#include <string>
6+
7+
#include "arduinoSerial.h"
8+
9+
int main(){
10+
arduinoSerial serial("/dev/ttyACM0", false);
11+
serial.begin(B9600);
12+
serial.setTimeout(100000);
13+
while(true){
14+
if(serial.available() > 0){
15+
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // Wait for all the data to come in (small numbers shouldnt take more than like 10ms probably)
16+
std::cout << serial.parseInt() << std::endl;
17+
serial.flush();
18+
}
19+
}
20+
return 0;
21+
}

0 commit comments

Comments
 (0)