求救串口读写问题
各位高手:
本人用PB4.2建造了一个CEPC WEBPAD,并编程进行串口读写操作,现遇到一奇怪问题:
对串口读写,但串口无反应.
我对串口的设置是:
hLocal = CreateFile (TEXT("COM1:"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
DCB dcb;
dcb.DCBlength = sizeof (DCB);
GetCommState (hLocal, &dcb);
dcb.BaudRate = 2400;//nSpeed; //2400 baudrate
dcb.fBinary = TRUE;
dcb.fParity = TRUE;
dcb.fOutxCtsFlow = FALSE; // No CTS output flow control
dcb.fOutxDsrFlow = FALSE; // No DSR output flow control
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = FALSE; // DSR sensitivity
dcb.fTXContinueOnXoff = TRUE; // XOFF continues Tx
dcb.fOutX = FALSE; // No XON/XOFF out flow control
dcb.fInX = FALSE; // No XON/XOFF in flow control
dcb.fErrorChar = FALSE; // Disable error replacement.
dcb.fNull = FALSE; // Disable null stripping.
dcb.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
dcb.fAbortOnError = FALSE; // Do not abort reads/writes on
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
SetCommState (hLocal, &dcb);
COMMTIMEOUTS cto;
cto.ReadIntervalTimeout = 0;
cto.ReadTotalTimeoutMultiplier = 0;
cto.ReadTotalTimeoutConstant = 0;
cto.WriteTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = 0;
SetCommTimeouts (hLocal, &cto);
EscapeCommFunction (hLocal, CLRIR);
然后创建两个THREAD对串口读写:
rc = ReadFile (hLocal, &Byte, 1, &cBytes, NULL);
DEBUG可看到:
rc = 1;cBytes = 0;也就是没读到数据,可我正往串口送数据;
rc = WriteFile (hLocal, &Byte, 1, &cBytes, NULL);
DEBUG可看到:
rc = 1;cBytes = 1;也就是正确的写入一个数,可我示波器在串口看不到波型.
不知各位是否也遇到这奇怪问题.我已搞了三天了,只好求救各位了.多谢!
另我在WIN2000下用VC也是用以上的方法编程对以上说的同一串口读写操作,但能正确读写操作.