void BitButton10OnClick(tWidget *pWidget) { //--- Close the existing handle --- if(hOpen>=0) { uart_Close(hOpen); hOpen = -1; } //--- Establish a new handle --- if(hOpen<0) { hOpen = uart_Open("COM1,9600,N,8,1"); } //--- If success, display current COM Port settings on screen --- if(hOpen>=0) { LabelTextSet(&Label8, "COM1,9600,N,8,1"); uart_SetTimeOut(hOpen, 300); } /* if(hOpen>=0) { BOOL ret; unsigned char *Buffer = {'AA'};//, '55', '04', '01', '03', '19', '20'}; char* buf; ByteToChar(Buffer, buf, 1); ret = uart_Send(hOpen, buf); } */ }
DMPAPI(bool) uart_Init(void *vport) { SerialPort *port = (SerialPort *)vport; if (vx86_uart_Init(port->com) == false) { err_print((char*)"%s: Vertex86 COM%d init fail!!\n", __FUNCTION__, port->com + 1); goto VX86_UART_INIT_FAIL; } if (uart_IsOK(port) == false) { err_print((char*)"%s: COM%d doesn't exist.\n", __FUNCTION__, port->com + 1); goto CHECK_EXIST_FAIL; } if (irq_Init() == false) { err_print((char*)"%s: IRQ init fail.\n", __FUNCTION__); goto IRQ_INIT_FAIL; } // save old UART config port->old_lsb = port->lsb = _16550_DLAB_In(port, port->DLLSB); port->old_msb = port->msb = _16550_DLAB_In(port, port->DLMSB); port->old_ier = port->ier = io_inpb(port->IER); port->old_lcr = port->lcr = io_inpb(port->LCR); port->old_mcr = port->mcr = io_inpb(port->MCR); port->old_TimeOut = port->TimeOut = UART_NO_TIMEOUT; // UART initial uart_SetBaud(vport, UARTBAUD_115200BPS); uart_SetFormat(vport, BYTESIZE8 + STOPBIT1 + NOPARITY); uart_SetTimeOut(vport, UART_NO_TIMEOUT); // flush rx & tx queue uart_FlushRxQueue(vport); uart_FlushTxQueue(vport); // clear Hardware RX & TX FIFO uart_ClearRFIFO(vport); uart_ClearWFIFO(vport); // enable FIFO mode uart_SetHWFIFO(vport, ENABLE_HW_FIFO_16); // enable receive data interrupt uart_IntEnable(vport, IERALL); // set flow control uart_SetFlowControl(vport, NO_CONTROL); port->InUse = 1; return true; IRQ_INIT_FAIL: CHECK_EXIST_FAIL: vx86_uart_Close(port->com); VX86_UART_INIT_FAIL: if (io_Close() == false) err_print((char*)"Close IO lib error!!\n"); return false; }
void BitButton12OnClick(tWidget *pWidget) { //--- Close the existing handle --- if(hOpen>=0) { uart_Close(hOpen); hOpen = -1; } //--- Establish a new handle --- if(hOpen<0) { hOpen = uart_Open("COM1,9600,N,8,1"); } //--- If success, display current COM Port settings on screen --- if(hOpen>=0) { LabelTextSet(&Label8, "COM1,9600,N,8,1"); uart_SetTimeOut(hOpen, 300); } if(hOpen>=0) { BOOL ret; char Buffer[1000]; //char buf[]="AA 55 04 01 03 19 20"; //char *test = strtok(buf, " "); unsigned char buf[]={0xAA, 0x55, 0x04, 0x01, 0x03, 0x19, 0x20}; //int str_len = strlen(buf); char *ends=(char*)buf; ends[7]='\0'; /* for(int i=0;i<str_len;i++) { if(buf[i]!=' ') { Buffer[i] = strtol(buf[i], &ends, 16); } } Buffer[str_len]=""; */ //LabelTextSet(&Label8, (char*)10); //for(int nBytes=0;nBytes<str_len;nBytes++) //Buffer[nBytes] = strtol(test[nBytes], &ends, 16); //char* buf="p"; //ByteToChar(Buffer, buf, 7); //char *s = "70", *ends; //char c = (char)strtol(s, &ends, 16); ret = uart_Send(hOpen, ends); } }
DMPAPI(void) uart_Close(void *vport) { unsigned char lcr; SerialPort *port = (SerialPort *)vport; if (port == NULL) { err_print((char*)"%s: port is null.\n", __FUNCTION__); return; } if (port->InUse != 0) { // restore old IER & MCR uart_IntDisable(vport); io_DisableINT(); { io_outpb(port->MCR, port->old_mcr); } io_RestoreINT(); irq_Close(); // restore old LSB & MSB #ifdef _VORTEX86EXC_UART_WORKAROUND /* To avoid Vortex86EX(D) write sync. issue. */ io_DisableINT(); { lcr = io_inpb(port->LCR); io_outpb(port->LCR, 0x80); do { io_outpb(port->DLLSB, port->old_lsb); } while (io_inpb(port->DLLSB) != port->old_lsb); do { io_outpb(port->DLMSB, port->old_msb); } while (io_inpb(port->DLMSB) != port->old_msb); io_inpb(0x80); // do IO delay io_outpb(port->LCR, lcr); } io_RestoreINT(); #else _16550_DLAB_Out(port, port->DLLSB, port->old_lsb); _16550_DLAB_Out(port, port->DLMSB, port->old_msb); #endif // restore old LCR & timeout uart_SetFormat(vport, port->old_lcr); uart_SetTimeOut(vport, port->old_TimeOut); vx86_uart_Close(port->com); if (io_Close() == false) err_print((char*)"Close IO lib error!!\n"); } uart_Free(port); }