Pages: [1]   Go Down
Author Topic: Does a C/C++ PC (not arduino) serial port library exist on Linux?  (Read 1240 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6914
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am having a hard time trying to talk with my Arduino on a Debian system. I am a linux noob. It seems that these unix/linux systems are holding on to their serial terminals tight. Every option in the termios struct is about using serial ports with a terminal (like VT100 or something). I seriously doubt there's such usage nowadays. Trying to use a serial port for unbuffered and un-processed data is just so hard. I just learned today that by turning off flow control, I was finally able to send bytes from Debian PC to Arduino. The Arduino -> Debian worked out of the box.

I wonder if there exists a c/c++ library for linux to run Arduino fashion Serial ports, such as Serial.begin, available, read, write. I am just hacking up code I found online and hoping it will hold together.

Here is what I found, from a member on the beagle board forum (probably also on pi forum):

https://github.com/KurtE/Raspberry_Pi/blob/master/library/WrapperSerial.cpp

I really can't understand why a FILE pointer is used and why one needs to open the serial port with open and then use the file descriptor to fopen again and only fread and fwrite with the FILE pointer instead of read and write with the file descriptor. I was using the file descriptor and it worked fine. Looks like the code disables buffering on FILE object so isn't that becoming just read and write?
Logged


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8096
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you open the device (like "/dev/usbtty") as if it were a file.  Then you can read from the 'file' and write to the 'file'.

Here is a tutorial:  http://www.tldp.org/HOWTO/Serial-HOWTO.html
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6914
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks John! I should have read on the hardware flow control in the tutorial.
Logged


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

You asked about C++ lib, but if you want to easily, quickly, seamlessly, problemfree, etc., etc to communicate with an Arduino from a host computer, one of the best way is to use Python and pySerial lib (http://pyserial.sourceforge.net/). You will find lot of examples if you google for it, as an elaborated one I can offer my arduino-hosted lib: https://github.com/pfalcon/arduino-hosted
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6914
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, I know about python library but I don't use python. I use c/c++. It is much more powerful and faster. I have had enough trouble using Processing because it is slow and won't do static methods. I should be better off learning Java but then c is always my choice of language. If there are very good python scripts I can call them with system command. I envy people with good bash shell programming skills. They can do a lot with it. But I don't think I will learn bash that much, because of that. Too old to learn a language. What I will try is to cal python code from c so if I can understand a bit of python I can make use some good python scripts.
Logged


Pistestii din Deal
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino project collection rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

See this one
http://qt-project.org/wiki/QtSerialPort
Logged

Online Online
God Member
*****
Karma: 32
Posts: 506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know if this is bad practice but if in a hurry I sometimes use system("stty ..."); instead of filling in the termios struct, eg:

Code:
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(){
int sp=open("/dev/ttyACM0",O_RDWR);
system("stty -F /dev/ttyACM0 sane raw -echo 115200");
sleep(2);

unsigned char c;
for(c=0;c<255;c++) write(sp,&c,1);
while(read(sp,&c,1)) printf("got %d\n",c);
}
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6914
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Great! Thanks. I'll give both a try.
Logged


Pages: [1]   Go Up
Jump to: