LRESULT CALLBACK BitBanger(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: //IDC_PRINTMON if (!strlen(SerialCaptureFile)) strcpy(SerialCaptureFile,"No Capture File"); SendDlgItemMessage(hDlg,IDC_SERIALFILE,WM_SETTEXT,strlen(SerialCaptureFile),(LPARAM)(LPCSTR)SerialCaptureFile); SendDlgItemMessage(hDlg,IDC_LF,BM_SETCHECK,TextMode,0); SendDlgItemMessage(hDlg,IDC_PRINTMON,BM_SETCHECK,PrtMon,0); break; case WM_COMMAND: switch (LOWORD(wParam)) { case IDC_OPEN: SelectFile(SerialCaptureFile); SendDlgItemMessage(hDlg,IDC_SERIALFILE,WM_SETTEXT,strlen(SerialCaptureFile),(LPARAM)(LPCSTR)SerialCaptureFile); break; case IDC_CLOSE: ClosePrintFile(); strcpy(SerialCaptureFile,"No Capture File"); SendDlgItemMessage(hDlg,IDC_SERIALFILE,WM_SETTEXT,strlen(SerialCaptureFile),(LPARAM)(LPCSTR)SerialCaptureFile); PrtMon=FALSE; SetMonState(PrtMon); SendDlgItemMessage(hDlg,IDC_PRINTMON,BM_SETCHECK,PrtMon,0); break; case IDC_LF: TextMode=(char)SendDlgItemMessage(hDlg,IDC_LF,BM_GETCHECK,0,0); SetSerialParams(TextMode); break; case IDC_PRINTMON: PrtMon=(char)SendDlgItemMessage(hDlg,IDC_PRINTMON,BM_GETCHECK,0,0); SetMonState(PrtMon); } //End switch wParam break; } return(0); }
int RS232Interface::OpenSerial(int no) { UserDebug1(UserApp1, "RS232Interface::OpenSerial(%d) I\n", no); int ret_val = E2ERR_OPENFAILED; if (no >= 1 && no <= MAX_COMPORTS) { #ifdef _WINDOWS char str[MAXPATH]; snprintf(str, MAXPATH, "%s%d", profile->GetDevName(), no); hCom = CreateFile(str, 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 */ ); if ( hCom != INVALID_HANDLE_VALUE ) { GetCommState(hCom, &old_dcb); GetCommTimeouts(hCom, &old_timeout); GetCommMask(hCom, &old_mask); if (wait_endTX_mode) SetCommMask(hCom, EV_TXEMPTY); else SetCommMask(hCom, 0); SetSerialTimeouts(); SetSerialParams(); ret_val = OK; } #else #ifdef _LINUX_ char devname[MAXPATH]; int chars_read; no--; //linux call ttyS0 --> COM1, ttyS1 --> COM2, etc.. // implement device locking in /var/lock/LCK..ttySx snprintf(lockname, MAXPATH, "%s/LCK..%s%d", profile->GetLockDir(), profile->GetDevName(), no); UserDebug1(UserApp2, "RS232Interface::OpenSerial() now lock the device %s\n", lockname); fd = open ((const char *)lockname,O_RDWR|O_EXCL|O_CREAT); if (fd < 0) { fd = open ((const char *)lockname,O_RDONLY); lockname[0]=0; UserDebug1(UserApp2, "RS232Interface::OpenSerial Can't lock port %d\n", no); if (fd < 0) return ret_val; chars_read = read(fd,devname,MAXLINESIZE-1); devname[chars_read]=0; UserDebug1(UserApp2, "RS232Interface::OpenSeriial locked by %s\n", devname); close(fd); return ret_val; } snprintf(devname, MAXPATH, "%10d\n", (int) getpid() ); write(fd, devname, strlen(devname)); close(fd); fd = INVALID_HANDLE_VALUE; snprintf(devname, MAXPATH, "%s/%s%d", profile->GetDevDir(), profile->GetDevName(), no); UserDebug1(UserApp2, "RS232Interface::OpenSerial() now open the device %s\n", devname); fd = open ((const char *)devname, O_RDWR|O_NONBLOCK|O_EXCL); // fd = open ((const char *)devname, O_RDWR); UserDebug1(UserApp2, "RS232Interface::OpenSerial open result = %d\n", fd); if (fd < 0) { UserDebug1(UserApp2, "RS232Interface::OpenSerial can't open the device %s\n", devname); unlink(lockname); return ret_val; } // Check for the needed IOCTLS #if defined(TIOCSBRK) && defined(TIOCCBRK) //check if available for compilation // Check if available during runtime if ((ioctl(fd,TIOCSBRK,0) == -1) || (ioctl(fd,TIOCCBRK,0) == -1)) { UserDebug(UserApp2, "RS232Interface::OpenPort IOCTL not available\n"); return ret_val; } #else close(fd); fd = INVALID_HANDLE_VALUE; unlink(lockname); return ret_val; #endif /*TIOCSBRK*/ /* open sets RTS and DTR, reset it */ #if defined(TIOCMGET) && defined(TIOCMSET) //check if available for compilation int flags; if (ioctl(fd,TIOCMGET, &flags)== -1) { UserDebug(UserApp2, "RS232Interface::OpenPort IOCTL not available\n"); close(fd); fd = INVALID_HANDLE_VALUE; unlink(lockname); return ret_val; } else { flags &= ~(TIOCM_RTS|TIOCM_DTR); if (ioctl(fd,TIOCMSET, &flags)== -1) { UserDebug(UserApp2, "RS232Interface::OpenPort IOCTL not available\n"); close(fd); fd = INVALID_HANDLE_VALUE; unlink(lockname); return ret_val; } } #endif /*TIOCMGET */ UserDebug(UserApp2, "RS232Interface::OpenPort GETATTR\n"); if (tcgetattr(fd, &old_termios) == -1) { UserDebug(UserApp2, "RS232Interface::OpenPort GETATTR failed\n"); close(fd); fd = INVALID_HANDLE_VALUE; unlink(lockname); return ret_val; } UserDebug(UserApp2, "RS232Interface::OpenPort SetTimeouts && Params\n"); if ( SetSerialTimeouts() != OK ) { UserDebug(UserApp2, "RS232Interface::OpenPort SetSerialTimeouts() failed\n"); close(fd); fd = INVALID_HANDLE_VALUE; unlink(lockname); } else if ( SetSerialParams() != OK ) { UserDebug(UserApp2, "RS232Interface::OpenPort SetSerialParams() failed\n"); close(fd); fd = INVALID_HANDLE_VALUE; unlink(lockname); } else { fd_clear_flag(fd, O_NONBLOCK); //Restore to blocking mode ret_val = OK; } #endif /*_LINUX_*/ #endif } UserDebug1(UserApp2, "RS232Interface::OpenSerial() = %d O\n", ret_val); return ret_val; }