📄 serial2.html
字号:
<hr ALIGN=LEFT SIZE=3 WIDTH="100%">
<h3>Software API</h3>
<div class="description">
<br>You may download the following files:
<BR><BR>
<table border=0>
<tr>
<td><li><a href="tserial_event.h">tserial_event.h</a></li></td>
<td>Header file for the serial_event communication object </td>
</tr>
<tr>
<td><li><a href="tserial_event.cpp">tserial_event.cpp</a></li></td>
<td>C++ source file for the serial_event communication object</td>
</tr>
<tr>
<td><li><a href="serialtest.cpp">serialtest.cpp</a></li></td>
<td>Simple application using the serial_event communication object</td>
</tr>
</table>
<br>
<br>
</div>
<h4>Methods</h4>
<div class="description">
<table BORDER >
<tr>
<th>Function</font></th>
<th>Description</th>
</tr>
<tr>
<td>Tserial_event()</td>
<td>Object creation</td>
</tr>
<tr>
<td>~Tserial_event()</td>
<td>Object destruction</td>
</tr>
<tr>
<td>int connect (char *port_arg, int rate_arg, serial_parity parity_arg,
int ByteSize, bool modem_events)</td>
<td>Serial port setup and start of connection
<ul>
<li>port_arg : "COM1", "COM2", ...</li>
<li>rate_arg : 19200, 9600, ...</li>
<li>parity_arg : spNONE, spODD, spEVEN</li>
<li>ByteSize : size of data transmitted (7 or 8 bits)</li>
<li>modem_event : indicates if you want to receive CD and RING events</li>
</ul>
Returns 0 if no error occured</td>
</tr>
<tr>
<td>void setManager(type_myCallBack manager)</td>
<td>Specify the event manager of this communication object. This is the
address of the callback function.</td>
</tr>
<tr>
<td>void setRxSize(int size)</td>
<td>Specifiy the size of the data to read from the port. Note that this
might not always be the size of the data actually read. To know this value,
use the getDataInSize().</td>
</tr>
<tr>
<td>void sendData(char *buffer, int size)</td>
<td>Transmit the message array to the output driver. This buffer is copied
internally and should not be longer than SERIAL_MAX_TX, so the buffer may
be freed directly after the call. You should never call this function is
you have not received a SERIAL_DATA_SENT event (or if it's the first write).</td>
</tr>
<tr>
<td>int getNbrOfBytes (void)</td>
<td>Returns the number of bytes waiting in the input buffer. Not very usefull.</td>
</tr>
<tr>
<td>int getDataInSize (void)</td>
<td>Returns the number of data that have been read.</td>
</tr>
<tr>
<td>char *getDataInBuffer (void)</td>
<td>Returns a pointer to the buffer where the data read are stored. Since
this memory location is used by the serial object, you MUST call dataHasBeenRead()
once you finished reading the data, and you should not access the buffer
after that. </td>
</tr>
<tr>
<td>void dataHasBeenRead (void)</td>
<td>Calling this function will tell the serial object that you have read
the incoming data and that it can continue to read further incoming data.
If you do not call this function, the port is not read anymore.</td>
</tr>
<tr>
<td>void disconnect (void)</td>
<td>Disconnect the serial port.</td>
</tr>
</table>
<br>
<br>
</div>
<h4>Events</h4>
<div class="description">
When an event occurs, the serial object call the callback function (if
not null). This function must have the following prototype:
<pre>void SerialEventManager(uint32 object, uint32 event)</pre>
where <i>uint32 </i> is an unsigned long.<br>
<br>
The <b>object</b> field is the address of the serial communication object
that has called the callback function.
<br><b>event</b> is the event kind that occured. It can be any of the seven
values described above. For six of them, the event is notified with no
additionnal data. The SERIAL_DATA_ARRIVAL is associated with data. These
one can be retrieved by calling <b>getDataInSize</b> and <b>getDataInBuffer</b>,
like described hereafter.
<br>
<pre>
void SerialEventManager(uint32 object, uint32 event)
{
char *buffer;
int size;
Tserial_event *com;
com = (Tserial_event *) object;
if (com!=0)
{
switch(event)
{
case SERIAL_CONNECTED :
printf("Connected ! \n");
break;
case SERIAL_DISCONNECTED :
printf("Disonnected ! \n");
break;
case SERIAL_DATA_SENT :
printf("Data sent ! \n");
break;
case SERIAL_RING :
printf("DRING ! \n");
break;
case SERIAL_CD_ON :
printf("Carrier Detected ! \n");
break;
case SERIAL_CD_OFF :
printf("No more carrier ! \n");
break;
case SERIAL_DATA_ARRIVAL :
size = com->getDataInSize();
buffer = com->getDataInBuffer();
OnDataArrival(size, buffer);
com->dataHasBeenRead();
break;
}
}
}
</pre>
</div>
<H4>Interfacing with C++</H4>
<div class="description">
One of the major problem encoutered here is the fact that the SerialEventManager
functio is a C function and not the method of a C++ object. Using C++ callback
is not as easy as using C callback.<BR>
In order to solve partially this problem, use the owner field of the Tserial_event
class. You can give him the address of a C++ object to be called by the callback
function.<BR>
<pre>
com->owner = (void *) object1;
</pre>
Where object1 is a pointer to a Tmyobject having a method named <BR>
<pre>SerialCallback(Tserial_event *com_source, uint32 event);</pre>
So in the callback function you can do the following stuff:
<pre>
void SerialEventManager(uint32 object, uint32 event)
{
Tserial_event *com;
Tmyobject *object;
com = (Tserial_event *) object;
if (com!=0)
{
object = (Tmyobject *) com->owner;
if (object!=0)
object->SerialCallback(com, event);
}
}
</pre>
</div>
<BR><BR><HR width="80%"><BR><center>Tetraedre Company Copyright ©2000-2003</center><BR></td><td width=20> </td></tr></table></center></tr></table><BR><BR><center><a href="/WEB/advanced/serial2.php3"><font color='#FFFFFF'>BACK</font></a></center><BR></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -