-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForm1.cs
1644 lines (1392 loc) · 54.8 KB
/
Form1.cs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections;
using USBCAN.Properties;
using System.IO;
namespace USBCAN
{
public partial class Form1 : Form
{
/// <summary>
/// Form level Variables
/// </summary>
// private static int MyAngle = 0; //旋转角度[-360,360]
private static int InitAngle = -210;
private Image img;
public static Hashtable htable;
private FormGear gearform;
private FormControl controlform;
private FormMeters meterform;
VerticalProgressBar TempVpOnMain;
VerticalProgressBar OilHeightVpOnMain;
/// <summary>
/// data segment DS
/// </summary>
string PARK = "PARK";
string D1 = "D1";
string D2 = "D2";
string D3 = "D3";
string D4 = "D4";
string REVERS = "REVERS";
ArrayList alist;
int ArrayCounter = 0;
int SpeedToSend = 0;
int GearState = 0;
public static string SpeedID = "C7 F7 88 00";
public static string GearID = "18 FE F1 1";
public static string TempID = "C7 F7 70 00";
public static string OilHeightID = "C7 F7 E0 00";
public static string PressureID = "67 80 20 00";
public static string WatchID = "50000000";
public static string SelSpeedID = "00 00 00 0A";
public static string speed = "speed";
public static string speedtogo = "speedtogo";
private int LastSpeed = 0;
public static string temp = "temp";
public static string temptogo = "temptogo";
private int LastTemp = 0;
public static string OilHeight = "OilHeight";
public static string OilHeighttogo = "OilHeighttogo";
private int LastOilHeight = 0;
public static string gear = "gear";
public static string geartogo = "geartogo";
public static string pressure = "pressure";
public static string pressuretogo = "pressuretogo";
private int LastPressure = 0;
int SpeedAngle = 0;
int MaxSpeed = 100;
Int32 distance = 0;
Int32 angle = 0;
Int32 X_actual;
Int32 Y_actual;
String file_name;
String file_txt;
Coor valid_coord;
/// <summary>
/// claim ends
/// </summary>
/// protacals
///Gear out 01000000H
///Gear input 02000000H
///Temp input 10000000H
///
PVCI_CAN_OBJ CurrentBuff;
[StructLayout(LayoutKind.Sequential)]
public struct PVCI_INIT_CONFIG
{
public uint AccCode;
public uint AccMask;
public uint Reserved;
public byte Filter;
public byte kCanBaud;
public byte Timing0;
public byte Timing1;
public byte Mode;
public byte CanRx_IER;
}
[StructLayout(LayoutKind.Sequential)]
public struct PVCI_CAN_OBJ
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]//声明数组大小为4
public byte [] ID; //4
public UInt32 TimeStamp;//C++的UINT对应32位
public byte TimeFlag;
public byte SendType;
public byte RemoteFlag; //=0
public byte ExternFlag; //=1
public byte DataLen; //=8
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]//声明数组大小为8
public byte[] Data; //8
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]//声明数组大小为3
public byte[] Reserved;//3
}
[DllImport("VCI_CAN.dll",EntryPoint = "VCI_OpenDevice")]
public static extern int VCI_OpenDevice(uint Devtype, uint Devindex, uint Reserved);
[DllImport("VCI_CAN.dll", EntryPoint = "VCI_StartCAN")]
public static extern int VCI_StartCAN(uint Devtype, uint Devindex, uint CANInd);
[DllImport("VCI_CAN.dll", EntryPoint = "VCI_CloseDevice")]
public static extern int VCI_CloseDevice(int DeviceType, int DeviceInd);
[DllImport("VCI_CAN.dll", EntryPoint = "VCI_SetReference")]
public static extern int VCI_SetReference(uint DeviceType, uint DeviceInd, uint CANInd, uint RefType, ref byte pData);
[DllImport("VCI_CAN.dll", EntryPoint = "VCI_InitCAN")]
public static extern int VCI_InitCAN(uint DevType, uint DevIndex, uint CANIndex, ref PVCI_INIT_CONFIG pInitConfig);
[DllImport("VCI_CAN.dll", EntryPoint = "VCI_Transmit")]
public static extern int VCI_Transmit(uint DevType, uint DevIndex, uint CANIndex, ref PVCI_CAN_OBJ pSend);
[DllImport("VCI_CAN.dll", EntryPoint = "VCI_Receive")]
public static extern int VCI_Receive(uint DevType, uint DevIndex, uint CANIndex, [Out]/*声明参数为输出*/ PVCI_CAN_OBJ[] pReceive);
uint m_nNum = 0;//发送和接收的总帧数
public Form1()
{
InitializeComponent();
}
public void DrawCoorodinates_withColor(int x, int y, Color col)
{
// //pen 画线
Pen myPen = new Pen(col);
Graphics g = this.CreateGraphics();
g.DrawEllipse(myPen, x, y, 5, 5);
}
public void DrawAxis(int org_x, int org_y, int length, Color col)
{
Pen myPen = new Pen(col, 3);
Graphics g = this.CreateGraphics();
g.DrawLine(myPen, (float)org_x, (float)org_y, (float)(org_x + length), (float)org_y);
g.DrawLine(myPen, (float)org_x, (float)org_y, (float)(org_x), ((float)org_y + length));
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
file_txt = "";
ORG_X = 1000;
ORG_Y = 500;
string B = "00";
string C = "1";
string R = "0"+ B + C;
img = Resource1.littleprobe;
gearform = new FormGear();
alist = new ArrayList();
alist.Add(PARK);
alist.Add(D1);
alist.Add(D2);
alist.Add(D3);
alist.Add(D4);
alist.Add(REVERS);
///the following codes
///initialize the hashtable
///
htable = new Hashtable();
htable.Add("speed", 0);
htable.Add("gear", PARK);
htable.Add("temp", 0);
htable.Add("OilHeight",0);
htable.Add(pressure, 0);
htable.Add(speedtogo, 0);
htable.Add(pressuretogo, 0);
htable.Add(OilHeighttogo, 0);
htable.Add(temptogo, 0);
///
///Init the ProgressBars
///
int gap = 30;
TempVpOnMain = new VerticalProgressBar();
OilHeightVpOnMain = new VerticalProgressBar();
TempVpOnMain.Location = new Point(750, 330);
TempVpOnMain.Width = 20;
TempVpOnMain.Height = 100;
TempVpOnMain.Maximum = 500;
OilHeightVpOnMain.Location = new Point(800+gap, 330);
OilHeightVpOnMain.Width = 20;
OilHeightVpOnMain.Height = 100;
OilHeightVpOnMain.Maximum = 500;
//PressureProgressBar.Maximum = 500;
this.Controls.Add(TempVpOnMain);
this.Controls.Add(OilHeightVpOnMain);
DrawAxis(ORG_X, ORG_Y, 500, Color.Blue);
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public Image RotateImg(Image b, int angle)
{
angle = angle % 360;
//弧度转换
double radian = angle * Math.PI / 180.0;
double cos = Math.Cos(radian);
double sin = Math.Sin(radian);
//原图的宽和高
int w = b.Width;
int h = b.Height;
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
//目标位图
Bitmap dsImage = new Bitmap(W, H);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//计算偏移量
Point Offset = new Point((W - w) / 2, (H - h) / 2);
//构造图像显示区域:让图像的中心与窗口的中心点一致
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
// Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);//original
g.TranslateTransform(center.X, center.Y);
g.RotateTransform(360 - angle);
//恢复图像在水平和垂直方向的平移
g.TranslateTransform(-center.X, -center.Y);
g.DrawImage(b, rect);
//重至绘图的所有变换
g.ResetTransform();
g.Save();
g.Dispose();
//保存旋转后的图片
b.Dispose();
dsImage.Save("FocusPoint.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
return dsImage;
}
public Image RotateImg(string filename, int angle)
{
return RotateImg(GetSourceImg(filename), angle);
}
public Image GetSourceImg(string filename)
{
Image img;
img = Bitmap.FromFile(filename);
return img;
}
/// <summary>
/// rotation function over
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonOpen_Click(object sender, EventArgs e)
{
if (VCI_OpenDevice(2, 0, 0) != 1)
{
MessageBox.Show("Open USB_CAN fail");
}
else
{
PVCI_INIT_CONFIG[] config = new PVCI_INIT_CONFIG[1];
config[0].AccCode = 0x80000008;
config[0].AccMask = 0xFFFFFFFF;
config[0].Reserved = 204;
config[0].Filter = 0;
config[0].kCanBaud = 15;
/////////////
////////Config the baund rate
config[0].Timing0 =0x01;// 0x00;
config[0].Timing1 = 0x1C;//0x14;
/////
config[0].CanRx_IER = 1;
config[0].Mode =0;//1~自发自收模式,0~正常工作模式
if (VCI_InitCAN(2, 0, 0, ref config[0]) != 1)
{
MessageBox.Show("Init USB_CAN fail");
return;
}
if (VCI_StartCAN(2, 0, 0) != 1)
{
MessageBox.Show("Start USB_CAN fail");
return;
}
}
buttonOpen.Enabled = false;
buttonClose.Enabled = true;
buttonSend.Enabled=true;
MessageBox.Show("启动成功");
}
private void buttonClose_Click(object sender, EventArgs e)
{
int count = VCI_CloseDevice(2, 0);
if (count == 1)
{
buttonOpen.Enabled = true;
buttonClose.Enabled = false;
buttonSend.Enabled = false;
MessageBox.Show("close sucessful");
}
else if (count == 0)
{
MessageBox.Show("close fail");
}
else
{
MessageBox.Show("device not open");
}
}
public int HexChar(char c)
{
if ((c >= '0') && (c <= '9'))
return c - 0x30;
else if ((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;
else if ((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
else
return 0x10;
}
public int Str2Hex(string str)
{
int len = str.Length;
if (len == 2)
{
int a = HexChar(str[0]);
int b = HexChar(str[1]);
if (a == 16 || b == 16)
{
MessageBox.Show("format error!");
return 256;
}
else
{
return a * 16 + b;
}
}
else
{
MessageBox.Show("len must be 2");
return 256;
}
}
public void DrawCoorodinates(int x,int y)
{
// //pen 画线
Pen myPen = new Pen(Color.Black);
Graphics g = this.CreateGraphics();
g.DrawEllipse(myPen, x, y, 5, 5);
}
public void DrawCoorodinates_with_pen(int x, int y,Pen p)
{
// //pen 画线
Graphics g = this.CreateGraphics();
g.DrawEllipse(p, x, y, 5, 5);
}
private void buttonSend_Click(object sender, EventArgs e)
{
DrawCoorodinates(1000, 50);
PVCI_CAN_OBJ sendbuf = new PVCI_CAN_OBJ();
sendbuf.ID = new byte[4];
sendbuf.Data = new byte[8];
sendbuf.Reserved = new byte[3];
string[] str5 = new string[32];
byte[] buf = new byte[50];
byte[] SendID = new byte[10];
string strtemp, strtemp1;
string str1;
int len, datanum = 0, IDnum = 0, newflag = 1;
string strSendID =textBoxID.Text.ToString();
char[] m_strSendID = strSendID.ToCharArray();
len = m_strSendID.Length;
for (int i = 0; i < len; i++)
{
strtemp = m_strSendID[i].ToString();
if (strtemp == " ")
newflag = 1;
else if (newflag == 1)
{
newflag = 0;
strtemp = m_strSendID[i].ToString();
if (i == (len - 1))
{
str5[IDnum] = "0" + strtemp;
}
else
{
strtemp1 = m_strSendID[i + 1].ToString();
if (strtemp1 == " ")
str5[IDnum] = "0" + strtemp;
else
str5[IDnum] = strtemp + strtemp1;
}
SendID[IDnum] = (byte)Str2Hex(str5[IDnum]);
IDnum++;
if (IDnum >= 4)
break;
}
}
newflag = 1;
string strSendData = TextBoxSend.Text.ToString();
char[] m_strSendData = strSendData.ToCharArray();
len = m_strSendData.Length;
//m_strSendData.GetLength();
for (int i = 0; i < len; i++)
{
strtemp = m_strSendData[i].ToString();
if (strtemp == " ")
newflag = 1;
else if (newflag == 1)
{
newflag = 0;
strtemp = m_strSendData[i].ToString();
if (i == (len - 1))
{
str5[datanum] = "0" + strtemp;
}
else
{
strtemp1 = m_strSendData[i + 1].ToString();
if (strtemp1 == " ")
{
str5[datanum] = "0" + strtemp;
}
else
str5[datanum] = strtemp + strtemp1;
}
buf[datanum] = (byte)Str2Hex(str5[datanum]);
datanum++;
if (datanum >= 8)
break;
}
}
byte m_nSendFrameType = 0;
byte m_nSendFrameFormat = 0;
byte m_radioIDFormat = 0;
//////////
/////CHANGED
sendbuf.ExternFlag = 1;//m_nSendFrameType;//1
sendbuf.DataLen = (byte)datanum;//8
sendbuf.RemoteFlag = m_nSendFrameFormat;//0
if (m_nSendFrameFormat == 1)//if remote frame, data is invalid
for (int i = 0; i < datanum; i++)
buf[i] = 0;
byte[] SendID2 = new byte[4];
/////////////////////////////////
/////////////////brlow changed
if ((sendbuf.ExternFlag) == 1)//Extend ID progress
{
if (m_radioIDFormat != 0)//将直接ID值进行转换到CAN2.0B格式
{
// SendID[0] &= 0x1F;
SendID2[0] = (byte)(SendID[0]<<3);
SendID2[1] = (byte)(SendID[1] <<3);
SendID2[2] = (byte)(SendID[2]<<3);
SendID2[3] = (byte)(SendID[3]<<3);
for (int i = 0; i < 4; i++)
sendbuf.ID[i] = SendID2[i];
// sendbuf->ID=(SendID2[0]<<24)+(SendID2[1]<<16)+(SendID2[2]<<8)+SendID2[3];
}
else
{
// SendID[3] = (byte)(SendID[3] & 0xF8);//the last 3 bits is invalid,clear to 0
for (int i = 0; i < 4; i++)
sendbuf.ID[i] = (byte)(SendID[i] << 3);
// sendbuf->ID=(SendID[0]<<24)+(SendID[1]<<16)+(SendID[2]<<8)+SendID[3];
}
}
else//Basic CAN
{
if (m_radioIDFormat != 0)//Direct id value to SJA1000 format
{
//SendID[2] &= 0x07;
SendID2[0] = (byte)(SendID[0]<<3);
SendID2[1] = (byte)(SendID[1]<<3);
for (int i = 0; i < 2; i++)
sendbuf.ID[i] = SendID2[i];
//sendbuf->ID=(SendID2[0]<<24)+(SendID2[1]<<16)+(SendID2[2]<<8)+SendID2[3];
}
else
{
//SendID[1] = (byte)(SendID[1] & 0xE0);//the last 5 bits is invalid, clear to 0
for (int i = 0; i < 2; i++)
sendbuf.ID[i] = (byte)(SendID[i] << 3);
// sendbuf->ID=(SendID[0]<<24)+(SendID[1]<<16)+(SendID[2]<<8)+SendID[3];
}
}
//////////////above changed
///////////////////////////////////////
for (int i = 0; i < datanum; i++)
sendbuf.Data[i] = buf[i];
int flag=0;
// while (flag != 1)
//{
flag = VCI_Transmit(2, 0, 0, ref sendbuf);//CAN DATA SEND
//}
PVCI_CAN_OBJ[] TempBuf = new PVCI_CAN_OBJ[50];
TempBuf[0] = sendbuf;
showCan(0, 0, TempBuf, 1);
//DrawBar(TempBuf);
if (flag != 1)
{
if (flag == -1)
MessageBox.Show("fail,device not open\n");
else if (flag == 0)
// MessageBox.Show("fail\n");
return;
}
string str = "";
for (int i = 0; i < sendbuf.DataLen; i++) //DATA
{
uint data = sendbuf.Data[i];
str1 = Convert.ToString(data, 16);
str += str1 + " ";
}
CurrentBuff=sendbuf;
// listView1.Items.Add(str).SubItems.Add("Send");
}
/// <summary>
///
/// </summary>
/// <param name="shiliu"></param>
/// <returns></returns>
private int HexToInt(string shiliu)
{
int shi = 0;
try
{
int Int = Convert.ToInt32(shiliu, 16);
shi = Int;
}
catch (Exception Err)
{
MessageBox.Show(Err.Message);
}
return shi;
}
/// <summary>
/// HexArr convert to int
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
int HexArr2Int(byte[] arr) {
int sum=0;
for (int i = 0; i < arr.GetLength(0);i++ ) {
sum+=HexToInt(Convert.ToString(arr[i]))*16^(7-i);
}
return sum;
}
/// <summary>
/// timer's tick in which we recive and update the data sent to the PC then update them to the htable
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timerRecieve_Tick(object sender, EventArgs e)
{
PVCI_CAN_OBJ[] sendbuf = new PVCI_CAN_OBJ[200];
int NumValue = 0;
NumValue = VCI_Receive(2, 0, 0, sendbuf);/////////////////Get the msg from the CAN NetWork
showCan(0, 1, sendbuf, NumValue);
int len=sendbuf.GetLength(0);
if (sendbuf[0].DataLen > 0)
{
CurrentBuff = sendbuf[0];
for (int i = 0; i < CurrentBuff.ID.GetLength(0); i++)
{
CurrentBuff.ID[i] = (byte)((CurrentBuff.ID[i])>> 3);
}
////////////////Recive the data from the dsp /////recive data of valid distance and draw on board ////the ID is 00 11 12 19
if (CurrentBuff.ID[0] == 0x00 && CurrentBuff.ID[1] == 0x11 && CurrentBuff.ID[2] == 0x12 && CurrentBuff.ID[3] == 0x19)
{
Int32[] temp = new Int32[8];
CurrentBuff = sendbuf[0];
for (int i = 0; i < 6; i++)
{
temp[i] = CurrentBuff.Data[i];
}
temp[0] = (temp[0] << 16);
temp[1] = (temp[1] << 8);
temp[2] = (temp[2]);
distance = temp[0] + temp[1] + temp[2];
angle = (temp[3]<<16) + (temp[4]<<8) + temp[5];
angle = angle * 360 /1152000;
valid_coord = new Coor();
valid_coord= pollor_to_Descart(distance, angle);
file_txt+="<Real X " + valid_coord.X.ToString() + " Y " + valid_coord.Y.ToString() + ">\n\r ";
//write_file("E:\\trail.txt","","X "+valid_coord.X.ToString()+"Y "+valid_coord.Y.ToString()+"\n\r");
//"D:\\trail.txt"
DrawCoorodinates( ORG_X+ (valid_coord.X)/100,ORG_Y+ (valid_coord.Y)/100);
}
////////////////Recive the data from the dsp /////valid aim map point 00 11 12 1a
if (CurrentBuff.ID[0] == 0x00 && CurrentBuff.ID[1] == 0x11 && CurrentBuff.ID[2] == 0x12 && CurrentBuff.ID[3] == 0x1a)
{
Int32[] temp = new Int32[8];
CurrentBuff = sendbuf[0];
for (int i = 0; i < 6; i++)
{
temp[i] = CurrentBuff.Data[i];
}
temp[0] = (temp[0] << 16);
temp[1] = (temp[1] << 8);
temp[2] = (temp[2]);
x_map = temp[0] + temp[1] + temp[2];
y_map = (temp[3] << 16) + (temp[4] << 8) + temp[5];
//angle = angle * 360 / 1152000;
valid_coord = new Coor();
valid_coord = pollor_to_Descart(distance, angle);
file_txt += "<Map :X " + valid_coord.X.ToString() + " Y " + valid_coord.Y.ToString() + ">\n\r ";
//write_file("E:\\trail.txt","","X "+valid_coord.X.ToString()+"Y "+valid_coord.Y.ToString()+"\n\r");
//"D:\\trail.txt"
DrawCoorodinates_with_pen(ORG_X + (valid_coord.X) / 100, ORG_Y + (valid_coord.Y) / 100,new Pen(Color.Red));
}
}
// TempVpOnMain.Value = (int)Form1.htable[pressure]/TempVpOnMain.Maximum;
//DrawBar(sendbuf[sendbuf.GetLength(0)-1]);
}
int GetSpeedAngleFromHtable(Hashtable table) {
if (table.ContainsKey("Speed"))
{
int tempSpeed = Convert.ToInt32(table["Speed"].ToString());
tempSpeed = tempSpeed / MaxSpeed;
return tempSpeed;
}
else {
return 0;
}
}
private bool write_file(String path, String f_name, String content)
{
if (!File.Exists(path + f_name))//"D:\\trail.txt"
{
try
{
FileStream fs1 = new FileStream(path + f_name, FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(content);//开始写入值
sw.Close();
fs1.Close();
return true;
}
catch (IOException e)
{
MessageBox.Show(e.ToString());
return false;
}
}
else
{
try
{
FileStream fs = new FileStream(path + f_name, FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
sr.WriteLine(content);//开始写入值
sr.Close();
fs.Close();
return true;
}
catch (IOException e)
{
MessageBox.Show(e.ToString());
return false;
}
}
}
private void showCan(byte byCanIndex,byte byOrientation, PVCI_CAN_OBJ []buf,int nLength)
{
DateTime CurrentTime = new DateTime();
for(uint i=0;i<nLength;i++)
{
PVCI_CAN_OBJ temp = buf[i];
ListViewItem Item = new ListViewItem();
string strNum = Convert.ToString(m_nNum);
string strTime = DateTime.Now.ToString();//strHour + ":" + strMinute + "" + strSecond;
int foundS1 = strTime.IndexOf(" ");
strTime= strTime.Remove(0, foundS1);
string strCanIndex = Convert.ToString(byCanIndex);
string strOrientation;
if(byOrientation==0)
strOrientation ="发送";
else
strOrientation="接收";
byte a = temp.ID[0];
string strID0 = Convert.ToString((buf[i]).ID[0]>>3);
string strID1 = Convert.ToString((buf[i]).ID[1] >> 3);
string strID2 = Convert.ToString((buf[i]).ID[2] >> 3);
string strID3 = Convert.ToString((buf[i]).ID[3] >> 3);
string strID=strID0+" "+strID1+" "+strID2+" "+strID3;
string strFrame;
if ((buf[i]).RemoteFlag==1)
strFrame="远程帧";
else
strFrame="数据帧";
string strType;
if(buf[i].ExternFlag==1)
strType="扩展帧";
else
strType="标准帧";
string strDataLen=Convert.ToString(buf[i].DataLen);
string strData=Convert.ToString(buf[i].Data[0]);
for(int j=1;j<8;j++)
{
string strTemp;
strTemp=Convert.ToString(buf[i].Data[j]);
strData=strData+" "+strTemp;
}
Item.SubItems.Clear();
Item.SubItems[0].Text = strNum;
Item.SubItems.Add(strTime);
Item.SubItems.Add(strCanIndex);
Item.SubItems.Add(strOrientation);
Item.SubItems.Add(strID);
Item.SubItems.Add(strFrame);
Item.SubItems.Add(strType);
Item.SubItems.Add(strDataLen);
Item.SubItems.Add(strData);
listViewRecieve.Items.Add(Item);
m_nNum++;
}
//DrawBar(buf);/////////////////////////////////
}
private void listViewRecieve_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//SpeedToSend+=1;
//0F000000
Char[] value = (up_down_value.Text.ToString().Trim().ToCharArray());
String vertikcal_step = "";
vertikcal_step=value[0].ToString() + value[1].ToString();
TransmitACanMsg("00 11 12 13","00 00 00 00 00 "+vertikcal_step+" 00 00");
}
private void init_can(int baud,int work_mode)
{
PVCI_INIT_CONFIG[] config = new PVCI_INIT_CONFIG[1];
config[0].AccCode = 0x80000008;
config[0].AccMask = 0xFFFFFFFF;
config[0].Reserved = 204;
config[0].Filter = 0;
config[0].kCanBaud = 15;
/////////////
////////Config the baund rate
config[0].Timing0 = 0x01;// 0x00;
config[0].Timing1 = 0x1C;//0x14;
if (baud==250)
{
config[0].Timing0 = 0x01;// 0x00;
config[0].Timing1 = 0x1C;//0x14;
}
else
{
if (baud==500)
{
config[0].Timing0 = 0x00;// 0x00;
config[0].Timing1 = 0x1C;//0x14;
}
}
/////
config[0].CanRx_IER = 1;
config[0].Mode =(byte) work_mode;//1~自发自收模式,0~正常工作模式
if (VCI_InitCAN(2, 0, 0, ref config[0]) != 1)
{
MessageBox.Show("Init USB_CAN fail");
return;
}
if (VCI_StartCAN(2, 0, 0) != 1)
{
MessageBox.Show("Start USB_CAN fail");
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
Char[] value = (up_down_value.Text.ToString().Trim().ToCharArray());
String vertikcal_step = "";
vertikcal_step = value[0].ToString() + value[1].ToString();
TransmitACanMsg("00 11 12 13", "00 00 00 00 00 " + vertikcal_step + " 00 01");
}
public void TransmitACanMsg(string IDtoSend,string data) {
PVCI_CAN_OBJ sendbuf = new PVCI_CAN_OBJ();
sendbuf.ID = new byte[4];
sendbuf.Data = new byte[8];
sendbuf.Reserved = new byte[3];
string[] str5 = new string[32];
byte[] buf = new byte[50];
byte[] SendID = new byte[10];
string strtemp, strtemp1;
// string str1;
int len, datanum = 0, IDnum = 0, newflag = 1;
string strSendID = IDtoSend;//textBoxID.Text.ToString();
char[] m_strSendID = strSendID.ToCharArray();
len = m_strSendID.Length;
for (int i = 0; i < len; i++)
{
strtemp = m_strSendID[i].ToString();
if (strtemp == " ")
newflag = 1;
else if (newflag == 1)
{
newflag = 0;
strtemp = m_strSendID[i].ToString();
if (i == (len - 1))
{
str5[IDnum] = "0" + strtemp;
}
else
{
strtemp1 = m_strSendID[i + 1].ToString();
if (strtemp1 == " ")
str5[IDnum] = "0" + strtemp;
else
str5[IDnum] = strtemp + strtemp1;
}
SendID[IDnum] = (byte)Str2Hex(str5[IDnum]);
IDnum++;
if (IDnum >= 4)
break;
}
}
newflag = 1;
string strSendData =data;
char[] m_strSendData = strSendData.ToCharArray();
len = m_strSendData.Length;
//m_strSendData.GetLength();
for (int i = 0; i < len; i++)
{
strtemp = m_strSendData[i].ToString();
if (strtemp == " ")
newflag = 1;
else if (newflag == 1)
{
newflag = 0;
strtemp = m_strSendData[i].ToString();
if (i == (len - 1))
{
str5[datanum] = "0" + strtemp;
}
else
{
strtemp1 = m_strSendData[i + 1].ToString();