By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

using RS232 serial port

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.