Pages: [1]   Go Down
Author Topic: interfacing w/ C++ (Win XP), using Serial library CPPWindows from Playground  (Read 766 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I am using this library, http://arduino.cc/playground/Interfacing/CPPWindows, for controlling USB port (virtual COM3).

I can send data from my c++ application towards arduino, but I can not read what arduino is sending to PC. Using the serial monitor from arduino software, everything works fine, but not in my c++ app. My application is a Visual C++ 6.0 MFC Application.

arduino is using this code:
         if (Serial.available()){
          temp = Serial.read();
          Serial.println(temp);
        }

my c++ app is using the exact code from the library in the link above. this is how I use the library in my c++ application:

I made a class Serial, use an instance of this class,

Serial mySerial("COM3");

// if a button on my application is clicked, it should send a char to arduino, and monitor what arduino is sending back (first it sends the same character back, as in the code above, later it will send results from some sensors on arduino).

   char a = 'W';

   if (mySerial.IsConnected()){          
      mySerial.WriteData(&a,1);  // WriteData works, arduino starts doing what it should if char W received
   }

   char *temp = new char();
   int x = mySerial.ReadData(temp,1);
      
   if(mySerial.IsConnected())
   {
      if(x > 0)
      {
         m_ListBox2.AddString(temp);
      }
      else
      {
         m_ListBox2.AddString("data not read");
      }
   }
   else
   {
      m_ListBox2.AddString("not connected"); // this never executes
   }

PROBLEM is that it sends only giberish char...

SO, I think that I have a problem of understanding how this library works, probably I don't use the functions how I should... or my C code is wrong. Or both
« Last Edit: June 13, 2012, 04:17:52 pm by vali196 » Logged

New Zealand
Offline Offline
Jr. Member
**
Karma: 4
Posts: 99
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't see code setting the speed of the serial connection on the PC side. If you don't set it to the same speed as the speed defined in your Arduino sketch, you will get gibberish. Even the Arduino IDE Serial monitor gives gibberish if the speed is incorrect.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your answer.

on the arduino side I use 9600 ... in setup() I have... Serial.begin(9600);

on the PC side, I haven't changed the code in the library, and in the Serial Library .cpp I can see the following code that I suppose it sets baud rate to 9600 also:

//Define serial connection parameters for the arduino board
            dcbSerialParams.BaudRate=CBR_9600;
            dcbSerialParams.ByteSize=8;
            dcbSerialParams.StopBits=ONESTOPBIT;
            dcbSerialParams.Parity=NOPARITY;

doesn't this code resolve this issue ?

thanks,

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   char *temp = new char();
   int x = mySerial.ReadData(temp,1);
     
   if(mySerial.IsConnected())
   {
Read a character, and then test if you are connected. Does that order really make sense?
Logged

Pages: [1]   Go Up
Jump to: