Hi, I need to send ascii strings to a device via a rs232 port.
Is it possible to just set up the port, and send the string, assuming no hand shaking is needed?
I browsed through alot of codes and they are all so complicated and most don't compile which I don't know why.
Thanks very much. I am not proficient in using the C/C++ language for accessing serial ports hope someone can help me out.
Thanks!
Nick
Comments
you can use the bios.h header file to configure and use the serial port.
I will write a little tutorial on it this night and you will have it by tomorrow.
programming the serial port is easy as you will notice soon.
good programming
Hi, I need to send ascii strings to a device via a rs232 port.
:
: Is it possible to just set up the port, and send the string, assuming no hand shaking is needed?
:
:
: I browsed through alot of codes and they are all so complicated and most don't compile which I don't know why.
:
:
: Thanks very much. I am not proficient in using the C/C++ language for accessing serial ports hope someone can help me out.
:
: Thanks!
: Nick
:
I think that's the problem, most codes I see have bios.h and i couldn't seem to find this header file anywhere..
Thanks
Nick
: hi there ,
: you can use the bios.h header file to configure and use the serial port.
: I will write a little tutorial on it this night and you will have it by tomorrow.
: programming the serial port is easy as you will notice soon.
: good programming
:
:
:
:
:
:
:
:
: Hi, I need to send ascii strings to a device via a rs232 port.
: :
: : Is it possible to just set up the port, and send the string, assuming no hand shaking is needed?
: :
: :
: : I browsed through alot of codes and they are all so complicated and most don't compile which I don't know why.
: :
: :
: : Thanks very much. I am not proficient in using the C/C++ language for accessing serial ports hope someone can help me out.
: :
: : Thanks!
: : Nick
: :
:
:
:
: I think that's the problem, most codes I see have bios.h and i couldn't seem to find this header file anywhere..
:
: Thanks
: Nick
:
Forget about bios.h, it is for DinOSaurs. As most of us know, there are no DinOSaurs anymore.
In the past, I have posted several examples of how to do it in Windows. Search the messageboards. What you need is to use the following functions: (windows.h)
[code]
CreateFile open com port
GetCommTimeouts get port timeout settings
SetCommTimeouts set port timeout, if you don't need any
detailed configuration, just set
COMMTIMEOUTS.ReadTotalTimeoutConstant to 100
and the rest of them to zero and it will work.
GetCommState Read the current port settings and change them,
SetCommState (baudrate, parity etc) then use SetCommState to
save them
WriteFile send data
ReadFile receive data
CloseHandle close down the com port when you aren't using it.
[/code]
Manual can be found here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/communications_functions.asp
Thanks
Nick
: : Hi, my compilers do not have bios.h
: :
: : I think that's the problem, most codes I see have bios.h and i couldn't seem to find this header file anywhere..
: :
: : Thanks
: : Nick
: :
:
:
: Forget about bios.h, it is for DinOSaurs. As most of us know, there are no DinOSaurs anymore.
:
: In the past, I have posted several examples of how to do it in Windows. Search the messageboards. What you need is to use the following functions: (windows.h)
:
: [code]
: CreateFile open com port
:
: GetCommTimeouts get port timeout settings
:
: SetCommTimeouts set port timeout, if you don't need any
: detailed configuration, just set
: COMMTIMEOUTS.ReadTotalTimeoutConstant to 100
: and the rest of them to zero and it will work.
:
: GetCommState Read the current port settings and change them,
: SetCommState (baudrate, parity etc) then use SetCommState to
: save them
:
: WriteFile send data
: ReadFile receive data
: CloseHandle close down the com port when you aren't using it.
: [/code]
:
:
: Manual can be found here:
:
: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/communications_functions.asp
:
Thanks
Nick
: : Hi, my compilers do not have bios.h
: :
: : I think that's the problem, most codes I see have bios.h and i couldn't seem to find this header file anywhere..
: :
: : Thanks
: : Nick
: :
:
:
: Forget about bios.h, it is for DinOSaurs. As most of us know, there are no DinOSaurs anymore.
:
: In the past, I have posted several examples of how to do it in Windows. Search the messageboards. What you need is to use the following functions: (windows.h)
:
: [code]
: CreateFile open com port
:
: GetCommTimeouts get port timeout settings
:
: SetCommTimeouts set port timeout, if you don't need any
: detailed configuration, just set
: COMMTIMEOUTS.ReadTotalTimeoutConstant to 100
: and the rest of them to zero and it will work.
:
: GetCommState Read the current port settings and change them,
: SetCommState (baudrate, parity etc) then use SetCommState to
: save them
:
: WriteFile send data
: ReadFile receive data
: CloseHandle close down the com port when you aren't using it.
: [/code]
:
:
: Manual can be found here:
:
: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/communications_functions.asp
:
: Hi, somehow I managed to configure the port. May I know how I can send data to the device?
:
: Thanks
: Nick
:
:
:
: : : Hi, my compilers do not have bios.h
: : :
: : : I think that's the problem, most codes I see have bios.h and i couldn't seem to find this header file anywhere..
: : :
: : : Thanks
: : : Nick
: : :
: :
: :
: : Forget about bios.h, it is for DinOSaurs. As most of us know, there are no DinOSaurs anymore.
: :
: : In the past, I have posted several examples of how to do it in Windows. Search the messageboards. What you need is to use the following functions: (windows.h)
: :
: : [code]
: : CreateFile open com port
: :
: : GetCommTimeouts get port timeout settings
: :
: : SetCommTimeouts set port timeout, if you don't need any
: : detailed configuration, just set
: : COMMTIMEOUTS.ReadTotalTimeoutConstant to 100
: : and the rest of them to zero and it will work.
: :
: : GetCommState Read the current port settings and change them,
: : SetCommState (baudrate, parity etc) then use SetCommState to
: : save them
: :
: : WriteFile send data
: : ReadFile receive data
: : CloseHandle close down the com port when you aren't using it.
: : [/code]
: :
: :
: : Manual can be found here:
: :
: : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/communications_functions.asp
: :
:
:
:
: Thanks
: Nick
:
WriteFile() -- I think there are some examples in MSDN.
You should read the article about Communications Resources
http://windowssdk.msdn.microsoft.com/en-gb/library/ms680810.aspx
Code should be pretty self-explaning, call openComPort() with "COM1" and "9600" as parameters, for example.
comport.h
[code]
#ifndef _COMPORT_H
#define _COMPORT_H
#include
#include
BOOL openComPort (const char* port, const char* baudrate);
void closeComPort (void);
DWORD sendData (const char* data, DWORD size);
DWORD receiveData (char* data, DWORD size);
#endif /* _COMPORT_H */
[/code]
comport.c
[code]
#include "comport.h"
static HANDLE _hCom;
BOOL openComPort (const char* port, const char* baudrate)
{
char buildStr[50];
DCB dcb;
COMMTIMEOUTS timeouts = {0};
_hCom = CreateFile(port,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if(_hCom == INVALID_HANDLE_VALUE)
{
_hCom = NULL;
return FALSE;
}
/* set timeouts */
timeouts.ReadTotalTimeoutConstant = 100;
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 0;
if(SetCommTimeouts(_hCom, &timeouts) == FALSE)
return FALSE;
dcb.DCBlength = sizeof(DCB);
if(GetCommState(_hCom, &dcb) == FALSE)
return FALSE;
/* Simplified way of setting up the COM port using strings: */
buildStr[0] = '';
strcat(buildStr, "baud=");
strcat(buildStr, baudrate);
strcat(buildStr," parity=N data=8 stop=1");
/* (A more effective way is to setup the members of the DCB struct manually,
then you don't need BuildCommDCB) */
BuildCommDCB(buildStr, &dcb);
return SetCommState(_hCom, &dcb);
}
void closeComPort(void)
{
CloseHandle(_hCom);
}
DWORD sendData (const char* data, DWORD size)
{
DWORD numberOfBytesWritten;
WriteFile(_hCom,
data,
size,
&numberOfBytesWritten,
0);
return numberOfBytesWritten;
}
DWORD receiveData (char* data, DWORD size)
{
DWORD numberOfBytesRead;
ReadFile(_hCom,
data,
size,
&numberOfBytesRead,
0);
return numberOfBytesRead;
}
[/code]