473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using RS232 serial port

Hi,

Can anyone help me, please?

I have to read from some device through a serial port.
I am writing a program in C and I have to set baud rate 2400,
transmit bits 8, parity none, stop bits 1 or 2.

I used some open source code to do this but I can't link termsio.h.
How can solve this problem?

Nov 17 '05 #1
8 2457
"tacoBell" <ta******@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
Yeah..I checked the site but I need c code to set up the serial port.
I tried another open source code, but this time I have linking error with
bios.h.
Forget the BIOS, it's ancient history. :-)

Depending on what you need to do, you may not need a library. Under 32 bit
Windows serial ports are treated using the "everything is a handle" approach
just as anything else. The quick HACK below opens up COM3: on this machine
where I have a modem, sends the Hayes command "identify" and displays the
response. It does the send and receieve asynchronously (loosely at the same
time) but includes NOTHING resembling sensible error checking.
I am writing a program in C and I have to set baud rate 2400,
transmit bits 8, parity none, stop bits 1 or 2.


Pay particular attention to the members of the DCB structure passed to
SetCommState() below. You'll need to set

BaudRate
Parirty
fParity
StopBits

My hack does the expedient thing of accepting whatever was there. That won't
work in your case.

You'll need to set the timeout parameters appropriately as well.

Regards,
Will

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
char szBuffer[80];
DCB dcb = {0};
DWORD dwRead, dwWritten;
HANDLE hComm;
OVERLAPPED ovlr = {0}, ovlw = {0};
COMMTIMEOUTS cto;

// Create events for overlapped operation

ovlr.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ovlw.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

// Open the port

hComm = CreateFile("\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);

// Get the state of the device and modify it

dcb.DCBlength = sizeof(dcb);
GetCommState(hComm, &dcb);
dcb.BaudRate = CBR_9600;
SetCommState(hComm, &dcb);

// Set the timeout parameters

cto.ReadIntervalTimeout = 1000;
cto.ReadTotalTimeoutConstant = 1000;
cto.ReadTotalTimeoutMultiplier = 1000;
cto.WriteTotalTimeoutConstant = 1000;
cto.WriteTotalTimeoutMultiplier = 1000;

SetCommTimeouts(hComm, &cto);

// Send a command and receieve a response. Note that
// we post the receive in advance of sending the
// command in order not to miss anything

printf("\r\nSending: ATI1\r\n");

ReadFile (hComm, szBuffer, sizeof(szBuffer), &dwRead, &ovlr);
WriteFile(hComm, "ATI1\r", strlen("ATI1\r"), &dwWritten, &ovlw);

// Wait for the receive to complete and display the response

if ( GetOverlappedResult(hComm, &ovlr, &dwRead, TRUE) )
{
szBuffer[dwRead] = 0;
printf("Received: %s\r\n", szBuffer);
}

// Close the device

CloseHandle(hComm);

return 0;
}
Nov 17 '05 #2
Hi =?Utf-8?B?dGFjb0JlbGw=?=,
I have to read from some device through a serial port.
I am writing a program in C and I have to set baud rate 2400,
transmit bits 8, parity none, stop bits 1 or 2.


I suggest to use one of the following implementations:

Serial library for C++
http://www.codeproject.com/system/serial.asp

http://www.codeproject.com/system/cserialport.asp

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/
Nov 17 '05 #3
Thank you very much for your suggestion.
Yeah..I checked the site but I need c code to set up the serial port.
I tried another open source code, but this time I have linking error with
bios.h.
What libraries can I use??

By the way, I'm using Windows XP.

"Jochen Kalmbach" wrote:
Hi =?Utf-8?B?dGFjb0JlbGw=?=,
I have to read from some device through a serial port.
I am writing a program in C and I have to set baud rate 2400,
transmit bits 8, parity none, stop bits 1 or 2.


I suggest to use one of the following implementations:

Serial library for C++
http://www.codeproject.com/system/serial.asp

http://www.codeproject.com/system/cserialport.asp

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp

Do you need daily reports from your server?
http://sourceforge.net/projects/srvreport/

Nov 17 '05 #4
"tacoBell" <ta******@discussions.microsoft.com> wrote in message
news:B6**********************************@microsof t.com...
Yeah..I checked the site but I need c code to set up the serial port.
I tried another open source code, but this time I have linking error with
bios.h.
Forget the BIOS, it's ancient history. :-)

Depending on what you need to do, you may not need a library. Under 32 bit
Windows serial ports are treated using the "everything is a handle" approach
just as anything else. The quick HACK below opens up COM3: on this machine
where I have a modem, sends the Hayes command "identify" and displays the
response. It does the send and receieve asynchronously (loosely at the same
time) but includes NOTHING resembling sensible error checking.
I am writing a program in C and I have to set baud rate 2400,
transmit bits 8, parity none, stop bits 1 or 2.


Pay particular attention to the members of the DCB structure passed to
SetCommState() below. You'll need to set

BaudRate
Parirty
fParity
StopBits

My hack does the expedient thing of accepting whatever was there. That won't
work in your case.

You'll need to set the timeout parameters appropriately as well.

Regards,
Will

#include <windows.h>
#include <stdio.h>

