![]() |
| | #1 |
| Guest
Posts: n/a
| #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 ; } |
|
| | #2 |
| Guest
Posts: n/a
| 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 |
|
| | #3 | |
| &TH of undefined behavior Join Date: Aug 2001
Posts: 5,218
| Quote:
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 | |
| | #4 |
| Guest
Posts: n/a
| 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 |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |