Pages: [1]   Go Down
Author Topic: [solved] Ubuntu C++ <--> Mega2560 Serial Communication  (Read 1404 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I would like to communicate with my Arduino Mega2560 under Ubuntu with C++.
I couldn't find a working code in the web.

Does anybody know, how to solve my problem?

Nathan
« Last Edit: July 24, 2013, 05:37:33 pm by Nathax » Logged

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

Quote
I couldn't find a working code in the web.
Unbelievable.

Quote
Does anybody know, how to solve my problem?
Google again. Perhaps with a less narrow search. C++ on Ubuntu, windoze, etc. is the same.
Logged

Down Under (Australia), SA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
Newbie is an understatement
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I couldn't find a working code in the web.
Unbelievable.

Quote
Does anybody know, how to solve my problem?
Google again. Perhaps with a less narrow search. C++ on Ubuntu, windoze, etc. is the same.
What's windoze? I have never heard of it.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The only code I found is up here: http://webtutsdepot.com/2010/05/01/arduino-visual-c-plus-plus-tutorial/
Maybe now you could understand why I would like a Linux tutorial, because clr isn't possible in codeblocks, afaik.
Any advises?
Logged

Offline Offline
Jr. Member
**
Karma: 5
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It really is dead easy you know!

It's as simple as this - the serial ports (both in windows and *nix) may be read using the very same interface used to read files.

So, all you need to do is call fopen with the name of the serial port device as the filename.
You then read/write from/to the 'file'. It's a 27 line program to provide a dodgy read-only 'Serial Monitor' like the one in the arduino ide.

Here's the first 11 lines of something I threw together. You'll be fine from here. If you can read from a disk file, serial ports are just as easy to get started with.
If not, research how to read from a file.
Just make sure you change the serialPortFilename to reflect the port you're actually connected to (you can see it in the bottom right corner of the arduino ide, in the status-bar)


Code:
#include <stdio.h>
#include <string.h>

char serialPortFilename[] = "/dev/ttyACM0";

int main()
{
    char readBuffer[1024];
    int numBytesRead;

    FILE *serPort = fopen(serialPortFilename, "rwb");
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 197
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Solution for reading from serial port:

Code:
#include <stdio.h>
#include <string.h>

char serialPortFilename[] = "/dev/ttyACM0";

int main()
{
    char readBuffer[1024];
    int numBytesRead;

    FILE *serPort = fopen(serialPortFilename, "r");

if (serPort == NULL)
{
printf("ERROR");
return 0;
}

printf(serialPortFilename);
printf(":\n");
while(1)
{
memset(readBuffer, 0, 1024);
fread(readBuffer, sizeof(char),1024,serPort);
if(sizeof(readBuffer) != 0)
{
printf(readBuffer);
}
}
return 0;
}

Writing to serial port:
Code:
#include <stdio.h>
#include <string.h>

char serialPortFilename[] = "COM5";

int main()
{
FILE *serPort = fopen(serialPortFilename, "w");

if (serPort == NULL)
{
printf("ERROR");
return 0;
}

char writeBuffer[] = {'1'};

fwrite(writeBuffer, sizeof(char), sizeof(writeBuffer), serPort);
fclose(serPort);
return 0;

}

Thanks for your help. smiley
« Last Edit: July 24, 2013, 05:37:14 pm by Nathax » Logged

Offline Offline
Jr. Member
**
Karma: 5
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ripper!

Learning is a lot of fun. Helping someone else to learn feels even better!
Logged

Pages: [1]   Go Up
Jump to: