GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2009, 12:42
psm psm is offline
New Member
 
Join Date: Nov 2009
Posts: 9
psm is on a distinguished road

Configuring port rs232


Good evening,
please can you help me with this code? I want to change it, because I must read some data from reader, which will be access to pc by rs232.
Thank you.

CPP / C++ / C Code:
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
char *pcCommPort = "COM2";

hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}

// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.

fSuccess = GetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}

// Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.

dcb.BaudRate = CBR_57600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

fSuccess = SetCommState(hCom, &dcb);

if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}

printf ("Serial port %s successfully reconfigured.\n", pcCommPort);
return (0);
}
Last edited by LuciWiz : 11-Nov-2009 at 14:07. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 11-Nov-2009, 13:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Configuring port rs232


Quote:
Originally Posted by psm
...can you help me...

What kind of help would you like? Exactly?

There is a short and sweet introduction here: http://www.robbayer.com/serial.php Just click on the Windows link.

Regards,

Dave
  #3  
Old 12-Nov-2009, 06:19
psm psm is offline
New Member
 
Join Date: Nov 2009
Posts: 9
psm is on a distinguished road

Re: Configuring port rs232


Quote:
Originally Posted by davekw7x
What kind of help would you like? Exactly?

There is a short and sweet introduction here: http://www.robbayer.com/serial.php Just click on the http://www.robbayer.com/files/serial-win.pdf link.

Regards,

Dave

Hi, thank you for good introduction. And I want to know just this:

1. how can I tell this program, that I will use a rs232 port? can it be set up by instruction: OPEN_EXISTING???

2. in thi sintroduction is some example to configure timeouts and it is written there, that "tell Windows not to wait for data to show up", if I understand it mean, that if I dont want any timeouts, I must to write it. Is it true??? because I want to read data from a reader, which wont work in the same time, it will work in random time.

3. how can I set up the DCB? must I find informations from a producer of the reader?

Thank you for answer.
  #4  
Old 12-Nov-2009, 07:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Configuring port rs232


Quote:
Originally Posted by psm
Hi, thank you for good introduction. And I want to know just this:

1. how can I tell this program, that I will use a rs232 port? can it be set up by instruction: OPEN_EXISTING???
Huh? Your code and the example code in the paper that I referenced show how to use CreateFile for a given COM port. Your example uses COM2; the paper uses COM1.
Quote:
Originally Posted by psm
2. ..."tell Windows not to wait for data to show up"
The paper shows exactly how to do it. The explanation tells exactly how to set timeout values. See Footnote.
Quote:
Originally Posted by psm
3. how can I set up the DCB? must I find informations from a producer of the reader?
The paper gives an example of how to set it for 19200 bits/sec, eight data bits, one stop bit and no parity bit. Of course you have to know the bit rate for your device (eight data bits, one stop bit, no parity bits are pretty much ubiquitous). See Footnote (again).



Regards,

Dave

Footnote: For definitions and applications information for Windows functions, I suggest that you start at the Microsoft msdn site http://msdn.microsoft.com

Search for the function that you are interested in.

For example:

SetCommState Function

or

SetCommTimeouts Function

These have numerous links that explain the structs and how to use them.
  #5  
Old 12-Nov-2009, 08:42
psm psm is offline
New Member
 
Join Date: Nov 2009
Posts: 9
psm is on a distinguished road

Re: Configuring port rs232


Quote:
Huh? Your code and the example code in the paper that I referenced show how to use CreateFile for a given COM port. Your example uses COM2; the paper uses COM1.

I think, we dont understand, because I know it, but COM1 and COM2 are just your names for the port. And I can name it random, but instruction OPEN_EXISTING mean, that it load actual port, which is used. I think so. Is it true?

Quote:
The paper shows exactly how to do it. The explanation tells exactly how to set timeout values. See Footnote.

So, now I understand better, this paper shows exactly what I have to do. Sorry for some mistakes, it is because I couldnt find som words.

Thank you very much, so I try to do something and than I can give it here.
  #6  
Old 12-Nov-2009, 09:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Configuring port rs232


Quote:
Originally Posted by psm
COM1 and COM2 are just your names for the port. And I can name it random,
No, they are not, and no, you can not!

