Rev 434 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 434 | Rev 444 | ||
|---|---|---|---|
| 1 | using System; |
1 | using System; |
| 2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
| 3 | using System.Text; |
3 | using System.Text; |
| 4 | using System.IO.Ports; |
4 | using System.IO.Ports; |
| 5 | using System.Net; |
5 | using System.Net; |
| 6 | using System.Net.Sockets; |
6 | using System.Net.Sockets; |
| 7 | 7 | ||
| 8 | public class SerialConnection { |
8 | public class SerialConnection { |
| 9 | const byte DEBUG_LEVEL = 3; //0, 1, 2, 3 |
9 | const byte DEBUG_LEVEL = 3; //0, 1, 2, 3 |
| 10 | 10 | ||
| 11 | public const byte UART_START_BYTE = 253; |
11 | public const byte UART_START_BYTE = 253; |
| 12 | public const byte UART_END_BYTE = 250; |
12 | public const byte UART_END_BYTE = 250; |
| 13 | public const int PACKET_LENGTH = 17; |
13 | public const int PACKET_LENGTH = 17; |
| 14 | private const int readBufferSize = 8192; |
14 | private const int readBufferSize = 8192; |
| 15 | public const byte UART_CONN_BYTE = 252; |
15 | public const byte UART_CONN_BYTE = 252; |
| 16 | 16 | ||
| 17 | private Socket udpserver = null; |
17 | private Socket udpserver = null; |
| 18 | private IPEndPoint iep = null; |
18 | private IPEndPoint iep = null; |
| 19 | private string remoteIP = null; |
19 | private string remoteIP = null; |
| 20 | private int remotePort = 1100; |
20 | private int remotePort = 1100; |
| 21 | 21 | ||
| 22 | private SerialPort serial_conn = null; |
22 | private SerialPort serial_conn = null; |
| 23 | private byte[] iBuff = new byte[8192]; |
23 | private byte[] iBuff = new byte[8192]; |
| 24 | private int iBuffPointer = 0; |
24 | private int iBuffPointer = 0; |
| 25 | 25 | ||
| 26 | public SerialConnection() { } |
26 | public SerialConnection() { } |
| 27 | 27 | ||
| 28 | public bool open() { |
28 | public bool open() { |
| 29 | if (serial_conn != null) { |
29 | if (serial_conn != null) { |
| 30 | if (!serial_conn.IsOpen) serial_conn.Open(); |
30 | if (!serial_conn.IsOpen) serial_conn.Open(); |
| 31 | return true; |
31 | return true; |
| 32 | } else if (udpserver != null) { |
32 | } else if (udpserver != null) { |
| 33 | return true; |
33 | return true; |
| 34 | } else { |
34 | } else { |
| 35 | return false; |
35 | return false; |
| 36 | } |
36 | } |
| 37 | } |
37 | } |
| 38 | 38 | ||
| 39 | public bool close() { |
39 | public bool close() { |
| 40 | if (serial_conn != null) { |
40 | if (serial_conn != null) { |
| 41 | if (serial_conn.IsOpen) serial_conn.Close(); |
41 | if (serial_conn.IsOpen) serial_conn.Close(); |
| 42 | return true; |
42 | return true; |
| 43 | } else if (udpserver != null) { |
43 | } else if (udpserver != null) { |
| 44 | udpserver.Close(); |
44 | udpserver.Close(); |
| 45 | return true; |
45 | return true; |
| 46 | } else { |
46 | } else { |
| 47 | return false; |
47 | return false; |
| 48 | } |
48 | } |
| 49 | 49 | ||
| 50 | } |
50 | } |
| 51 | 51 | ||
| 52 | public bool isOpen() { |
52 | public bool isOpen() { |
| 53 | if (serial_conn != null) { |
53 | if (serial_conn != null) { |
| 54 | return serial_conn.IsOpen; |
54 | return serial_conn.IsOpen; |
| 55 | } else if (udpserver != null) { |
55 | } else if (udpserver != null) { |
| 56 | return true; |
56 | return true; |
| 57 | } else { |
57 | } else { |
| 58 | return false; |
58 | return false; |
| 59 | } |
59 | } |
| 60 | } |
60 | } |
| 61 | 61 | ||
| 62 | public void setConfiguration(int baudRate, Parity parity, string portName, StopBits stopbits, int databits, bool rtsEnable) { |
62 | public void setConfiguration(int baudRate, Parity parity, string portName, StopBits stopbits, int databits, bool rtsEnable) { |
| 63 | if (DEBUG_LEVEL>1) { Console.WriteLine("Connecting to target..."); } |
63 | if (DEBUG_LEVEL>1) { Console.WriteLine("Connecting to target..."); } |
| 64 | 64 | ||
| 65 | if (!portName.Contains("/udp")) { |
65 | if (!portName.Contains("/udp")) { |
| 66 | try { |
66 | try { |
| 67 | serial_conn = new SerialPort(); |
67 | serial_conn = new SerialPort(); |
| 68 | serial_conn.ReadBufferSize = readBufferSize; |
68 | serial_conn.ReadBufferSize = readBufferSize; |
| 69 | serial_conn.BaudRate = baudRate; |
69 | serial_conn.BaudRate = baudRate; |
| 70 | serial_conn.Parity = parity; |
70 | serial_conn.Parity = parity; |
| 71 | serial_conn.PortName = portName; |
71 | serial_conn.PortName = portName; |
| 72 | serial_conn.StopBits = stopbits; |
72 | serial_conn.StopBits = stopbits; |
| 73 | serial_conn.DataBits = databits; |
73 | serial_conn.DataBits = databits; |
| 74 | serial_conn.RtsEnable = rtsEnable; |
74 | serial_conn.RtsEnable = rtsEnable; |
| 75 | if (DEBUG_LEVEL>1) { Console.WriteLine("Serial port opened to target"); } |
75 | if (DEBUG_LEVEL>1) { Console.WriteLine("Serial port opened to target"); } |
| 76 | if (DEBUG_LEVEL>1) { Console.WriteLine("Establishing serial connection to target"); } |
76 | if (DEBUG_LEVEL>1) { Console.WriteLine("Establishing serial connection to target"); } |
| 77 | byte[] bytes = new byte[1]; |
77 | byte[] bytes = new byte[1]; |
| 78 | bytes[0] = UART_CONN_BYTE; |
78 | bytes[0] = UART_CONN_BYTE; |
| 79 | //... ... |
79 | //... ... |
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | } |
82 | } |
| 83 | catch (Exception e) { |
83 | catch (Exception e) { |
| 84 | throw e; |
84 | throw e; |
| 85 | } |
85 | } |
| 86 | } else { |
86 | } else { |
| 87 | try { |
87 | try { |
| 88 | string[] splitted = portName.Split('/'); |
88 | string[] splitted = portName.Split('/'); |
| 89 | if (splitted.Length == 4) { |
89 | if (splitted.Length == 4) { |
| 90 | if (splitted[0] == "" && splitted[1] == "udp") { |
90 | if (splitted[0] == "" && splitted[1] == "udp") { |
| 91 | remoteIP = splitted[2]; |
91 | remoteIP = splitted[2]; |
| 92 | remotePort = Int32.Parse(splitted[3]); |
92 | remotePort = Int32.Parse(splitted[3]); |
| 93 | iep = new IPEndPoint(IPAddress.Any, remotePort); |
93 | iep = new IPEndPoint(IPAddress.Any, remotePort); |
| 94 | udpserver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); |
94 | udpserver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); |
| 95 | udpserver.Bind(iep); |
95 | udpserver.Bind(iep); |
| 96 | 96 | ||
| 97 | if (DEBUG_LEVEL>1) { Console.WriteLine("Establishing UDP connection to target"); } |
97 | if (DEBUG_LEVEL>1) { Console.WriteLine("Establishing UDP connection to target"); } |
| 98 | byte[] bytes = new byte[1]; |
98 | byte[] bytes = new byte[1]; |
| 99 | bytes[0] = UART_CONN_BYTE; |
99 | bytes[0] = UART_CONN_BYTE; |
| 100 | udpserver.SendTo(bytes, bytes.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse(remoteIP), remotePort)); |
100 | udpserver.SendTo(bytes, bytes.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse(remoteIP), remotePort)); |
| 101 | 101 | ||
| 102 | //... här ska man vänta på svar från servern ... |
102 | //... här ska man vänta på svar från servern ... |
| 103 | 103 | ||
| 104 | if (DEBUG_LEVEL>1) { Console.WriteLine("UDP connection established to target"); } |
104 | if (DEBUG_LEVEL>1) { Console.WriteLine("UDP connection established to target"); } |
| 105 | } |
105 | } |
| 106 | } |
106 | } |
| 107 | 107 | ||
| 108 | } catch (Exception e) { |
108 | } catch (Exception e) { |
| 109 | throw e; |
109 | throw e; |
| 110 | } |
110 | } |
| 111 | } |
111 | } |
| 112 | } |
112 | } |
| 113 | 113 | ||
| 114 | public bool writePacket(CanPacket cp) { |
114 | public bool writePacket(CanPacket cp) { |
| 115 | if (serial_conn != null) { |
115 | if (serial_conn != null) { |
| 116 | if (serial_conn.IsOpen) { |
116 | if (serial_conn.IsOpen) { |
| 117 | byte[] bytes = new byte[PACKET_LENGTH]; |
117 | byte[] bytes = new byte[PACKET_LENGTH]; |
| 118 | Array.Copy(cp.getBytes(), 0, bytes, 1, PACKET_LENGTH - 2); |
118 | Array.Copy(cp.getBytes(), 0, bytes, 1, PACKET_LENGTH - 2); |
| 119 | bytes[0] = UART_START_BYTE; |
119 | bytes[0] = UART_START_BYTE; |
| 120 | bytes[PACKET_LENGTH - 1] = UART_END_BYTE; |
120 | bytes[PACKET_LENGTH - 1] = UART_END_BYTE; |
| 121 | serial_conn.Write(bytes, 0, PACKET_LENGTH); |
121 | serial_conn.Write(bytes, 0, PACKET_LENGTH); |
| 122 | return true; |
122 | return true; |
| 123 | } |
123 | } |
| 124 | return false; |
124 | return false; |
| 125 | } else if (udpserver != null) { |
125 | } else if (udpserver != null) { |
| 126 | byte[] bytes = new byte[PACKET_LENGTH]; |
126 | byte[] bytes = new byte[PACKET_LENGTH]; |
| 127 | Array.Copy(cp.getBytes(), 0, bytes, 1, PACKET_LENGTH - 2); |
127 | Array.Copy(cp.getBytes(), 0, bytes, 1, PACKET_LENGTH - 2); |
| 128 | bytes[0] = UART_START_BYTE; |
128 | bytes[0] = UART_START_BYTE; |
| 129 | bytes[PACKET_LENGTH - 1] = UART_END_BYTE; |
129 | bytes[PACKET_LENGTH - 1] = UART_END_BYTE; |
| 130 | 130 | ||
| 131 | udpserver.SendTo(bytes, bytes.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse(remoteIP), remotePort)); |
131 | udpserver.SendTo(bytes, bytes.Length, SocketFlags.None, new IPEndPoint(IPAddress.Parse(remoteIP), remotePort)); |
| 132 | - | ||
| 133 | return true; |
132 | return true; |
| 134 | } else { |
133 | } else { |
| 135 | return false; |
134 | return false; |
| 136 | } |
135 | } |
| 137 | } |
136 | } |
| 138 | 137 | ||
| 139 | public bool getPacket(out CanPacket cp) { |
138 | public bool getPacket(out CanPacket cp) { |
| 140 | cp = null; |
139 | cp = null; |
| 141 | if ((serial_conn != null) || (udpserver != null)) { |
140 | if ((serial_conn != null) || (udpserver != null)) { |
| 142 | 141 | ||
| 143 | if (serial_conn != null) { |
142 | if (serial_conn != null) { |
| 144 | int bytesToRead = serial_conn.BytesToRead; |
143 | int bytesToRead = serial_conn.BytesToRead; |
| 145 | if (bytesToRead > 0) { |
144 | if (bytesToRead > 0) { |
| 146 | byte[] data = new byte[bytesToRead]; |
145 | byte[] data = new byte[bytesToRead]; |
| 147 | 146 | ||
| 148 | try { |
147 | try { |
| 149 | serial_conn.Read(data, 0, data.Length); |
148 | serial_conn.Read(data, 0, data.Length); |
| 150 | } catch (Exception e) { |
149 | } catch (Exception e) { |
| 151 | Console.WriteLine("Exception " + e + " occured\n"); |
150 | Console.WriteLine("Exception " + e + " occured\n"); |
| 152 | } |
151 | } |
| 153 | 152 | ||
| 154 | Array.Copy(data, 0, iBuff, iBuffPointer, data.Length); |
153 | Array.Copy(data, 0, iBuff, iBuffPointer, data.Length); |
| 155 | iBuffPointer += bytesToRead; |
154 | iBuffPointer += bytesToRead; |
| 156 | } |
155 | } |
| 157 | } else if (udpserver != null) { |
156 | } else if (udpserver != null) { |
| 158 | int bytesToRead = udpserver.Available; |
157 | int bytesToRead = udpserver.Available; |
| 159 | if (udpserver.Available > 0) { |
158 | if (udpserver.Available > 0) { |
| 160 | byte[] data = new byte[bytesToRead]; |
159 | byte[] data = new byte[bytesToRead]; |
| 161 | try { |
160 | try { |
| 162 | EndPoint tmpRemote = (EndPoint)iep; |
161 | EndPoint tmpRemote = (EndPoint)iep; |
| 163 | udpserver.ReceiveFrom(data, ref tmpRemote); |
162 | udpserver.ReceiveFrom(data, ref tmpRemote); |
| 164 | } catch (Exception e) { |
163 | } catch (Exception e) { |
| 165 | Console.WriteLine("Exception " + e + " occured\n"); |
164 | Console.WriteLine("Exception " + e + " occured\n"); |
| 166 | } |
165 | } |
| 167 | 166 | ||
| 168 | int bytestomove = data.Length; |
167 | int bytestomove = data.Length; |
| 169 | if (bytestomove ==18) { |
168 | if (bytestomove ==18) { |
| 170 | bytestomove = data.Length -1; |
169 | bytestomove = data.Length -1; |
| 171 | } |
170 | } |
| 172 | Array.Copy(data, 0, iBuff, iBuffPointer, bytestomove); |
171 | Array.Copy(data, 0, iBuff, iBuffPointer, bytestomove); |
| 173 | iBuffPointer += bytestomove; |
172 | iBuffPointer += bytestomove; |
| 174 | } |
173 | } |
| 175 | 174 | ||
| 176 | } |
175 | } |
| 177 | 176 | ||
| 178 | // N�r array st�rre eller lika med PACKET_LENGTH... |
177 | // N�r array st�rre eller lika med PACKET_LENGTH... |
| 179 | while (iBuffPointer >= PACKET_LENGTH) { |
178 | while (iBuffPointer >= PACKET_LENGTH) { |
| 180 | int startIndex = 0; |
179 | int startIndex = 0; |
| 181 | 180 | ||
| 182 | // S�k igenom efter start byte fr�n b�rjan. |
181 | // S�k igenom efter start byte fr�n b�rjan. |
| 183 | for (int i = 0; i < iBuffPointer; i++) { |
182 | for (int i = 0; i < iBuffPointer; i++) { |
| 184 | // Poppa alla bytes som inte �r startbyte. |
183 | // Poppa alla bytes som inte �r startbyte. |
| 185 | if (iBuff[i] != UART_START_BYTE) startIndex++; |
184 | if (iBuff[i] != UART_START_BYTE) startIndex++; |
| 186 | else { |
185 | else { |
| 187 | // N�r startbyte hittas, kolla om �terst�ende l�ngd �r st�rre eller lika med PACKET_LENGTH (inkl startbyte) |
186 | // N�r startbyte hittas, kolla om �terst�ende l�ngd �r st�rre eller lika med PACKET_LENGTH (inkl startbyte) |
| 188 | if ((iBuffPointer - startIndex) >= PACKET_LENGTH) { |
187 | if ((iBuffPointer - startIndex) >= PACKET_LENGTH) { |
| 189 | //om s�, kolla PACKET_LENGTH-1 byte fram. |
188 | //om s�, kolla PACKET_LENGTH-1 byte fram. |
| 190 | if (iBuff[startIndex + PACKET_LENGTH - 1] == UART_END_BYTE) { |
189 | if (iBuff[startIndex + PACKET_LENGTH - 1] == UART_END_BYTE) { |
| 191 | // Om byte PACKET_LENGTH-1 �r slutbyte s� extraktas startIndex till slutbyteindex. |
190 | // Om byte PACKET_LENGTH-1 �r slutbyte s� extraktas startIndex till slutbyteindex. |
| 192 | CanPacket cm = new CanPacket(iBuff, (uint)startIndex + 1); |
191 | CanPacket cm = new CanPacket(iBuff, (uint)startIndex + 1); |
| 193 | // S�tt ny startindex och avsluta loop. |
192 | // S�tt ny startindex och avsluta loop. |
| 194 | startIndex += PACKET_LENGTH; |
193 | startIndex += PACKET_LENGTH; |
| 195 | cp = cm; |
194 | cp = cm; |
| 196 | // och i slutet g�ra en array copy. |
195 | // och i slutet g�ra en array copy. |
| 197 | // Flytta ner allt efter slutbyte till index 0 i array. |
196 | // Flytta ner allt efter slutbyte till index 0 i array. |
| 198 | Array.Copy(iBuff, startIndex, iBuff, 0, iBuffPointer - PACKET_LENGTH); |
197 | Array.Copy(iBuff, startIndex, iBuff, 0, iBuffPointer - PACKET_LENGTH); |
| 199 | iBuffPointer -= startIndex; |
198 | iBuffPointer -= startIndex; |
| 200 | 199 | ||
| 201 | return true; |
200 | return true; |
| 202 | } |
201 | } |
| 203 | } |
202 | } |
| 204 | } |
203 | } |
| 205 | } |
204 | } |
| 206 | // och i slutet g�ra en array copy. |
205 | // och i slutet g�ra en array copy. |
| 207 | // Flytta ner allt efter slutbyte till index 0 i array. |
206 | // Flytta ner allt efter slutbyte till index 0 i array. |
| 208 | Array.Copy(iBuff, startIndex, iBuff, 0, iBuffPointer - PACKET_LENGTH); |
207 | Array.Copy(iBuff, startIndex, iBuff, 0, iBuffPointer - PACKET_LENGTH); |
| 209 | iBuffPointer -= startIndex; |
208 | iBuffPointer -= startIndex; |
| 210 | } |
209 | } |
| 211 | return false; |
210 | return false; |
| 212 | 211 | ||
| 213 | } else { |
212 | } else { |
| 214 | return false; |
213 | return false; |
| 215 | } |
214 | } |
| 216 | } |
215 | } |
| 217 | 216 | ||
| 218 | } |
217 | } |
| 219 | 218 | ||