Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <unistd.h> //read function
- #include <string.h>
- #include <wiringSerial.h> //simple serial port library
- using namespace std;
- /*
- * sudo apt-get install libwiringpi-dev
- * This code is working on a x86_64 machine, not a raspberry pi.
- *
- * compiled with g++ -Wall -o readSerial readSerial.cpp -lwiringPi
- *
- */
- int main(int argc, char ** argv)
- {
- const char *SensorPort = "/dev/ttyACM0"; //Serial Device Address
- int levelSensor = serialOpen(SensorPort, 9600);
- //serialPuts(levelSensor, "1"); //Send command to the serial device
- while (1){
- char buffer[100];
- ssize_t length = read(levelSensor, &buffer, sizeof(buffer));
- if (length == -1){
- cerr << "Error reading from serial port" << endl;
- break;
- }
- else if (length == 0){
- cerr << "No more data" << endl;
- break;
- }else{
- buffer[length] = '\0';
- cout << buffer; //Read serial data
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement