雖然問題出在C++端,但Arduino Nano和C++之間的串行端口通信存在問題。基本上我想從Arduino發送整數(或長,...)到C++程序進行處理。使用Arduino和C++進行串行端口通信
首先我做了一個測試,使用Matlab將信息從Arduino發送到計算機。 Arduino的代碼非常簡單:
int i = 0;
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
establishContact();
}
void loop() {
Serial.println(i);
i=i+1;
delay(10);
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println('A', BYTE);
delay(10);
}
}
Matlab的側面也很簡單:
clc;
clear all;
numSec=2;
t=[];
v=[];
s1 = serial('COM3'); % define serial port
s1.BaudRate=9600; % define baud rate
set(s1, 'terminator', 'LF'); % define the terminator for println
fopen(s1);
try % use try catch to ensure fclose
% signal the arduino to start collection
w=fscanf(s1,'%s'); % must define the input % d or %s, etc.
if (w=='A')
display(['Collecting data']);
fprintf(s1,'%s\n','A'); % establishContact just wants
% something in the buffer
end
i=0;
t0=tic;
while (toc(t0)<=numSec)
i=i+1;
t(i)=toc(t0);
t(i)=t(i)-t(1);
v(i)=fscanf(s1,'%d');
end
fclose(s1);
plot(t,v,'*r')
catch me
fclose(s1);
end
我的目標是,用C++,這樣做是在Matlab使用的fscanf做了同樣的(S1, '%d')。
下面是當前的代碼,我使用(C++代碼):
void main()
{
HANDLE hSerial;
hSerial = CreateFile(TEXT("COM3"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED,
NULL);
if (hSerial == INVALID_HANDLE_VALUE)
{
printf("Error initializing handler");
}
else
{
// Set the parameters of the handler to the serial port.
DCB dcb = {0};
dcb.DCBlength = sizeof(dcb);
if (!GetCommState(hSerial, &dcb))
{
printf("Error setting parameters");
}
FillMemory(&dcb, sizeof(dcb), 0);
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
if (!SetCommState(hSerial, &dcb))
{
// error setting serial port state.
}
// Tell the program not to wait for data to show up
COMMTIMEOUTS timeouts = {0};
timeouts.ReadIntervalTimeout = 0;//20;
timeouts.ReadTotalTimeoutConstant = 0;//20;
timeouts.ReadTotalTimeoutMultiplier = 0;//50;
timeouts.WriteTotalTimeoutConstant = 0;//100;
timeouts.WriteTotalTimeoutMultiplier = 0;//100;
if (!SetCommTimeouts(hSerial, &timeouts))
{
printf("Error setting the timeouts");
}
char szBuff[5] = "";
DWORD dwBytesRead = 0;
int i = 0;
char test[] = "B\n";
int maxSamples = 10;
DWORD dwCommStatus;
WriteFile(hSerial, test, 2, &dwBytesRead, NULL);
SetCommMask(hSerial,EV_RXCHAR);
while (i < maxSamples)
{
WaitCommEvent (hSerial, &dwCommStatus, 0);
if (dwCommStatus & EV_RXCHAR)
{
memset(szBuff,0,sizeof(szBuff));
ReadFile(hSerial, LPVOID(szBuff), 4, &dwBytesRead, NULL);
cout<<szBuff;
printf(" - %d - \n", atoi(szBuff));
}
i++;
}
scanf("%d", &i);
CloseHandle(hSerial);
}
}
我的代碼的目標是像num = ReadSerialCOM(hSerial, "%d");
我當前的C++代碼從緩衝區讀取信息,但沒有一個可接受的行結束,這意味着我的數字(整數)被收到削減。
如:
我從Arduino的,哪些地方是在COM端口發送8889。命令ReadFile
將'88'保存爲szBuff
。在下一次迭代'89 \ n'被保存到sZBuff
。基本上我想避免後處理sZBuff
來連接'88'和'89 \ n'。
有人嗎? 謝謝!
也許你曾在此格式它可能幫助? – 2010-08-11 02:29:31
我在格式化文本時沒有問題,但我不確定您指的是什麼。代碼,「解釋」還是兩者? PS:注意引號的解釋。我不確定我是否很好地解釋了這個問題。 – sergi 2010-08-11 02:51:13
只要使用原始API,就無法避免它。帶有實現ReadLine()的串口封裝器的類庫當然可用。 – 2010-08-11 02:58:21