00001 #ifndef __J2K__SerialPort_HPP__
00002 #define __J2K__SerialPort_HPP__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #define MODEM_DELAY 100
00013
00014
00015
00016
00017
00018 #define MAX_SERIAL_TASK 30
00019
00020 struct SerialTask_t {
00021 int cmd;
00022 int id;
00023 int valid;
00024 };
00025
00026 class SerialPort {
00027 public:
00028
00029 SerialPort() {
00030
00031 modem = open( "//2/dev/ser1", O_RDWR );
00032 Task = new SerialTask_t[ MAX_SERIAL_TASK ];
00033
00034 for( int i = 0; i < MAX_SERIAL_TASK; i++ ) {
00035 Task[i].cmd = 0;
00036 Task[i].id = 0;
00037 Task[i].valid = 0;
00038 }
00039
00040 bottom = 0;
00041 Waiting = 0;
00042
00043 curr.cmd = 0;
00044 curr.id = 0;
00045 curr.valid = 0;
00046
00047 TrainCmd( 96, -1 );
00048 }
00049
00050
00051 virtual ~SerialPort() {
00052 delete [] Task;
00053 TrainCmd( 97, -1 );
00054 }
00055
00056 void TrainCmd( int cmd, int id ) {
00057 buf[0] = cmd;
00058
00059 if ( id > 0 ) {
00060 buf[1] = id;
00061 write( modem, &buf, 2 );
00062 } else {
00063 write( modem, &buf, 1 );
00064 }
00065
00066 Waiting = 1;
00067 delay( MODEM_DELAY );
00068
00069 Waiting = 0;
00070 }
00071
00072 void AddTask( int cmd, int id ) {
00073 if ( Waiting == 0 ) {
00074 TrainCmd( cmd, id );
00075 } else if ( bottom >= MAX_SERIAL_TASK ) {
00076 printf("\n*** Error: Reach the end of the array *** ");
00077 printf("\n*** Can't Add more Serial Task ! ***\n");
00078 return;
00079 } else {
00080 ++bottom;
00081 Task[ bottom ].cmd = cmd;
00082 Task[ bottom ].id = id;
00083 Task[ bottom ].valid = 1;
00084 }
00085 }
00086
00087 void ExecuteTask() {
00088 if ( bottom < 1 ) return;
00089 if ( Task[0].valid == 0 ) return;
00090
00091 curr.cmd = Task[0].cmd;
00092 curr.id = Task[0].id;
00093
00094 for( int k = 0; k < bottom; k++ ) {
00095 Task[k].cmd = Task[k+1].cmd;
00096 Task[k].id = Task[k+1].id;
00097 Task[k].valid = Task[k+1].valid;
00098 }
00099
00100 Task[bottom].cmd = 0;
00101 Task[bottom].id = 0;
00102 Task[botton].valid = 0;
00103
00104 bottom--;
00105
00106 curr.valid = 0;
00107 TrainCmd( curr.cmd, curr.id );
00108 }
00109
00110 private:
00111 int modem;
00112 char buf[3];
00113
00114 SerialTask_t* Task;
00115 SerialTask_t curr;
00116 int bottom;
00117 int Waiting;
00118 };
00119
00120 #endif