Forum

USB Issue with Linu...
 
Notifications
Clear all

USB Issue with Linux

1 Posts
1 Users
0 Reactions
114 Views
Forum 1
(@elegresl)
Active Member
Joined: 6 months ago
Posts: 3
Topic starter  

Hi,

I have been using Ubuntu 22.04 to control the solo uno v2 with USB.

The Python library works fine.

When I use code I wrote using the libserial library OR a terminal, most times I can't write more than one byte.

Sometimes, I am able to send all 10 and the SOLO Uno responds as I expect.

 

Has anyone had an issue where some data gets held up when flushing the write buffers?

I tried removing the flusing and adding delays but the SOLO didn't interpret the command correctly.

/**
 *      to compile, execute 'g++ -I/usr/include/libserial testing_serial.cpp -lserial -o testing_serial' in the shell 
 */

#include </usr/include/libserial/SerialPort.h>
#include <cstdlib>
#include <fstream>
#include <iostream>

#include <chrono>
#include <thread>
#include <sstream>

constexpr const char* const SERIAL_PORT_2 = "/dev/ttyACM0" ;
int main()
{   
    using namespace LibSerial ;
    // Instantiate a SerialPort object.
    SerialPort serial_port ;

    try
    {
        serial_port.Open(SERIAL_PORT_2) ;
    }
    catch (const OpenFailed&)
    {
        std::cerr << "The serial port did not open correctly." << std::endl ;
        return EXIT_FAILURE ;
    }
    serial_port.FlushIOBuffers();
    serial_port.FlushInputBuffer();
    serial_port.FlushOutputBuffer();

    // Set the baud rate of the serial port.
    serial_port.SetBaudRate(BaudRate::BAUD_115200) ;

    // Set the number of data bits.
    serial_port.SetCharacterSize(CharacterSize::CHAR_SIZE_8) ;

    // Turn off hardware flow control.
    serial_port.SetFlowControl(FlowControl::FLOW_CONTROL_NONE) ;

  
    serial_port.SetParity(Parity::PARITY_NONE) ;  // Disable parity.
    
    // Set the number of stop bits.
    serial_port.SetStopBits(StopBits::STOP_BITS_1) ;

    // Read characters from the input file and write them to the serial port. 
    std::cout << "Writing data to the serial port." << std::endl ;

        std::ofstream outputFile("ascii.txt");

        //char data_string[] = {'\xFF', '\xFF', '\x00','\x15','\x00','\x00','\x00','\x01','\x00','\xFE'};
        //char data_byte = data_string[0];
        
        char data_byte = 0xFF;
        serial_port.WriteByte(data_byte);
        serial_port.DrainWriteBuffer();
        std::cout << "First byte written." << std::endl ;

        //data_byte = data_string[1];
        data_byte = 0xFF;
        serial_port.WriteByte(data_byte);  
        serial_port.DrainWriteBuffer() ;
        std::cout << "Second byte written" << std::endl ;

          //data_byte = data_string[2];
          data_byte = 0x01;
          serial_port.WriteByte(data_byte);
         serial_port.DrainWriteBuffer() ;
         std::cout << "Third byte written" << std::endl ;

          //data_byte = data_string[3];
          data_byte = 0x81;
          serial_port.WriteByte(data_byte);
         serial_port.DrainWriteBuffer() ;
         std::cout << "4th byte written" << std::endl ;

        //data_byte = data_string[4];
        data_byte = 0x00;
        serial_port.WriteByte(data_byte);
        serial_port.DrainWriteBuffer() ;
        std::cout << "5th byte written" << std::endl ;

          //data_byte = data_string[5];
          data_byte = 0x00;
          serial_port.WriteByte(data_byte);
         serial_port.DrainWriteBuffer() ;
         std::cout << "6th byte written" << std::endl ;

          //data_byte = data_string[6];
          data_byte = 0x00;
          serial_port.WriteByte(data_byte);
         serial_port.DrainWriteBuffer() ;
        std::cout << "7th byte written" << std::endl ;

          //data_byte = data_string[7];
          data_byte = 0x00;
          serial_port.WriteByte(data_byte);
         serial_port.DrainWriteBuffer() ;
         std::cout << "8th byte written" << std::endl;

         //data_byte = data_string[8];
         data_byte = 0x00;
         serial_port.WriteByte(data_byte);
         serial_port.DrainWriteBuffer() ;
         std::cout << "9th byte written" << std::endl;

        //data_byte = data_string[9];
        data_byte = 0xFE;
        serial_port.WriteByte(data_byte);
        serial_port.DrainWriteBuffer() ;
        std::cout << "10th byte written" << std::endl;

        std::string reading;
        serial_port.Read(reading, 10, 5000);

        std::stringstream ss;
        ss << reading;
    
        std::cout << ss.str() << std::endl;
    
        outputFile << ss.str();
        outputFile.close();

        serial_port.Close();
    return EXIT_SUCCESS ;
}

   
Quote
Topic Tags
Share: