#include #include int Write_ARB_USB(HANDLE port,int arb_addr,int arb_data){ unsigned char message[6]; unsigned long num_bytes; message[0] = 3<<6 | ((arb_addr >> 14) & 0x3f); message[1] = 2<<6 | ((arb_addr >> 8) & 0x3f); message[2] = 2<<6 | ((arb_addr >> 2) & 0x3f); message[3] = 2<<6 | ((arb_addr << 4) & 0x30) | ((arb_data >> 12) & 0xf); message[4] = 2<<6 | ((arb_data >> 6) & 0x3f); message[5] = 2<<6 | ((arb_data << 0) & 0x3f); WriteFile(port,message,6,&num_bytes,NULL); return 0; } int Write_ARB_USB_Burst(HANDLE port,int arb_addr,int* arb_data, int length){ unsigned char *message;//[4+2*length]; unsigned long num_bytes; int A; message = (unsigned char*)malloc((4+2*length)*sizeof(char)); message[0] = 3<<6 | ((arb_addr >> 14) & 0x3f); message[1] = 2<<6 | ((arb_addr >> 8) & 0x3f); message[2] = 2<<6 | ((arb_addr >> 2) & 0x3f); message[3] = 2<<6 | ((arb_addr << 4) & 0x30); for(A = 0; A> 10) & 0x3f); message[5+A*2] = 1<<6 | ((arb_data[A*2+1] >> 4) & 0x3f); } WriteFile(port,message,4+2*length,&num_bytes,NULL); free(message); return 0; } int Read_ARB_USB(HANDLE port,int arb_addr,int* arb_data){ unsigned char message[5]; unsigned long num_bytes; int success = 1; message[0] = 3<<6 | ((arb_addr >> 14) & 0x3f); message[1] = 1<<6 | ((arb_addr >> 8) & 0x3f); message[2] = 2<<6 | ((arb_addr >> 2) & 0x3f); message[3] = 2<<6 | ((arb_addr << 4) & 0x30); message[4] = 0; message[5] = 0; WriteFile(port,message,4,&num_bytes,NULL); num_bytes = 0; ReadFile(port,message + 4,2,&num_bytes,NULL); if(num_bytes != 2) { success = 0; printf("*"); memset(message,0,6); WriteFile(port,"\0",1,&num_bytes,NULL); ReadFile(port,message + 4,3,&num_bytes,NULL); WriteFile(port,"\0",1,&num_bytes,NULL); } //WriteFile(port,"\0",1,&num_bytes,NULL); *arb_data = ((message[4] << 8) & 0xff00) | (message[5] & 0xff); return 0; } HANDLE open_usb_port(const char* name){ DCB dcb; HANDLE hCom; DWORD dwError; BOOL fSuccess; COMMTIMEOUTS CommTimeouts; char portname[10]; int portnumber = 5; do{ sprintf(portname,"COM%d",portnumber); hCom = CreateFile(portname, GENERIC_READ | GENERIC_WRITE, 0, /* comm devices must be opened w/exclusive-access */ NULL, /* no security attrs */ OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */ 0, /* not overlapped I/O */ NULL /* hTemplate must be NULL for comm devices */ ); } while ((hCom == INVALID_HANDLE_VALUE )&& (portnumber++ <= 10)); if(hCom == INVALID_HANDLE_VALUE){ dwError = GetLastError(); printf("could not open port\n"); Sleep(2000); exit(-1); /* handle error */ } fSuccess = GetCommState(hCom, &dcb); if (!fSuccess) { printf("Could not get port state\n"); Sleep(2000); exit(-2); /* Handle the error. */ } /* Fill in the DCB: baud=115200, 8 data bits, no parity, 1 stop bit. */ dcb.BaudRate = 115200; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; fSuccess = SetCommState(hCom, &dcb); CommTimeouts.ReadIntervalTimeout=0; CommTimeouts.ReadTotalTimeoutMultiplier=20; CommTimeouts.ReadTotalTimeoutConstant=20; CommTimeouts.WriteTotalTimeoutMultiplier=0; CommTimeouts.WriteTotalTimeoutConstant=0; SetCommTimeouts(hCom,&CommTimeouts); GetCommTimeouts(hCom,&CommTimeouts); printf("Timeouts: %d,%d,%d,%d,%d", CommTimeouts.ReadIntervalTimeout, CommTimeouts.ReadTotalTimeoutMultiplier, CommTimeouts.ReadTotalTimeoutConstant, CommTimeouts.WriteTotalTimeoutMultiplier, CommTimeouts.WriteTotalTimeoutConstant); Sleep(1000); if (!fSuccess) { /* Handle the error. */ printf("Could not set port state\n"); Sleep(2000); exit(-3); } return hCom; } void close_usb_port(HANDLE hCom){ }