阅读:1592回复:2
串行通讯的问题
有一个单片机和一PC机通过串行口通讯,单片机设备每次只能接收一个字节,单片机端的串行口为9针的,引出了8(CTS)为忙信号检测。当单片机设备忙时CTS信号为高,不忙时为低,PC机上该如何检测CTS信号然后发送一个字节的数据呢?我这样做的:
COMSTAT ComStat; DWORD dwErrorFlags; do{ ClearCommError(m_hCom,&dwErrorFlags,&ComStat); }while(!ComStat.fCtsHold); WriteFile(....); 可是不灵啊?请问大虾们,该怎么编写呢!PC机上接收数据没问题! 有一段VB的代码,使用MSCOMM控件做的 Public Sub SCCCC(B As Byte) Dim S(0) As Byte S(0) = B Do Until MSComm1.CTSHolding = True '等待BUSY Loop MSComm1.Output = S End Sub |
|
沙发#
发布于:2004-08-19 21:58
你有没有产生一个句柄来操作writefile?
|
|
板凳#
发布于:2004-08-19 22:02
以下仅供参考(用BCB做的):
char *ComNo; DCB dcb; String Temp; HANDLE hComm; unsigned long lrc; Temp = "COM" + IntToStr(RadioGroup1->ItemIndex + 1); ComNo = Temp.c_str(); hComm = CreateFile(ComNo,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0); if(hComm == INVALID_HANDLE_VALUE) { MessageBox(0,"Error in opening port!","Comm Error",MB_OK); return; } GetCommState(hComm,&dcb); dcb.BaudRate = CBR_2400; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; if (!SetCommState(hComm,&dcb)) { MessageBox (0,"Error in setting port","Set Error",MB_OK); CloseHandle(hComm); return; }; WriteFile(hComm,XXX,X,&lrc,NULL); |
|