Go Down

Topic: Libusb help (Read 14325 times) previous topic - next topic

Robin2


can you give me sth to read, so i can make  things better?


I gave you a link to a working example in Reply #18.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mdimiou

Hello again... i made a new code in Linux in C using libserial...here is my code:

#include <SerialStream.h>
#include <SerialStreamBuf.h>
#include <SerialPort.h>
#include <string>
#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;
using namespace LibSerial;


     
int main(){
   
    char end[4] = { 0 };
    int reply;
    int f=0;
     SerialStream arduino;
     arduino.Open("/dev/ttyS0");
     arduino.SetBaudRate(SerialStreamBuf::BAUD_9600);
     arduino.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
     arduino.SetNumOfStopBits(1);
     arduino.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
     arduino.SetVTime(1);
     arduino.SetVMin(100);
     if(!arduino.good()){
                         f=1;
                         arduino.Open("/dev/ttyS1");
                         arduino.SetBaudRate(SerialStreamBuf::BAUD_9600);
                         arduino.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
                         arduino.SetNumOfStopBits(1);
                         arduino.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
                         arduino.SetVTime(1);
                         arduino.SetVMin(100);
                         }
     if(!arduino.good()){
                         cout<<"Arduino not connected \n";
                         
                         }
     if(f==0){
              cout<<"Arduino connected in /dev/ttyS0 port \n"<<"with: \n"<<"Baudrate:9600 \n";
        
              }
     if(f==1){
              cout<<"Arduino connected in /dev/ttyS1 port \n"<<"with: \n"<<"Baudrate:9600\n";
             
              }

    arduino<<"lights";
   
    arduino.Close();
    return 0;
   
}


I am trying to test it and i have the first msg for good connection in port /dev/ttyS1. It is ok until there but i have no response from Arduino... :/

PaulS

Quote
I am trying to test it and i have the first msg for good connection in port /dev/ttyS1. It is ok until there but i have no response from Arduino... :/

What a surprise. You open the serial port, resetting the Arduino. Before it has time to reset, you jam some data at it, and then close the serial port, causing another reset.

Where are you trying to read a response, it the Arduino were to reset instantly?
The art of getting good answers lies in asking good questions.

mdimiou

#33
Aug 13, 2014, 06:21 pm Last Edit: Aug 13, 2014, 06:26 pm by mdimiou Reason: 1
thanks for your response.... but how i can correct this?




mdimiou

i add this part of code, cause i thought it is better if i am waiting for some response from arduino....

int main(){
.
.
.
char end[4] = { 0 };
.
.
.


arduino<<"lights"<<std::endl;
   while(strcmp(end,"end")!=0){
                               arduino.read(end,4);
                               }
arduino.Close();
return 0;
}

and in Arduino code after each digital.write();
i put this command "end1=Serial.write("end");"
for example:

if (strcmp(incoming, "lights") == 0){
     Serial.println("Lights matched");
     matchFlag = 1;
     digitalWrite(Relay1, HIGH);
     end1=Serial.write("end");
   }


but this is nothing i guess...... :/

Robin2


thanks for your response.... but how i can correct this?


See the link in Reply #18. I also reminded you about that in Reply #30.

Seems like I am just wasting my time here.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mdimiou

#36
Aug 14, 2014, 05:27 pm Last Edit: Aug 14, 2014, 05:37 pm by mdimiou Reason: 1
Robin hello again, i had a break until now and i am back studying your code and trying to fix mine.... ;)


First of all i would like to ask you if you can check my code..... the first part that i am trying to open a serial port:

int main(){
   
    char end[4] = { 0 };
    char ready[6] = { 0 };
    int reply;
    int f=0;
     SerialStream arduino;
     arduino.Open("/dev/ttyS0");
     if(arduino.IsOpen()){
                         f=1;
                         arduino.SetBaudRate(SerialStreamBuf::BAUD_9600);
               arduino.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
               arduino.SetNumOfStopBits(1);
               arduino.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
               arduino.SetVTime(1);
               arduino.SetVMin(100);
                         }
     if(!arduino.IsOpen()){
                         f=2;
                         arduino.Open("/dev/ttyS1");
                         arduino.SetBaudRate(SerialStreamBuf::BAUD_9600);
                         arduino.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
                         arduino.SetNumOfStopBits(1);
                         arduino.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
                         arduino.SetVTime(1);
                         arduino.SetVMin(100);
                         }
     if(f==0){
                         cout<<"Arduino not connected \n";
                         
                         }
     if(f==1){
              cout<<"Arduino connected in /dev/ttyS0 port \n"<<"with: \n"<<"Baudrate:9600 \n";
        
              }
     if(f==2){
              cout<<"Arduino connected in /dev/ttyS1 port \n"<<"with: \n"<<"Baudrate:9600 \n";
             
              }



cause i don't think it works properly


Robin2

#37
Aug 14, 2014, 07:37 pm Last Edit: Aug 14, 2014, 07:38 pm by Robin2 Reason: 1
Please read the How to use the forum and learn how to post your code in code tags so it looks like this. It makes it so much easier to read.

I suggest you start very simply. Find out, using the Arduino IDE, which serial port the Arduino is connected to and just get your code to connect to that one. Worry about searching through various serial connections later.

I presume the code you posted is written in C. I am not familiar with the SerialStream class/library - perhaps you can post a link to its documentation.

That said, it looks correct - but I have no idea what setVTime and setVMin do.

It would be a good idea to get your code to show the error that asrises when it fails to connect.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mdimiou

i am thinking that there 's might have a problem cause i choose both options, ttyS0 and ttyS1 but in terminal when i run my program it  always opens the first written port....

mdimiou

When Rx and Tx flashing means that there is a connection with pc and arduino isn't it???
Rx->Receive
Tx->Transmit

Cause i try an other port, not /devttyS0 and /devttyS1 but /devttyACM0 and i saw that when i run my program Rx and Tx are flashing  once....and when i tried the other two ports Rx and Tx didn't flash....

Robin2

If the Rx and Tx lights on the Arduino flash that should mean the PC is sending some data and it is responding.

If you have the Arduino connected to your PC with the USB cable and if you open the Arduino IDE and look in Tools/Port what do you see?

On my Ubuntu PC I see /dev/ttyACM0  and /dev/ttyS80 - both refer to the same port.

Whatever you see on your PC, use that in your C program.

Another thing you can try (on Linux) is opening a terminal and running    dmesg    as soon as possible after connecting the USB cable to the Arduino - on my PC it shows ttyACM0.


You didn't provide any information about setVTime and setVMin or a link to the documentation for SerialStream. If you want help you must provide information and not just start off on another train of thought.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

mdimiou

#41
Aug 15, 2014, 09:04 am Last Edit: Aug 15, 2014, 09:19 am by mdimiou Reason: 1
this link give much info about VTime and VMin..... when i was searching about libserial someone had a problem reading data and after he changes those he was ok.....he gave to community this link...and i think it is very good..!!!

Robin2

OOPS!   You forgot to include the link.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.


Robin2

Code: [Select]
arduino.SetVTime(1);
               arduino.SetVMin(100);


I reckon you need to read that link again. It seems to me you have these things back to front?

What happens if you omit both of them? - I don't see, on that link, what are the default values.

Do you have a link for the full documentation for SerialStream? It is always useful to have the fundamental documentation to hand.

There are 3 questions here - please respond with 3 answers.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up