C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-26-2002, 05:08 PM   #1
Unregistered
Guest
 
Posts: n/a
Unhappy Serial port read..can someone tell me..

Below I have pasted my program. The purpose of the program is to read data from a serial port (in this case COM2) and print it out to the screen. I can't seem to get it to print to the screen. I don't know if the port is really configured properly. Can someone out there take a look at my code and run it to see what the problem is? Thnx in advance! Rachael

#include <windows.h>
#include <string.h>
#include <memory.h>
#include <commdlg.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX 130

// Flow control flaags
#define FC_DTRDSR 0x01
#define FC_RTSCTS 0x02
#define FC_XONXOFF 0x04

char port[5] = "COM2:" ; /* Comm port name. */
char msg[MAX] ; /* Message received from port. */
int n_msg = 0 ; /* Counts bytes to read. */
int n_in ; /* Counts bytes read. */
int ok ; /* Return code from function calls. */
HANDLE hPort ; /* Handle to IO port object. */
DCB dcb ;
COMMTIMEOUTS CommTimeOuts ;
BYTE bSet ;


int main(void) {

// Open comm port
hPort = CreateFile(port,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL) ;

if (hPort == INVALID_HANDLE_VALUE) {
printf("Cannot open IO port (error %d)\n", GetLastError()) ;
return 1 ;
}

// Setup comm port connection
dcb.DCBlength = sizeof(dcb) ;

GetCommState(hPort, &dcb) ;

dcb.BaudRate = 9600 ;
dcb.ByteSize = 8 ;
dcb.Parity = 0 ;
dcb.StopBits = 1 ;

// setup hardware flow control

bSet = (BYTE) ((FC_XONXOFF & FC_DTRDSR) != 0) ;
dcb.fOutxDsrFlow = bSet ;
if (bSet)
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE ;
else
dcb.fDtrControl = DTR_CONTROL_ENABLE ;

bSet = (BYTE) ((FC_XONXOFF & FC_RTSCTS) != 0) ;
dcb.fOutxCtsFlow = bSet ;
if (bSet)
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE ;
else
dcb.fRtsControl = RTS_CONTROL_ENABLE ;

// setup software flow control

bSet = (BYTE) ((FC_XONXOFF & FC_XONXOFF) != 0) ;

dcb.fInX = dcb.fOutX = bSet ;

dcb.XonChar = 0x11 ;
dcb.XoffChar = 0x13 ;
dcb.XonLim = 100 ;
dcb.XoffLim = 100 ;
dcb.fBinary = TRUE ;
dcb.fParity = TRUE ;

SetCommState(hPort, &dcb) ;

// read data from port
do {
n_msg = (n_msg <= MAX ? n_msg : MAX) ;
ok = ReadFile(hPort, msg, n_msg, &n_in, NULL) ;
printf("%s", msg) ;
} while(ok) ;

if (!ok) {
printf("Cannot read message (error %d)\n", GetLastError()) ;
return 1 ;
}

// Cleanup
ok = CloseHandle(hPort) ;
if (!ok) {
printf("Cannot close port handle (error %d)\n", GetLastError()) ;
}

return 0 ;
}
  Reply With Quote
Old 06-27-2002, 06:59 AM   #2
Unregistered
Guest
 
Posts: n/a
Wink

Is this code so specialized that there are no C and win32 experts out there...if you're out there please look at my code to see why I can't seem to read data from a serial port. Thanks!
Rachael
  Reply With Quote
Old 06-27-2002, 07:51 AM   #3
&TH of undefined behavior
 
Fordy's Avatar
 
Join Date: Aug 2001
Posts: 5,218
Quote:
Originally posted by Unregistered
Is this code so specialized that there are no C and win32 experts out there...if you're out there please look at my code to see why I can't seem to read data from a serial port. Thanks!
Rachael
It's not that the problem is difficult............personally I have no way to test your code as the com port on my computer is redundant.......I think there's a small spider living in there at the moment and he's welcome to it! Go USB!!!!


Oh...and by the way....didnt you post this question a few days ago but under the name "Fred"?
__________________
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut."
Albert Einstein (1879 - 1955)


Board Rules
Fordy is offline   Reply With Quote
Old 06-27-2002, 08:21 AM   #4
Unregistered
Guest
 
Posts: n/a
Wink

No, but I did use your link to develop this program. Anyway, I rewrote the code based off of information from the MSDN website and fixed the problem. Rachael
  Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial Port Questions valaris Tech Board 2 05-22-2009 08:26 AM
FTP program jakemott Linux Programming 14 10-06-2008 01:58 PM
Passing serial settings from pseudoterminal to physical port djinn Linux Programming 2 05-22-2008 11:43 PM
Serial Port Issues (again!) HalNineThousand Linux Programming 6 04-09-2008 08:26 PM
HELP with storing serial port data into txt file inmaterichard C Programming 2 04-02-2008 02:20 AM


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


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22