#ifdef HAVE_CONFIG_H # include #endif #define WIN32_LEAN_AND_MEAN 1 #include #include #include #include #include "serial.h" char *serial_names[10] = { "", "COM1:", "COM2:", "COM3:", "COM4:", "COM5:", "COM6:", "COM7:", "COM8:", NULL }; extern GtkWidget *foablak; serial_t *serial_open(const char *devname) { serial_t *serport; HANDLE fd; DWORD dwError; serport = malloc(sizeof(serial_t)); serport->fd = CreateFile(devname, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); dwError = GetLastError(); if (dwError == ERROR_FILE_NOT_FOUND) { free(serport); return NULL; } return serport; } void serial_setup(serial_t *serport) { DCB *dcb1, *dcb2; COMMTIMEOUTS *to1, *to2; DWORD dwError; to1 = &(serport->oldto); to2 = &(serport->newto); /* Initialize the DCBlength member. */ dcb1 = &(serport->olddcb); dcb2 = &(serport->newdcb); serport->olddcb.DCBlength = sizeof (DCB); /* Get the default port setting information. */ GetCommState(serport->fd, dcb1); memcpy(dcb2, dcb1, sizeof(DCB)); /* Change the DCB structure settings. */ dcb2->BaudRate = 115200; /* Current baud */ dcb2->fBinary = TRUE; /* Binary mode; no EOF check */ dcb2->fParity = TRUE; /* Enable parity checking */ dcb2->fOutxCtsFlow = FALSE; /* No CTS output flow control */ dcb2->fOutxDsrFlow = FALSE; /* No DSR output flow control */ dcb2->fDtrControl = DTR_CONTROL_DISABLE /* DTR_CONTROL_ENABLE */; /* DTR flow control type */ dcb2->fDsrSensitivity = FALSE; /* DSR sensitivity */ dcb2->fTXContinueOnXoff = TRUE; /* XOFF continues Tx */ dcb2->fOutX = FALSE; /* No XON/XOFF out flow control */ dcb2->fInX = FALSE; /* No XON/XOFF in flow control */ dcb2->fErrorChar = FALSE; /* Disable error replacement */ dcb2->fNull = FALSE; /* Disable null stripping */ dcb2->fRtsControl = RTS_CONTROL_ENABLE; /* RTS flow control */ dcb2->fAbortOnError = FALSE; /* Do not abort reads/writes error */ dcb2->ByteSize = 8; /* Number of bits/byte, 4-8 */ dcb2->Parity = NOPARITY; /* 0-4=no,odd,even,mark,space */ dcb2->StopBits = ONESTOPBIT; /* 0,1,2 = 1, 1.5, 2 */ /* Configure the port according to the specifications of the DCB structure. */ if (!SetCommState (serport->fd, dcb2)) { /* Could not configure the serial port. */ serport->err = -1; return; } /* Retrieve the time-out parameters for all read and write operations on the port. */ to1 = &(serport->oldto); to2 = &(serport->newto); GetCommTimeouts (serport->fd, to1); memcpy(to2, to1, sizeof(COMMTIMEOUTS)); /* Change the COMMTIMEOUTS structure settings. */ to2->ReadIntervalTimeout = 250; to2->ReadTotalTimeoutMultiplier = 0; to2->ReadTotalTimeoutConstant = 250; to2->WriteTotalTimeoutMultiplier = 0; to2->WriteTotalTimeoutConstant = 0; /* Set the time-out parameters for all read and write operations on the port. */ if (!SetCommTimeouts (serport->fd, to2)) { /* Could not set the time-out parameters. */ serport->err = -1; return; } } void serial_close(serial_t *serport) { SetCommState (serport->fd, &(serport->olddcb)); SetCommTimeouts (serport->fd, &(serport->oldto)); CloseHandle(serport->fd); free(serport); } void serial_write(serial_t *serport, const char *buffer, size_t n) { DWORD dwNumBytesWritten; int i; serport->err = 0; for (i = 0; i < n; i++) { WriteFile (serport->fd, /* Port handle */ &buffer[i], /* Pointer to the data to write */ 1, /* Number of bytes to write */ &dwNumBytesWritten,/* Pointer to the number of bytes written */ NULL); /* Must be NULL for Windows CE */ serport->err += dwNumBytesWritten; } if (serport->err != n) serport->err = -1; } void serial_read(serial_t *serport, char *buffer, size_t n, int wait) { DWORD dwBytesTransferred; int i; serport->err = 0; for (i = 0; i < n; i++) { ReadFile (serport->fd, /* Port handle */ &buffer[i], /* Pointer to data to read */ 1, /* Number of bytes to read */ &dwBytesTransferred, /* Pointer to number of bytes read */ NULL); /* Must be NULL for Windows CE */ serport->err += dwBytesTransferred; if (!dwBytesTransferred) { serport->err = -1; break; } } if (serport->err != n) serport->err = -1; } void serial_delay(int ms) { Sleep(ms); }