File: DSR-workaround.java

package info (click to toggle)
rxtx 2.2pre2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 6,960 kB
  • sloc: ansic: 14,367; sh: 10,742; java: 7,629; cpp: 2,717; makefile: 143
file content (164 lines) | stat: -rw-r--r-- 5,021 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
The following appears to work for Ken Eisner <019769e@acadiau.ca>
-----
under a windows platform we can check for lost of DSR and it is
consistant however,  under linux it is not consistant.  We rarely and
infrequently get loss of DSR.
-----
 We found a way around the inconsistant
 detection / loss of DSR.  We decided to check the DSR status in the
 OUTPUT_BUFFER_EMPTY event (I think that is where we put it).
-----
Trent, here is the code which works for us.

Excuse the formating.. damned thing won't space things properly...

Let me know if you need anything else

-Ken

 /**
  * Procedure:   void serialEvent(SerialPortEvent event)
  * Params:    SerialPortEvent event
  *
  * Define what we are doing when one of these events are fired
 **/
  public void serialEvent(SerialPortEvent event)
  {
    int numBytes = 0;
    int offset = 0;

    switch(event.getEventType())
    {
      case SerialPortEvent.BI:
      case SerialPortEvent.OE:
      case SerialPortEvent.FE:
      case SerialPortEvent.PE:
      case SerialPortEvent.CD:
      case SerialPortEvent.RI:
      case SerialPortEvent.CTS:
        break;
      case SerialPortEvent.DSR:
          break;
      case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        /*
         * HACK: This is designed to work around the inconsistent loss of
DSR
         *       and inconsistent detection of DSR if program running before
         *       cable connected
         *
         * Here we check if the DSR flag is high.
         *   If it is then we check to see if our class var is alread true,
         *     meaning that we have a connection.
         *     If our class var got_dsr is false then we set our class var
         *       got_dsr to true, lost_dsr to false
         *     Else the we already have a connection so just set the class
var
         *       got_dsr to true
         *   Else we check if we already have DSR
         *     If we do then we set the lost_dsr to true, and got_dsr to
false
         *     meaning that we lost DSR
         */
        if (serial_port_.isDSR() == true)
        {
           if (got_dsr == false) /* new connection */
           {
              got_dsr = true;
              System.out.println("\nReceived DSR -- BEGIN OF
TRANSMISSION\n");
              lost_dsr = false;
           }
           else /* old connection */
               got_dsr = true;
        }
        else
        {
           /* already got connection */
           if (got_dsr == true)
           {
      /*
       * Check if we have a bad call
       *
       * 1.) Get the current time (in milliseconds)
       * 2.) If the current time is less than the time of the last
       *     character read then we have major problems
       * 3.) Otherwise, if the current time less the time of the last
       *     char read is less than the length of the end tone then we
       *     have a bad call.
       */
           Date now = new Date();
           long current_time = now.getTime();
           if (current_time <= time_of_last_char)
                System.err.println("CommReader() - Current time is before
last char");
          else
          {
              if ((current_time - time_of_last_char) <= end_tone_length_)
              {
                  System.out.println("\nInterrupted transmission");
                  this.passed_client_checks_ = false;
              }
              else
              {
                  System.out.println("\nGood transmission");
                  this.passed_client_checks_ = true;
              }
      }

      /*
       * Stick a fork in us, we're done here.
       */
       sender_.close_zipfile();

      /*
       * Set lost_dsr to true and got_dsr to false
       * Set finished_receiving_data to true
       */
            lost_dsr = true;
            System.out.println("Lost DSR -- END OF TRANSMISSION");
            got_dsr = false;
      this.finished_receiving_data_ = true;
          }
        }
        break;
      case SerialPortEvent.DATA_AVAILABLE:
        byte[] readBuffer = new byte[2048];

        try
        {
      /*
      * Read while there is something to read, stop when done
      */
          while (is_.available() > 0)
          {
            offset = 0;
            numBytes = is_.read(readBuffer);
      Date now = new Date();
      time_of_last_char = now.getTime();

            if (numBytes > 0)
            {
        /*
        * The first character detected is some weird ^@ thing which we
        * want to ignore so we set the offset to 1 and reduce the
        * numBytes read by one
        */
              if (readBuffer[0] == 0)
        {
                offset = 1;
                numBytes--;
              }
              System.out.print(new String (readBuffer , offset, numBytes));
       sender_.write(readBuffer, offset, numBytes);
            }
          }
        }
        catch (Exception e)
        {
          System.err.println(e.getMessage());
          e.printStackTrace();
        }
        break;
    } /* end switch */
  }