PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PComPort.cpp
Go to the documentation of this file.
1/* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/devices/PParellelPort.cpp
4// Purpose: Class for handling parallel port
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2011-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
28#ifdef PEBL_USEPORTS
29#include "PComPort.h"
30
31#include "../utility/rs232.h"
32#include "../utility/PError.h"
33
34
35#if defined (PEBL_LINUX)
36#include <sys/io.h>
37#endif
38
39// Undefine Windows API SetPort macro that conflicts with our method name
40#ifdef SetPort
41#undef SetPort
42#endif
43
44using std::ostream;
45using std::fstream;
46using std::flush;
47using std::string;
48using std::cout;
49using std::endl;
50
51
52PComPort::PComPort(unsigned int port,
53 unsigned int baud,
54 std::string mode):
55 mMode(mode),
56 mIsOpen(false)
57{
58 mCDT = CDT_COMPORT;
59 SetPort(port,baud);
60}
61
62
63PComPort::~PComPort()
64{
65 if(mIsOpen)
66 RS232_CloseComport(mPort);
67 mIsOpen = false;
68}
69
70
71
72void PComPort::Init()
73{
74
75 //init
76 if(!mIsOpen)
77 {
78
79 //mode 8N1 was pre-2.0 default
80 //flowctrl=0 means no hardware flow control (RTS/CTS disabled)
81 int out = RS232_OpenComport(mPort,mBaud,mMode.c_str(),0);
82
83 mIsOpen = true;
84 std::cerr << "Initiating comport ["<< mPortName<<"] Return value :["<<out<<"]\n";
85
86
87 if(out)
88 {
89 PError::SignalFatalError("Unable to Open Com Port. Make sure script is run with root access.\n");
90 }
91 }
92
93}
94
95#ifdef SetPort
96#undef SetPort // Undefine Windows API macro that conflicts with our method
97#endif
98
99void PComPort::SetPort(unsigned int portnum,unsigned int baud)
100{
101
102#ifdef PEBL_LINUX
103
104 int bauds[23] ={50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400,57600,115200,230400,
105 460800,500000,576000,921600,1000000};
106
107 if(portnum < 16)
108 {
109 mPortName = Variant("ttyS")+Variant((int)portnum);
110 }
111 else if(portnum < 22)
112 {
113 mPortName = Variant("ttyUSB")+Variant((int)(portnum-16));
114 }
115 else
116 {
117 PError::SignalFatalError(Variant("Attempted to set improper port number:")+Variant((int)portnum));
118 }
119
120
121#else
122 int bauds[19] ={0,0,110,0,0,0,300,600,1200,0,2400,4800,9600,19200,38400,57600,115200,128000,256000};
123
124 if(portnum < 16)
125 {
126 mPortName = Variant("COM")+Variant((int)(portnum+1));
127 }
128#endif
129 //baud rate should be checked against legal bauds on each platform.
130 mBaud = baud;
131 mPort = portnum;
132}
133
134
135void PComPort::PSendByte(unsigned char byte)
136{
137 int check = RS232_SendByte(mPort,byte);
138
139 //if check == 1, nothing was sent
140}
141
142
143int PComPort::GetByte(unsigned char& out)
144{
145
146 int check = RS232_PollComport(mPort,&out,1);
147 return check;
148
149}
150
151
152
153unsigned char PComPort::GetByte()
154{
155 unsigned char out;
156 int check = RS232_PollComport(mPort,&out,1);
157 if(check==0)
158 {
159 return 0;
160 } else
161 {
162
163 return out;
164 }
165}
166
167
168
169
170Variant PComPort::GetBytes(int n)
171{
172
173
174 unsigned char *out;
175 out = new unsigned char[n];
176
177 int check = RS232_PollComport(mPort,out,n);
178 Variant got = Variant(out);
179 // int get = n;
180 // Variant got ="";
181 // while(get > 0)
182 // {
183 // int x = get>100?100:get;
184 //
185 // int check = PollComport(mPort,&out,get);
186 // got = got + Variant(out[]);
187 // get -= check;
188 // }
189
190 return got;
191}
192
193Variant PComPort::GetPortName()
194{
195 return mPortName;
196}
197
198void PComPort::Close()
199{
200
201 RS232_CloseComport(mPort);
202}
203
204
205
206
207ostream & PComPort::SendToStream(ostream & out) const
208{
209 out << "<Generic Com Port Object>" << flush;
210 return out;
211}
212
213
214#endif
@ CDT_COMPORT
Definition PEBLObject.h:66
void SignalFatalError(const std::string &message)
void RS232_CloseComport(int)
int RS232_OpenComport(int, int, const char *, int)
int RS232_PollComport(int, unsigned char *, int)
int RS232_SendByte(int, unsigned char)