我想用下位機(jī)以中斷傳輸方式通知上位機(jī)程序動(dòng)做,可是不太穩(wěn)定,有時(shí)上位機(jī)可以收到中斷但數(shù)據(jù)都為0,有時(shí)程序就死掉了。不知什么原因,網(wǎng)上也沒(méi)有更多的關(guān)于中斷傳輸資料供參考。批量上傳下傳都沒(méi)問(wèn)題。請(qǐng)指教,謝各位大俠!
\\以下是下位機(jī)部份關(guān)鍵代碼,其它略
while(1) { if(!Key1){ //當(dāng)key1按下通知上位機(jī)軟件動(dòng)做 EX0 = 0; //CH372中斷INT連接單片機(jī)中斷INT0 CH375_WR_CMD_PORT(CMD_WR_USB_DATA5);//用中斷傳輸通知PC Delay2us( ); cnt=8; CH375_WR_DAT_PORT(cnt); //發(fā)送8個(gè)字節(jié) do{ CH375_WR_DAT_PORT(0xa5); }while(--cnt); EX0 = 1; } }
\\以下是上位機(jī)VB部份關(guān)鍵代碼 private Sub Form_Load()
'--其它代碼略 '-- '-- '-- mIndex=0 mCallInt
End Sub
Public Sub mCallInt()'此段代碼在模塊里 Dim x As Boolean x = CH375SetIntRoutine(mIndex, AddressOf mInterruptEvent) '調(diào)用中斷子程序 End Sub
'此段代碼在模塊里 Public Sub mInterruptEvent(ByRef mBuffer As INT_PARA) '中斷服務(wù)程序 Dim bool As Boolean bool = PostMessage(FormMain.hwnd, WM_KEYDOWN, 1, 0)' 中斷特征數(shù)據(jù)1則鍵被按下 End Sub
Private Type DATA_PACKET ' 自定義的命令包結(jié)構(gòu) dat(8) As Byte ' 參數(shù) End Type
'模擬調(diào)用窗體的按鍵按下事件 Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) Dim i As Integer Dim rbuf as DATA_PACKET CH375ReadInter 0, rbuf, 8 Cls For i = 0 To 8 Print rbuf.dat(i) Next End Sub