Pages: [1]   Go Down
Author Topic: Arduino controled form PC(problems withc sending certian values)  (Read 748 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone,
I just stared my first project with arduino. It's a simple "car model" controlled form PC via USB cable. I decided to create my own app to do it.
After long nights of searching some libraries(i forgot to look at arduino page..) and info about serial communication i created this simple test app.
But i have some problem with it.How i can send a specific signal/value to arduino. I mean in my app i want send int value 1, but arduino don't recognise it as 1.
Here is my app code,it's Visual Studio 2010 MFC dialog based
Code:
void Ctest_2_mfcDlg::OnBnClickedButton2()
{
port_n=_T("com9");
port.Open(port_n);
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT1);
if(port.IsConnected())
{
info=_T("Connetion->OK");
}
else
info=_T("Error");
pEdit->SetWindowText(info);
}


void Ctest_2_mfcDlg::OnBnClickedCancel()
{
CDialogEx::OnCancel();
}


void Ctest_2_mfcDlg::OnBnClickedButton1()
{
if(port.IsConnected())
{
int data=1;
port.WriteData(&data,sizeof(data));
if(port.WriteData(&data,sizeof(data)))
info=_T("Signal sent");
else
info=_T("Error");
}
else
info=_T("Error");
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT2);
pEdit->SetWindowText(info);
}


void Ctest_2_mfcDlg::OnBnClickedButton3()
{
if(port.IsConnected())
{
int data=0;
port.WriteData(&data,sizeof(data));
if(port.WriteData(&data,sizeof(data)))
info=_T("Signal sent");
else
info=_T("Error");
}
else
info=_T("Error");
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT2);
pEdit->SetWindowText(info);
}


void Ctest_2_mfcDlg::OnBnClickedButton4()
{
int *odcz=0;
port.ReadData(odcz,10);
info.Format("%d",0,odcz);
CEdit* pEdit=(CEdit*)GetDlgItem(IDC_EDIT3);
pEdit->SetWindowText(info);
}
and test arduino scetch:
Code:
int ledG=2;
int syg=1;
void setup()
{
  pinMode(ledG,OUTPUT);
}
void loop()
{
 Serial.begin(9600);
 if(Serial.available()>0)
 {
 
   syg=Serial.read();
   switch(syg)
   {
     case '1':
       digitalWrite(ledG,HIGH);
     break;
     case '0':
        digitalWrite(ledG,LOW);
     break;
   }
  }
}
in MFC i posted only buttons code, if i miss something important please write it and i'll post it.
I forgot to add i use lib form arduino pages but i modify it a little.
Logged

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

Quote
Here is my app code,it's Visual Studio 2010 MFC dialog based
Why? Visual Studio 2010 supports C# which is so much easier to use to design Windows forms based applications AND to send and receive serial data. MFC deserves to be buried deep in a toxic landfill.

Nothing in the C++ code tells us exactly what the port instance is an instance of. So, it is impossible to know what the port.WriteData() method is doing. The fact that the method takes two arguments, a pointer to the data to write, and the number of bytes that the data occupies, implies that the value is written as a stream of bytes. Since an int on the PC is 4 bytes, there will be 4 bytes written to the serial port.

Quote
I mean in my app i want send int value 1, but arduino don't recognise it as 1.
You mean that the Arduino does not recognize it as a '1', which is a lot different than a 1.

The code on the Arduino is expecting the data to be written as a string of characters. I would guess that this is NOT happening in the PC code.

Quote
I forgot to add i use lib form arduino pages but i modify it a little.
Which library is that?

Logged

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

I used MFC because my teacher requires it(i will hand over this app to mark).Indeed i didn't used string in code i will fix this and try to run again.
link to header file and source:http://www.arduino.cc/playground/Interfacing/CPPWindows
Logged

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

Quote
I used MFC because my teacher requires it
Bummer.

Quote
link to header file and source
Quote
but i modify it a little.
How did you modify the code?

You can use sprintf to convert a variable containing any type of value to a string, and then send that string (using the original library). You'll need to modify the Arduino code to handle more than 1 digit numbers, though.

The string that is sent should contain start-of packet and end-of-packet markers, to make parsing easier/more robust. (Send something like "<1>", instead of 1. Search the forum for "started && ended" for code to collect and parse marker-delimited strings.)
Logged

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

I fixed this problem. As you said arduino was excepting a string, and i send data as int,i changed it and all is OK.
i changed only type of buffer i read and write method, and also constructor. if you want i can post it
Logged

Pages: [1]   Go Up
Jump to: