简体   繁体   English

用arduino连接c ++以控制电机步进

[英]interfacing c++ to control motor stepper with arduino

I have tried to control motor stepper via arduino uno board with slider in visual C++. 我试图在Visual C ++中通过带有滑块的arduino uno板来控制电机步进器。

but the servo didnot move at all. 但是伺服系统根本没有动。

here the program at PC side: 这是PC端的程序:

void CENVSConfigDlg::OnBnClickedButton1() 
{ 
 SetTimer(cTimer1,80,NULL); 
  }

 void CENVSConfigDlg::OnTimer(UINT_PTR ID)
 { 
   if(ID==cTimer1){ 
    DWORD nbytes;
    char buf[5]; 
    sprintf(buf, "%d \n", val_test);
     /* Open serial port. */ 
       if(!WriteFile( hnd_serial, (void*)buf, 5, &nbytes, NULL )){MessageBox(L"Write Com Port fail!");return;} 
 }

and the program in arduino: 和arduino中的程序:

 #include <Servo.h> 
 Servo servoMain;

int index=0; 
String inputString;

void setup() 
{ 
Serial.begin(9600);
servoMain.attach(9);
}

void loop() 
{ 
  int data;
while (Serial.available()) 
{ 
char inChar = (char)Serial.read(); 
if (inChar == '\n' || inChar == 'z') 
{
  data=stringToInt(inputString); 
Serial.println(data); //
inputString=""; 

servoMain.write(data); //tambahannya
delay(50);

break; 
} 
if (inChar != 0)
{ 
inputString += inChar;
} 
} 
}

int stringToInt(String s)
{ 
char char_string[s.length()+1]; 
 s.toCharArray(char_string, s.length()+1); 
 return atoi(char_string);
 } 

the pc i think is sending the data, but why the motor didnot working? 我认为正在发送数据的PC,但是为什么电机没有工作? any idea? 任何想法?

First off, does the serial link work at all? 首先,串行链接是否可以正常工作? The number of ways that an RS232 link can not work, for both hardware and software reasons, is legendary. 出于硬件和软件原因,RS232链接无法工作的方式是传奇性的。 Unless you can show that the hardware can transfer data, it's pointless to look at your dedicated software. 除非可以证明硬件可以传输数据,否则查看专用软件毫无意义。 If you have a scope, use that to see if anything is being transmitted and then check that it arrives at the correct pin on the arduino input. 如果您有示波器,请使用该示波器查看是否有任何信号在传输,然后检查它是否到达arduino输入上的正确引脚。 If you have no access to a scope, a small LED and a 4.7K resistor can be used to indicate that there is data on the line - it will flicker with the data. 如果您无法访问示波器,则可以使用一个小的LED和一个4.7K电阻来指示线路上有数据-它会随着数据闪烁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM