Advertisement
Guest User

c++ Serial Data

a guest
Apr 5th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.41 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<string>
  4. #include<iostream>
  5. #include<thread>
  6.  
  7. #include<fcntl.h>
  8. #include<errno.h>
  9. #include<termios.h>
  10. #include<unistd.h>
  11. #include<fstream>
  12.  
  13. int main(){
  14.     int serial_port = open("/dev/ttyACM0", O_RDWR);
  15.  
  16.     struct termios tty;
  17.     memset(&tty, 0, sizeof(tty));
  18.  
  19.     if(tcgetattr(serial_port, &tty) != 0){
  20.         printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
  21.     }
  22.    
  23.     tty.c_cflag &= ~PARENB;
  24.     tty.c_cflag &= ~CSTOP;
  25.     tty.c_cflag |= CS8;
  26.     tty.c_cflag &= ~CRTSCTS;
  27.     tty.c_cflag |= CREAD | CLOCAL;
  28.    
  29.     tty.c_lflag &= ~ICANON;
  30.     tty.c_lflag &= ~ECHO; // Disable echo
  31.     tty.c_lflag &= ~ECHOE; // Disable erasure
  32.     tty.c_lflag &= ~ECHONL; // Disable new-line echo
  33.     tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
  34.     tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
  35.     tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
  36.  
  37.     tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
  38.     tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
  39.     tty.c_cc[VTIME] = 10;    // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
  40.     tty.c_cc[VMIN] = 0;
  41.  
  42.     // Set in/out baud rate to be 9600
  43.     cfsetispeed(&tty, B9600);
  44.     cfsetospeed(&tty, B9600);
  45.  
  46.     // Save tty settings, also checking for error
  47.     if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
  48.         printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
  49.     }
  50.     char read_buf [1];
  51.     memset(&read_buf, '\0', sizeof(read_buf));
  52.     // Strings to Hold the data
  53.     std::string steeringAngle;
  54.     std::string brakePosition;
  55.     std::string gasPosition;
  56.     std::string serialData;
  57.     std::fstream myFile;
  58.  
  59.     //std::this_thread::sleep_for(std::chrono::milliseconds(5000));  // Wait time for cameras to start.
  60.  
  61.     bool readEnable = false;
  62.     int iters = 0;
  63.         while(true){
  64.             //memset(&read_buf, '\0', sizeof(read_buf));
  65.             // Read bytes. The behaviour of read() (e.g. does it block?,
  66.             // how long does it block for?) depends on the configuration
  67.             // settings above, specifically VMIN and VTIME
  68.             int num_bytes = read(serial_port, &read_buf, sizeof(read_buf));
  69.  
  70.             // n is the number of bytes read. n may be 0 if no bytes were received, and can also be -1 to signal an error.
  71.             if (num_bytes < 0) {
  72.                 printf("Error reading: %s", strerror(errno));
  73.             }
  74.             if (read_buf[0] == 'I') {
  75.                 serialData += read_buf[0];
  76.                 readEnable = true;
  77.             }
  78.            
  79.             while (readEnable) {
  80.                 num_bytes = read(serial_port, &read_buf, sizeof(read_buf));
  81.                 if (num_bytes <= 0){
  82.                     continue;
  83.                 }
  84.                 if (read_buf[0] == '\0') {
  85.                     readEnable = false;
  86.                     break;
  87.                 }
  88.                 serialData += read_buf[0];
  89.             }
  90.            
  91.            
  92.             // Grabs correct can bus ID and save data
  93.             if (serialData.find("0x156") != std::string::npos && steeringAngle.empty()) {
  94.                 std::cout << serialData << std::endl;
  95.                 steeringAngle = serialData;
  96.                 serialData.clear();
  97.             }
  98.             else if (serialData.find("0x17C") != std::string::npos && gasPosition.empty()) {
  99.                 std::cout << serialData << std::endl;
  100.                 gasPosition = serialData;
  101.                 serialData.clear();
  102.             }
  103.             else if (serialData.find("0x1A4") != std::string::npos && brakePosition.empty()) {
  104.                 std::cout << serialData << std::endl;
  105.                 brakePosition = serialData;
  106.                 serialData.clear();
  107.             }
  108.  
  109.         // Checks if all three can bus ID strings are empty
  110.         // Need all three filled before the program continues.
  111.         //Saves to file
  112.         if ((!steeringAngle.empty() && steeringAngle.length() > 1) &&
  113.         (!gasPosition.empty() && gasPosition.length() > 1) &&
  114.         (!brakePosition.empty() && brakePosition.length() > 1)) {
  115.            
  116.             iters++;
  117.  
  118.             myFile.open("carData.txt", std::ios_base::app);
  119.             std::string tempData = steeringAngle + ";" +
  120.                                     gasPosition + ";" +
  121.                                     brakePosition + '\n';
  122.             myFile << tempData;
  123.  
  124.            
  125.             myFile.close();
  126.            
  127.             tempData.clear();
  128.            
  129.             steeringAngle.clear();
  130.             gasPosition.clear();
  131.             brakePosition.clear();
  132.            
  133.            
  134.             // Tells threads to continue
  135.             //enabled = true;
  136.             //suspend_cv.notify_all();
  137.             //std::this_thread::sleep_for(std::chrono::milliseconds(300));
  138.  
  139.  
  140.            
  141.         }
  142.         if (iters == 10) {
  143.             std::this_thread::sleep_for(std::chrono::milliseconds(100));  // Lets all cams catch up if other cams are ahead.
  144.             std::cout << "COMPLETE!!!" << std::endl;
  145.             break;
  146.         }
  147.     }
  148.     close(serial_port);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement