Como corrigo este codigo
Publicado por Triana (1 intervención) el 29/04/2014 03:32:11
Hola, quisiera que me ayudaran como puedo arreglar este codigo que es para realizar un chat por medio de puerto paralelo. Este programa supuestamente debe enviar y recibir al imismo tiempo pero no se que comando me funcionara o como se pude corregir.
Necesito su ayuda,soy nueva en esto
Necesito su ayuda,soy nueva en esto
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <string.h>
#include <windows.h>
BOOL SerialSendByte(HANDLE hPort, BYTE byte);
BOOL SerialReceiveByte(HANDLE hPort, BYTE *pbyte, BOOL *pTimeout);
BOOL CloseSerialPort(HANDLE hPort);
HANDLE OpenSerialPort( char *psPort, DWORD dwBaudRate, BYTE bByteSize, BYTE bParity, BYTE bStopBits, DWORD Timeout)
{
HANDLE hPort;
DCB dcbPort;
COMMTIMEOUTS commTimeouts;
DWORD dwError; // Open Serial Port
hPort=CreateFile(psPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0,NULL);
if ( hPort == INVALID_HANDLE_VALUE )
{
dwError = GetLastError (); // Flush error code
return hPort;
}
FillMemory(&dcbPort, sizeof(dcbPort), 0);
dcbPort.DCBlength = sizeof(dcbPort);
GetCommState (hPort, &dcbPort);
dcbPort.BaudRate = dwBaudRate;
dcbPort.ByteSize = bByteSize;
dcbPort.Parity = bParity;
dcbPort.StopBits = bStopBits;
dcbPort.fBinary = TRUE; // Binary mode; no EOF check
dcbPort.fParity = TRUE; // Enable parity checking
dcbPort.fOutxCtsFlow = FALSE; // No CTS output flow control
dcbPort.fOutxDsrFlow = FALSE; // No DSR output flow control
dcbPort.fDtrControl = DTR_CONTROL_ENABLE;
dcbPort.fDsrSensitivity = FALSE; // DSR sensitivity
dcbPort.fTXContinueOnXoff = TRUE; // XOFF continues Tx
dcbPort.fOutX = FALSE; // No XON/XOFF out flow control
dcbPort.fInX = FALSE; // No XON/XOFF in flow control
dcbPort.fErrorChar = FALSE; // Disable error replacement
dcbPort.fNull = FALSE; // Disable null stripping
dcbPort.fRtsControl = RTS_CONTROL_ENABLE;
dcbPort.fAbortOnError = FALSE; // Do not abort reads/writes on
if (!SetCommState (hPort, &dcbPort))
{
dwError = GetLastError ();
CloseSerialPort(hPort);
hPort = INVALID_HANDLE_VALUE;
return hPort;
}
commTimeouts.ReadIntervalTimeout = 0; // Specifies the maximum time, in milliseconds, allowed to elapse between the arrival
commTimeouts.ReadTotalTimeoutMultiplier = 50;
commTimeouts.ReadTotalTimeoutConstant = Timeout;
commTimeouts.WriteTotalTimeoutMultiplier = 10;
commTimeouts.WriteTotalTimeoutConstant = 1000;
if (!SetCommTimeouts (hPort, &commTimeouts))
{
dwError = GetLastError (); // Flush error code
CloseSerialPort(hPort);
hPort = INVALID_HANDLE_VALUE;
return hPort;
}
return hPort;
}
BOOL SerialSendByte(HANDLE hPort, BYTE byte)
{
BOOL bRes;
DWORD dwError,
dwNumBytesWritten=0;
bRes=WriteFile(hPort, &byte, 1, &dwNumBytesWritten, NULL);
if ((!bRes)||(dwNumBytesWritten!=1))
dwError = GetLastError (); // Flush error code
return bRes;
}
BOOL SerialReceiveByte(HANDLE hPort, BYTE *pbyte, BOOL *pTimeout)
{
BOOL bRes;
DWORD dwError, lpNumberOfBytesRead=0;
*pTimeout=FALSE;
bRes=ReadFile( hPort, pbyte, 1, &lpNumberOfBytesRead, NULL);
if (!bRes)
dwError = GetLastError (); // Flush error code
if ((bRes)&&(lpNumberOfBytesRead==0))
*pTimeout = TRUE;
return bRes;
}
BOOL CloseSerialPort(HANDLE hPort)
{
BOOL bRes;
DWORD dwError;
bRes=CloseHandle(hPort);
if (!bRes)
dwError = GetLastError (); // Flush error code
return bRes;
}
int main(){
HANDLE hPort;
BOOL bRes;
char c;
BYTE byte;
BOOL timeout;
hPort=OpenSerialPort("COM3",CBR_9600,8,NOPARITY,TWOSTOPBITS,5000);
if (hPort==INVALID_HANDLE_VALUE)
return 1;
//printf("Error abriendo puerto com1");
while (1) {
c=getchar();
bRes=SerialSendByte(hPort,c);
if (!bRes) {
printf("Error escribiendo en puerto com1");
return 1;
}
if (c=='x')
break;
}
while(1){
bRes=SerialReceiveByte(hPort,&byte,&timeout);
if (!bRes)
break;
if (timeout)
printf("\n--->timeout\n");
else
putchar(byte);
if (!bRes) {
printf("Error leyendo de puerto com1");
return 1;
}
}
CloseSerialPort(hPort);
return 0;
}
Valora esta pregunta


0