Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Dev C++ Serial Port Interfacing

Status
Not open for further replies.

RFdata

Newbie level 6
Joined
Nov 27, 2008
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,407
c++ serial port

Hi, i´m trying to interface serial port using DEV c++, when i used an old Dos compiler everything was easy simply use outport and inport, now for windows it´s very different, but I sussefull put data on serial out, the problem is that I can´t read data from RX pin.
It only receives the data A that is the output, but there is no loop back from the TX to RX on the serial pins, so where it get that value?
I using this code

Any help is welcome
Tanks



Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <bios.h>
 
// Flow control flags
 
#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04
 
 
using namespace std;
    // variables used with the com <strong class="highlight">port[/b]
    BOOL            bPortReady;
    DCB                dcb;
    COMMTIMEOUTS    CommTimeouts;
    BOOL            bWriteRC;
    BOOL            bReadRC;
    DWORD            iBytesWritten;
    DWORD            iBytesRead;
 
HANDLE SerialInit(char *ComPortName, int BaudRate) 
{
    HANDLE hComm;
    
    hComm = CreateFile(ComPortName, 
        GENERIC_READ | GENERIC_WRITE,
        0, // exclusive access
        NULL, // no security
        OPEN_EXISTING,
        0, // no overlapped I/O
        NULL); // null template 
 
    bPortReady = SetupComm(hComm, 1, 128); // set buffer sizes
 
 
    bPortReady = GetCommState(hComm, &dcb);
    dcb.BaudRate = BaudRate;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    //dcb.Parity = EVENPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.fAbortOnError = TRUE;
 
    // set XON/XOFF
    dcb.fOutX = FALSE;                    // XON/XOFF off for transmit
    dcb.fInX    = FALSE;                    // XON/XOFF off for receive
    // set RTSCTS
    dcb.fOutxCtsFlow = FALSE;                    // turn on CTS flow control
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;    // 
    // set DSRDTR
    dcb.fOutxDsrFlow = FALSE;                    // turn on DSR flow control
    dcb.fDtrControl = DTR_CONTROL_ENABLE;    // 
    dcb.fDtrControl = DTR_CONTROL_DISABLE;    // 
    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;    // 
 
    bPortReady = SetCommState(hComm, &dcb);
 
    // Communication timeouts are optional
 
    bPortReady = GetCommTimeouts (hComm, &CommTimeouts);
 
    CommTimeouts.ReadIntervalTimeout = 5;
    CommTimeouts.ReadTotalTimeoutConstant = 5;
    CommTimeouts.ReadTotalTimeoutMultiplier = 1;
    CommTimeouts.WriteTotalTimeoutConstant = 5;
    CommTimeouts.WriteTotalTimeoutMultiplier = 1;
 
    bPortReady = SetCommTimeouts (hComm, &CommTimeouts);
 
    return hComm;
}
 
 
char SerialGetc(HANDLE *hComm)
{
    char rxchar;
    BOOL    bReadRC;
    static    DWORD    iBytesRead;
 
    bReadRC = ReadFile(*hComm, &rxchar, 1, &iBytesRead, NULL);
 
    return rxchar;
}
 
void SerialPutc(HANDLE *hComm, char txchar)
{
    BOOL    bWriteRC;
    static    DWORD    iBytesWritten;
    
    bWriteRC = WriteFile(*hComm, &txchar, 1, &iBytesWritten,NULL);
    
    return;
}
 
int main()
{
    HANDLE my=SerialInit("COM1",9600);
    
    char letter;
    printf("test\n");
        
    HANDLE *ptr;
    *ptr=my;
    
     
    for( ; ; )
    {
           
    SerialPutc(ptr,'A');     
    printf(" %c ",SerialGetc(ptr));
     getch();
    }
          
    return 0;    
}

 
Last edited by a moderator:

dev c++ serial

in msVC++ and devC++ it is same. As they both can use windows system calls.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top