19,502
社区成员
发帖
与我相关
我的任务
分享
BOOL InitSerailPort(CString csSerialPort,LPVOID pParent)
37.{
38. DCB commDCB;
39. COMMTIMEOUTS timeouts;
40.
41. ghCommHandle = CreateFile(csSerialPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
42. OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
43. if(INVALID_HANDLE_VALUE == ghCommHandle)
44. {
45. RETAILMSG(1, (TEXT("Opening GPS %s failed: %d!\r\n"),csSerialPort,(int)GetLastError()));
46. return FALSE;
47. }
48.
49. commDCB.DCBlength = sizeof (DCB);
50. if(!GetCommState(ghCommHandle,&commDCB))
51. {
52. CloseHandle(ghCommHandle);
53. ghCommHandle = INVALID_HANDLE_VALUE;
54. RETAILMSG(1,(L"Failed in getting GPS %s DCB settings: %d!\r\n",csSerialPort,(int)GetLastError()));
55. return FALSE;
56. }
57. commDCB.DCBlength = sizeof(DCB);
58. commDCB.BaudRate = 9600; // Current baud
59. commDCB.ByteSize = 8; // Number of bits/bytes, 4-8
60. commDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
61. commDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
62.
63. // Setting serial port to Centrality speicifcations
64. if(!SetCommState(ghCommHandle,&commDCB))
65. {
66. CloseHandle(ghCommHandle);
67. ghCommHandle = INVALID_HANDLE_VALUE;
68. RETAILMSG(1,(L"Error in trying to set GPS %s DCB settings: %d!\r\n",csSerialPort,(int)GetLastError()));
69. return FALSE;
70. }
71.
72. // Get the default timeout settings for port
73. if(!GetCommTimeouts(ghCommHandle, &timeouts))
74. {
75. CloseHandle(ghCommHandle);
76. ghCommHandle = INVALID_HANDLE_VALUE;
77. RETAILMSG(1,(L"Failed in getting GPS %s timeout settings: %d!\r\n",csSerialPort,(int)GetLastError()));
78. return FALSE;
79. }
80. // RETAILMSG(1,(L"%s DCB set successfully!\r\n",csSerialPort));
81.
82. // Change the timeouts structure settings to Centrality settings
83. timeouts.ReadIntervalTimeout = 500;
84. timeouts.ReadTotalTimeoutMultiplier = 0;
85. timeouts.ReadTotalTimeoutConstant = 0;
86.
87. // Set the time-out parameters for all read and write operations on the port.
88. if(!SetCommTimeouts(ghCommHandle,&timeouts))
89. {
90. CloseHandle(ghCommHandle);
91. ghCommHandle = INVALID_HANDLE_VALUE;
92. RETAILMSG(1,(L"Error in trying to set GPS %s timeout settings: %d!\r\n",csSerialPort,(int)GetLastError()));
93. return FALSE;
94. }
95.
96. if(0 != SetCommMask(ghCommHandle,EV_RXCHAR))
97. {
98. RETAILMSG(1,(L"==Set %s mask OK!\r\n",csSerialPort));
99. }
100. else
101. {
102. RETAILMSG(1,(TEXT("==Set %s mask failure:%d!\r\n"),csSerialPort,GetLastError()));
103. }
104.
105. nmeathread_hand = CreateThread(NULL,0,ReadNMEAThread,pParent,0,NULL);
106. //nmeathread_hand = CreateThread(NULL, 0, ReadNMEAThread, this, CREATE_SUSPENDED, NULL);
107. if(!nmeathread_hand)
108. {
109. RETAILMSG(1, (L"Could not create NMEA read thread.\r\n"));
110. return FALSE;
111. }
112. else
113. {
114. //SetThreadPriority(nmeathread_hand, THREAD_PRIORITY_BELOW_NORMAL);
115. //ResumeThread(nmeathread_hand);
116. }
117.
118. SYSTEMTIME stUTC;
119. SYSTEMTIME stLocal;
120.
121. GetLocalTime(&stLocal);
122. GetSystemTime(&stUTC);
123. giHourDiff = stLocal.wHour - stUTC.wHour;
124.
125. return TRUE;
126.}