int main(int argc, char **argv)
{
char szBuffer[80];
DCB dcb = {0};
DWORD dwRead, dwWritten;
HANDLE hComm;
OVERLAPPED ovlr = {0}, ovlw = {0};
COMMTIMEOUTS cto;

// Create events for overlapped operation

ovlr.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
ovlw.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

// Open the port

hComm = CreateFile("\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);

// Get the state of the device and modify it

dcb.DCBlength = sizeof(dcb);
GetCommState(hComm, &dcb);
dcb.BaudRate = CBR_9600;
SetCommState(hComm, &dcb);

// Set the timeout parameters

cto.ReadIntervalTimeout = 1000;
cto.ReadTotalTimeoutConstant = 1000;
cto.ReadTotalTimeoutMultiplier = 1000;
cto.WriteTotalTimeoutConstant = 1000;
cto.WriteTotalTimeoutMultiplier = 1000;

SetCommTimeouts(hComm, &cto);

// Send a command and receieve a response. Note that
// we post the receive in advance of sending the
// command in order not to miss anything

printf("\r\nSending: ATI1\r\n");

ReadFile (hComm, szBuffer, sizeof(szBuffer), &dwRead, &ovlr);
WriteFile(hComm, "ATI1\r", strlen("ATI1\r"), &dwWritten, &ovlw);

// Wait for the receive to complete and display the response

if ( GetOverlappedResult(hComm, &ovlr, &dwRead, TRUE) )
{
szBuffer[dwRead] = 0;
printf("Received: %s\r\n", szBuffer);
}

// Close the device

CloseHandle(hComm);

return 0;
}
Nov 17 '05 #5
Hi, again.
Thank you very much for your help, but I'm still so stuck!
Forgive me. I've never used Visual C++ before.

Okay, I finally downloaded the source files from the site:
http://www.codeproject.com/system/serial.asp.

and extracted the contents into a directory and in that same directory
I put a .cpp file that contains main(). (Now I'm writing in C++)
Then, I opened that main.cpp file and hit 'Build->compile main.cpp'.

Oh no!! There are so many errors like:
'DWORD' : undeclared identifier.
'AfxThrowSerialException' : illegal use of type 'void'.
unexpected 'class CSerialException ('................etc. etc.

I have no idea how to fix these.
Can anyone give me a help, pleaase?


Nov 17 '05 #6
"William DePalo [MVP VC++]" wrote:
Depending on what you need to do, you may not need a library. Under 32 bit
Windows serial ports are treated using the "everything is a handle" approach
just as anything else.


Thanks William!
I didn't imagine that there are built-in functions to deal with serial ports.
How stupid I am......I finally understood what you said.
Your code works.

tacoBell

Nov 17 '05 #7
"tacoBell" <ta******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com...
Your code works.
Geez, I hope so. It is awfully embarassing to post something to a server
which retains documents for months at a time which can be accessed by hordes
all over the globe. :-)
Thanks William!


You are welcome. Just remember that it is a quick HACK to get you started.
You should look up the help entry for all unfamiliar functions here:

http://msdn.microsoft.com/library/default.asp

This is a good read, too:

http://msdn.microsoft.com/library/de..._resources.asp

Regards,
Will
Nov 17 '05 #8
"William DePalo [MVP VC++]" <wi***********@mvps.org> wrote in message
news:uf**************@TK2MSFTNGP10.phx.gbl...
post something to a server ...


Oops, make that

post something that doesn't work ...

Regards,
Will
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: nchap99 | last post by:
hello, Does anybody know if Microsoft Dot Net supports the RS232 type of connection. As far as I know Visual basic 6 supports RS232 connection format. Thanks,
6
by: Przemo | last post by:
Hi, Do you know some good RS232C class? There is one in VB.NET 101 Examples, but I think it is poor. 1. I can't for e.g. read into my application all data received. I must tell how many...
2
by: tacoBell | last post by:
Hi, Can anyone help me, please? I have to read from some device through a serial port. I am writing a program in C and I have to set baud rate 2400, transmit bits 8, parity none, stop bits 1...
6
by: Ken Breit | last post by:
I am using the RS232.vb class to talk to the serial port. The problem I am having is when I try to read if anything is on the comm port. I call the read method, with the number of bytes I am...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
2
by: Wouter van Teijlingen | last post by:
Dear Readers, I was reading about how to control the COM and LPT port using VB .NET. I've found a lot of information, and it was very useful to me. I found an example program on the site of...
4
by: Dave Harry | last post by:
I found the RS232 class from MS's 101 VB samples. Writing to the port works fine. (I've got hyperterminal on the other comm port and a crossover cable between COM1 and COM2) The port is opened...
7
by: Lou | last post by:
I have a class that uses the serial port class Private SerialPort as New SerialPort When I receive the asyncronous serial port response it appears that data is on a different thread than my...
2
by: egress | last post by:
Forgive me for stupid questions for I am new to serial IO programming. I am developing an app that will need to communicate with a device via RS232 protocol using a standard 9 pin serial cable. ...
2
by: eljainc | last post by:
Hello, Does anybody know if there are any freeware or inexpensive RS232/ Serial port libraries that are C# .NET 1.1 or .NET 2.0? I think there is a SerialPort class in .NET 2.0 /C# but it is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.