View Full Version : Reading in a serial port (C++)
soper87
03-11-2010, 01:53 PM
I have created a GUI with a list box containing a list (funnily enough) of serial ports that are connected to the PC. The aim is that the user selects a port from the list and the application starts to read data in from that port and display in the text box on the GUI.
I am struggling with getting the application to properly allow me to select a port from the list and then read in from the selected port, any help on this greatly appreciated. Sorry it's a bit vague, not the best at trying to describe my issues in words
BrickInTheWall
03-11-2010, 04:53 PM
Not sure if it's still up to date, but there is a header file in ANSI C called bios.h I believe. This has the bioscom function that allows for serial communication. There are probably a few documentations with examples on the net, so I'd search for those.
BillTheBuilder
03-14-2010, 07:19 PM
What compiler are you using for the RS232 code.
I have two apps that use this interface using the following compilers and IDE's:
1 Borland C++ Builder.
2 Visual Basic. (Visual Studio)
Both Apps incorporate a GUI and both work.
The only problem appears to be getting them to you. I hate making my email address public. I get enough garbage as it is.
ffmast
03-15-2010, 05:46 AM
When I have to do something alike, I just show box like:
[label:COM] [textBOX]
User only enters number of a COM (for example 4)
Then I
fullComName = strcat("COM",comNum)
And then you can run
fd = open(fullComName...)
on it
Then go into loop: "read(fd,......)" from serial , and write into textBox.
--
If you were using advanced interface, like C# - you had a serial control, that you can use, which is much more simple.
soper87
03-15-2010, 09:27 AM
BillTheBuilder: I am using Visual C++ (2005 Visual Studio) for trying to do this. Not sure if you want to send me anything or not, entirely up to you but can PM you my e-mail address if you want.
ffmast: A text box input may be easier, I have been trying to do it using a list box at the moment but I have been getting a few errors when trying to get it to read in the item selected.
Cheers
Chris
BillTheBuilder
03-15-2010, 07:25 PM
ffmast
VS C++ and VB both contain the Serial control.
Soper87
I use VS2005 as well.
The App contains 2 forms:
The Main Form contains the input/output, The output is a TextBox and the input is a MultiLine TextBox.
The secondary Form containds the RS232 settings.
Five ComboBoxes are used to select the Com Port,Baud Rate, Parity, Data Bits and Stop Bits.
The only snag is that its been done in VB, but there is not a lot of code in it, so it should be easy to convert.
If you want a copy then by all means use PM and I will email it to you.
I will also sort some other stuff to send you regarding the RS232 inteface.
Just out of curiosity what have you got hung on the other end of the RS232?
ffmast
03-15-2010, 09:14 PM
soper87:
How are you reading the input? Put a messagebox/breakpoint, to make sure ,that you are setting the parameters, that you selected.
You should take the value from listbox in a way, that looks like:
comPort = lstComPorts.Items[lstComPorts.SelectedIndex].ToString();
PS. thats a c# way, not really sure how it looks in VB :(
BillTheBuilder
03-15-2010, 11:05 PM
Soper87
The following gives a bare skeleton of one of my RS232 applications in BC++B.
As you can see I populate a ComboBox to select the ComPort.
void __fastcall TForm1::btnPortsClick(TObject *Sender)
{
ComPorts = new TStringList();
for(int i = 1; i <= 255; i++)
{
HANDLE h;
h = CreateFile(ComPortFile(i).c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, NULL);
if(h != INVALID_HANDLE_VALUE )
{
ComPorts->Add(ComPortString(i));
CloseHandle(h);
}
}
ComboBox1->Text = " Available Com Ports";
ComboBox1->Items = ComPorts;
delete ComPorts;
}
//------------------------------------------------------------------------
String __fastcall TForm1::ComPortString(int portnumber)
{
String sName = "COM";;
String CommName = sName + IntToStr(portnumber);
return CommName;
}
//------------------------------------------------------------------------
String __fastcall TForm1::ComPortFile(int portnumber)
{
String CommFile = String("\\\\.\\COM") + String(portnumber);
return CommFile;
}
//------------------------------------------------------------------------
In a later function I open a Port the following two lines are included.
Port->SetCommPort(ComboBox1->Text.c_str());
Port->OpenCommPort();
Where Port is the class name. I include an RS232 Port class 'header file' with any RS232 App.
Notes:
1 If a comms port is already open then it is not included in the List of Ports.
2 If you are using: CreateFile(,,,,,,,,); to Read/Write to a port then you need to convert your String to a char.
3 c_str() is a C++ function that converts a String to a Null terminated character (char).
soper87
03-16-2010, 09:02 AM
Thanks for all the information guys, been a good help :thumbsup:. I have ploughed on with C++ and switched to using a command prompt input to make it easier to start with. It now asks the user to enter the com port they want to connect to and to name a log file to store the information being read in to.
It appears to open a connection to the requested port, I'm now just working on getting it to read in the information and store it into the named log file.
Cheers
Chris
soper87
03-16-2010, 02:32 PM
Appear to have got it working so thanks to everyone for the help on this
Now to start another thread to iron out converting from BCD to Hex as it isn't 100% happy with what I have given it :(
Chris
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.