D
deltaSoft
Unregistered / Unconfirmed
GUEST, unregistred user!
很好,还有一个:<br>//打开串口<br>bool __fastcall TForm1:pencomm(unsigned char byPort)<br> {<br> String sCom; <br> DCB dcb;<br> COMMTIMEOUTS CommTimeOuts; <br><br> if(commstate)<br> {<br> return true;<br> }<br> sCom = "////.//COM" + IntToStr(byPort); <br> <br> m_hComHandle = ::CreateFile(sCom.c_str(),<br> GENERIC_READ|GENERIC_WRITE, <br> 0, <br> NULL, <br> OPEN_EXISTING, <br> FILE_ATTRIBUTE_NORMAL,<br> NULL); <br> if(m_hComHandle == INVALID_HANDLE_VALUE) <br> {<br> CloseHandle(m_hComHandle);<br> return false;<br> }<br> <br> SetCommMask(m_hComHandle, EV_RXCHAR); <br> <br> if(SetupComm(m_hComHandle, 256, 256) == false) <br> { <br> CloseHandle(m_hComHandle); <br> return false; <br> } <br> PurgeComm(m_hComHandle,<br> PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR); <br> <br> CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF; <br> CommTimeOuts.ReadTotalTimeoutMultiplier = 0; <br> CommTimeOuts.ReadTotalTimeoutConstant = 0; <br> CommTimeOuts.WriteTotalTimeoutMultiplier = 0; <br> CommTimeOuts.WriteTotalTimeoutConstant = 0; <br> SetCommTimeouts(m_hComHandle, &CommTimeOuts);<br> <br> dcb.DCBlength = sizeof(DCB); <br> if(GetCommState(m_hComHandle, &dcb) == false)<br> {<br> //do something<br> CloseHandle(m_hComHandle); <br> return false;<br> } <br> else<br> { <br> dcb.BaudRate = 9600;<br> dcb.ByteSize = 8; <br> dcb.Parity = NOPARITY; <br> dcb.StopBits = 2;<br> dcb.fOutxCtsFlow = 0;<br> dcb.fRtsControl = 0;<br><br> if(SetCommState(m_hComHandle, &dcb) == false) <br> { <br> CloseHandle(m_hComHandle);<br> return false;<br> } <br> } //END OF else<br> commstate = true;<br> return true;<br> } //END OF SetupPort()<br><br><br>bool __fastcall TForm1::closecomm(void)<br>{<br> if(commstate)<br> {<br> if(m_hComHandle != INVALID_HANDLE_VALUE)<br> {<br> if(CloseHandle(m_hComHandle))<br> {<br> commstate = false;<br> }<br> else<br> {<br> return false;<br> }<br> }<br> }<br> return true;<br>}<br>//---------------------------------------------------------------------------<br><br>void __fastcall TForm1::sendandrev(unsigned char* sendchar,unsigned char *sendlen,unsigned char* revchar,unsigned char *revlen)<br>{<br> unsigned char j;<br> unsigned char i;<br> DWORD retlen;<br> bool state;<br> if(!commstate)<br> {<br> return;<br> }<br> settimeout(100,100);<br> *revlen = 0;<br> PurgeComm(m_hComHandle, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);<br><br> state = WriteFile(m_hComHandle,sendchar,*sendlen,&retlen,NULL);<br> if(!state)<br> {<br> *revlen = 0;<br> *sendlen = 0;<br> }<br><br> COMSTAT ComStat;<br> DWORD dwBytesRead,dwErrorFlags;<br><br> //等待50毫秒,如果没有数据传回来就退出,最多只接收10个数据<br> mydelayms(10);<br> j=0;<br> for(i = 0;i<5;i++)<br> {<br> mydelayms(1);<br> //读取串行设备的当前状态<br> ClearCommError(m_hComHandle,&dwErrorFlags,&ComStat);<br> //应该读取的数据长度<br> dwBytesRead=ComStat.cbInQue;<br> if(dwBytesRead > 10)<br> {<br> dwBytesRead = 10;<br> }<br> if(dwBytesRead>0)<br> {<br> i = 0;<br> if(!ReadFile(m_hComHandle,&(revchar[j]),dwBytesRead,&dwBytesRead,NULL))<br> {<br> dwBytesRead=0;<br> }<br><br> j += (int)dwBytesRead;<br> *revlen = (unsigned char)j;<br> }<br> }<br>}<br><br>void __fastcall TForm1::Button2Click(TObject *Sender)<br>{<br> int i;<br> unsigned char sendlen;<br> unsigned char revlen;<br><br><br> unsigned char senddata[20];<br> unsigned char revdata[10];<br> if(commindex->Items->Count<1)<br> {<br> Application->MessageBox("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);<br> return;<br> }<br> if(!opencomm(commindex->ItemIndex+1))<br> {<br> Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", MB_OK+MB_ICONINFORMATION);<br> return;<br> }<br> //发送<br><br> senddata[0] = 0xaa;<br> senddata[1] = 0xaa;<br> if(CheckBox1->Checked)<br> {<br> senddata[2] = 0x00;<br> senddata[3] = 0x00;<br> }<br> else<br> {<br> if(Edit2->Text.Length() == 0)<br> {<br> Application->MessageBox("原设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);<br> return;<br> }<br> if(!isrightint(Edit2->Text))<br> {<br> Application->MessageBox("原设备号中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);<br> return;<br> }<br> i = Edit2->Text.ToInt();<br> senddata[2] = i % 256;<br> senddata[3] = (i % 65536)/256;<br> }<br> senddata[4] = 0xf0;<br> if(Edit4->Text.Length() == 0)<br> {<br> Application->MessageBox("新设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);<br> return;<br> }<br> if(!isrightint(Edit4->Text))<br> {<br> Application->MessageBox("新设备号中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);<br> return;<br> }<br> i = Edit4->Text.ToInt();<br> senddata[5] = i % 256;<br> senddata[6] = (i % 65536)/256;<br> senddata[7] = senddata[5] ^ senddata[6];<br><br> sendlen = 8;<br> if(sendlen>0)<br> {<br> sendandrev(senddata,&sendlen,revdata,&revlen);<br> intohistory(senddata,&sendlen,revdata,&revlen);<br> }<br> else<br> {<br> return;<br> }<br> closecomm();<br> if(revlen>0)<br> {<br> if(revdata[0] == 0xf0)<br> {<br> Application->MessageBox(("机号设定成功,现在的机号为" + IntToStr((int)senddata[6]*256 + senddata[5]) + "!".c_str(), "提示", MB_OK+MB_ICONINFORMATION);<br> return;<br> }<br> }<br> Application->MessageBox("机号设定失败!", "提示", MB_OK+MB_ICONINFORMATION);<br><br>}