Society of Robots - Robot Forum

Software => Software => Topic started by: benji on November 26, 2007, 11:12:00 AM

Title: have C++ serial code?
Post by: benji on November 26, 2007, 11:12:00 AM
hey folks, anyone knows a library in c++ that has functions to send recieve via the serial port?
Title: have C++ serial code?
Post by: bens on November 26, 2007, 12:41:01 PM
I know Microsoft's .Net framework has built-in support for serial ports, so if you're writing a computer-side application that sends data over the serial port I'd recommend Microsoft C++/C#/VB 2005 (it's free and the serial port interface is quite easy to use).
Title: Re: have C++ serial code?
Post by: benji on November 26, 2007, 01:33:41 PM
yes bens im using microsoft visual c++ ,,but i dont know the library and functions for sending recieving via the serial port,, what are they?
Title: Re: have C++ serial code?
Post by: bens on November 26, 2007, 03:01:36 PM
I've only done serial port stuff using C# and VB, but I expect it should be almost the same for visual C++.  In C# (and maybe in C++), you would do the following:

System.IO.Ports.SerialPort port = new System.IO.Ports.SerialPort("COM1", 19200);
port.Open();
port.Write(new Byte[] { 128, 2, 45, 254 }, 0, 4);  // array of bytes, array index of first byte to send, number of bytes to send
port.Close();

To read from the serial port you can either do

Byte[] buffer = new Byte[255];
int bytesToRead = 5;
port.Read(buffer, 0, bytesToRead);

or

int byte;
while (port.BytesToRead > 0)
{
  byte = port.ReadByte();
  ...
}
Title: Re: have C++ serial code?
Post by: benji on November 27, 2007, 11:28:50 AM
u sure yiou dont have to include anythin here??? what files?
Title: Re: have C++ serial code?
Post by: bens on November 27, 2007, 01:25:12 PM
It's part of .Net 2.0 under the System.IO.Ports namespace.  In C# you don't have to include anything, but in C++ you might need to include System.IO.Ports.h or something.  Try using your visual C++'s help feature and search for serial port (or just write your program in C#, which is almost the same syntactically as C++ but I like a lot better).  Make sure you're using Visual xx 2005.
Title: Re: have C++ serial code?
Post by: Kohanbash on November 27, 2007, 10:45:49 PM
Hi
You probably want to use termios.h
This lets you configure the port.
Then use can declare the device as a file and use standard write and read commands.
Title: Re: have C++ serial code?
Post by: benji on November 28, 2007, 06:11:50 AM
Quote
You probably want to use termios.h
yea but what are the functions? shouldnt there be also a .c file to include?
Title: Re: have C++ serial code?
Post by: Kohanbash on November 29, 2007, 08:36:28 AM
Why do you need a .c file?
Look for examples online as there are a lot of configurations to use it.
Title: Re: have C++ serial code?
Post by: benji on December 05, 2007, 05:27:40 PM
oh yea right but what are the functions included in this library< termios.h>?
Title: Re: have C++ serial code?
Post by: paulstreats on December 05, 2007, 09:05:40 PM
open it and find out
Title: Re: have C++ serial code?
Post by: neo01124 on December 10, 2007, 09:26:17 AM
(http://img134.imageshack.us/img134/7629/tccj7.th.jpg) (http://img134.imageshack.us/my.php?image=tccj7.jpg)
this is some simple code for serial communication!!!
outportb(port address,value);
this is the syntax and address for parallel port (LPT1) is 0x378 while for COM1(serial port) is 0x3f8 this can be checkd from device manager->ports->details
Title: Re: have C++ serial code?
Post by: reSpawn on December 11, 2007, 07:42:00 AM
Hello,
I'm using Qt with http://qextserialport.sourceforge.net/ QextSerialPort and it works well.