Author Topic: have C++ serial code?  (Read 5274 times)

0 Members and 1 Guest are viewing this topic.

Offline benjiTopic starter

  • Supreme Robot
  • *****
  • Posts: 830
  • Helpful? 0
have C++ serial code?
« 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?
good ol' BeNNy

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 335
  • Helpful? 3
have C++ serial code?
« Reply #1 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).

Offline benjiTopic starter

  • Supreme Robot
  • *****
  • Posts: 830
  • Helpful? 0
Re: have C++ serial code?
« Reply #2 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?
good ol' BeNNy

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 335
  • Helpful? 3
Re: have C++ serial code?
« Reply #3 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();
  ...
}
« Last Edit: November 26, 2007, 03:07:05 PM by bens »

Offline benjiTopic starter

  • Supreme Robot
  • *****
  • Posts: 830
  • Helpful? 0
Re: have C++ serial code?
« Reply #4 on: November 27, 2007, 11:28:50 AM »
u sure yiou dont have to include anythin here??? what files?
good ol' BeNNy

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 335
  • Helpful? 3
Re: have C++ serial code?
« Reply #5 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.
« Last Edit: November 27, 2007, 02:13:42 PM by bens »

Offline Kohanbash

  • Supreme Robot
  • *****
  • Posts: 430
  • Helpful? 1
Re: have C++ serial code?
« Reply #6 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.
Robots for Roboticists Blog - http://robotsforroboticists.com/

Offline benjiTopic starter

  • Supreme Robot
  • *****
  • Posts: 830
  • Helpful? 0
Re: have C++ serial code?
« Reply #7 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?
good ol' BeNNy

Offline Kohanbash

  • Supreme Robot
  • *****
  • Posts: 430
  • Helpful? 1
Re: have C++ serial code?
« Reply #8 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.
Robots for Roboticists Blog - http://robotsforroboticists.com/

Offline benjiTopic starter

  • Supreme Robot
  • *****
  • Posts: 830
  • Helpful? 0
Re: have C++ serial code?
« Reply #9 on: December 05, 2007, 05:27:40 PM »
oh yea right but what are the functions included in this library< termios.h>?
good ol' BeNNy

paulstreats

  • Guest
Re: have C++ serial code?
« Reply #10 on: December 05, 2007, 09:05:40 PM »
open it and find out

Offline neo01124

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: have C++ serial code?
« Reply #11 on: December 10, 2007, 09:26:17 AM »

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

Offline reSpawn

  • Full Member
  • ***
  • Posts: 56
  • Helpful? 0
Re: have C++ serial code?
« Reply #12 on: December 11, 2007, 07:42:00 AM »
Hello,
I'm using Qt with http://qextserialport.sourceforge.net/ QextSerialPort and it works well.

 


data_list