Emulator for the UART, left the write part out for brevity. As you can see it's a sort of a mix of 8051 stuff and windows API. I cleaned it up a bit, in a rush, as there was more stuff in, such as to mimic leds on the PC application window, and some error handling.
The communication thread:
DWORD CommThread(DWORD dwParam) { while(TRUE) { read_serial() // reads *and* processes data from PC's serial port
if((P4_4==1) && S0BUF) { Stufftosend[0]=1; Stufftosend[1]=S0BUF; write_serial(); TI = 1; SerialComInterrupt(); // call the 8051 interrupt handler } } return 0; }
And this read the PC's serial port and transfers the character to the 8051 interrupt code:
int read_serial(void) { DWORD dwREAD1; DWORD dwErrors; COMSTAT comStat; BOOL bResult; DWORD dwcharsREAD1; DWORD idx;
if (hComm == INVALID_HANDLE_VALUE) return FALSE; // port not available bResult = ReadFile(hComm, (LPVOID)&ComRecBuf[0], COMINBUFSIZE, &dwREAD1, NULL);
if(!bResult) { ClearCommError(hComm, &dwErrors, &comStat); //PrintfToBuf(SELDEBUG, "RS232 read-error, code=%lx\r\n", dwErrors); } else { if(dwREAD1>0) { // raise dtr line // EscapeCommFunction( hComm, SETDTR);
if(port==TERMINALCOM) // data from terminal { dwcharsREAD1 = dwREAD1; ComRecBuf[dwREAD1]=0; idx = 0; // OkMsgBox("Timer", "string>%s