These are special strings that CreateFIle() treats as serial ports. If you call CreateFile() with "COM1" as its first argument, it will attempt to open serial communications (RS-232) port number one. "COM2" would be for serial communications (RS-232) port number two. Etc. Look at the examples and follow them exactly for starters. See Footnote.


You might want to poke around msdn some more. For example: Serial Communications in Win32


Regards,

Dave

I have observed that CreateFIle() also treats COM strings with colons ("COM1:", "COM2:", etc.) as serial port names (those were used in previous versions of Windows and in DOS and you might run across them in old programs by old programmers), but I would stick with the designations used in examples on msdn and other reliable sources.
Last edited by davekw7x : 12-Nov-2009 at 10:10.
  #7  
Old 12-Nov-2009, 10:16
psm psm is offline
New Member
 
Join Date: Nov 2009
Posts: 9
psm is on a distinguished road

Re: Configuring port rs232


Quote:
I have observed that strings with colons ("COM1:", "COM2:", etc.) are also treated as serial port names (those were used in previous versions of Windows and in DOS and you might run across them in old programs by old programmers), but I would stick with the designations used in examples on msdn and other reliable sources.

Yes, I have some darkness in it. I didt know, that rs-232 have ports COM1 and COM2. I thought, that COM1 and COM2 are just usb ports. So now I am more clever. Thanks.
  #8  
Old 14-Nov-2009, 07:36
psm psm is offline
New Member
 
Join Date: Nov 2009
Posts: 9
psm is on a distinguished road

Re: Configuring port rs232


This is the program. It dont give any error, so I think it would be good, but I must try it on the reader. But I have to give recieved data to xml form yet. How can I do it? Thanks.

CPP / C++ / C Code:
#include <windows.h>
#include <stdio.h>
#include <winbase.h>

int main(int argc, char *argv[])
{
DCB dcb;
HANDLE hCom;                              
BOOL fSuccess;                            
BYTE Data;                                
DWORD Pocet;                             
COMMTIMEOUTS CommTimeouts;
char *pcCommPort = "COM2";

hCom = CreateFile(                 
         pcCommPort,                      
         GENERIC_READ | GENERIC_WRITE,   
         0,                               
         NULL,                            
         OPEN_EXISTING,                 
         0,                               
         NULL);                           


if ( hCom == INVALID_HANDLE_VALUE )
     {
         printf("\n Chyba:  Port sa neda otvorit.\n");
         system("pause");
         CloseHandle(hCom);
         return 0;
     }





DCB dcbSerialParams = {0};                  
dcb.DCBlength = sizeof(DCB);

dcb.BaudRate = CBR_57600; 
dcb.ByteSize = 8; 
dcb.Parity = NOPARITY; 
dcb.StopBits = ONESTOPBIT; 

fSuccess = SetCommState(hCom, &dcb);      

if (!fSuccess)
{
// Handle the error.
printf ("Nastavenie DCB zlyhalo %d.\n", GetLastError());
return (0);
}





//Zmena nastavení prerušení
COMMTIMEOUTS timeouts={0};
CommTimeouts.ReadIntervalTimeout = 50;  
CommTimeouts.ReadTotalTimeoutMultiplier = 10;  
CommTimeouts.ReadTotalTimeoutConstant = 50;    
 
fSuccess = SetCommTimeouts(hCom, &CommTimeouts);          

if (!fSuccess)
{
printf ("Nastavenie preruseni zlyhalo %d.\n", GetLastError());
return (0);
}

printf ("Port %s bol uspesne nastaveny.\n", pcCommPort);
return (0);





ReadFile(
              hCom,         
              &Data,        
              1,            
              &Pocet,       
              NULL);        

                         
}
Last edited by LuciWiz : 14-Nov-2009 at 11:16. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Serial Port Communication a3.charles C Programming Language 0 03-Aug-2006 23:42
Code for receiving data from parallel port in VB.net harry.net .NET Forum 1 10-Mar-2006 15:41
Problem to read on a serial port collinm C Programming Language 2 30-Mar-2005 01:42
Problem with string and serial port collinm C Programming Language 13 25-Mar-2005 07:39

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:11.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.