linux下与Arduino通信

第一次使用Arduino IDE时,如果不是要求最新的版本,在Ubuntu的应用商店里即可下载到Arduino的IDE,安装完成后会提示将当前用户添加到dialout用户组中,这样才能下载,选择对话框的ADD即可,或者用下面的语句:
Arduino官网示例

ls -l /dev/ttyACM*
you will get something like:

crw-rw---- 1 root dialout 188, 0 5 apr 23.01 ttyACM0

The "0" at the end of ACM might be a different number, or multiple entries might be returned. The data we need is "dialout" (is the group owner of the file).

Now we just need to add our user to the group:

sudo usermod -a -G dialout 

where  is your linux user name. You will need to log out and log in again for this change to take effect. 

在arduino pro mini上进行串口通信时,利用FTD1232,下载时需要用DTR data terminal ready,这样就不用在下载时按复位键了。 在linux上与它通信时,有个现象,重新拔插USB后,第一此执行程序时,没反应,再次执行时,就可以了,后来拔掉DTR后,无论是否刚插上USB,都可以直接进行通信。
linux下:

//============================================================================
// Name        : SerialPortSend.cpp
// Author      : zn
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include 
#include  /* Standard input/output definitions */
#include  /* String function definitions */
#include  /* UNIX standard function definitions   write 在这里 */
#include  /* File control definitions  O_RDWR 在这里 */
#include  /* Error number definitions */
#include  /* POSIX terminal control definitions */
using namespace std;
int  ToSerial()
{
    int  file = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY); //以读写方式打开串口,不控制TTY
    if(file < 0)
    {
        printf("error to open\n");
        return 1;
    }
    usleep(20000);
    write(file,"b",2);
    usleep(20000);
    write(file,"f",2);
    usleep(20000);
    usleep(20000);
    printf("hello!\n");
    close(file);
    return 0;
}
int main() {
    for(int i=0;i<1;i++)
    {
        ToSerial();
    }
    return 0;
}

Arduino下:

int x;
void setup()  
{  
  Serial.begin(9600);
  pinMode(9,OUTPUT); // Enable 
  pinMode(3,OUTPUT); // Step  
  pinMode(2,OUTPUT); // Dir  

}  
void loop()  
{  
  char ch = Serial.read();
  switch(ch){
  case 'f': 
    digitalWrite(9,LOW); // Set Enable low 
    fwd();
    Serial.println("receive f");
    digitalWrite(9,HIGH); // Set Enable low 
    break;
  case 'b':
    digitalWrite(9,LOW); // Set Enable low 
    bwd();
    Serial.println("receive b");
    digitalWrite(9,HIGH); // Set Enable low 
    break;
  }
}

void fwd(){
  digitalWrite(2,HIGH); // Set Dir high  
  for(x = 0; x < 400; x++) // Loop 400 times  1.8*200=360
    {
      digitalWrite(3,HIGH); // Output high  
      delayMicroseconds(5000); // Wait  ms  
      digitalWrite(3,LOW); // Output low  
      delayMicroseconds(2500); // Wait a ms  
    } 
delay(1000); // pause one second  
}

void bwd(){
  digitalWrite(2,LOW); // Set Dir low  
  for(x = 0; x < 400; x++) // Loop 400 times  
    {  
      digitalWrite(3,HIGH); // Output high  
      delayMicroseconds(5000); // Wait ms  
      digitalWrite(3,LOW); // Output low  
      delayMicroseconds(2500); // Wait ms  
    }
delay(1000); // pause one second  
}

浅谈linux下的串口通讯开发

linux下查看串口信息

sudo ttylog -d /dev/ttyACM0 -b 38400(这个是波特率)

你可能感兴趣的:(linux下与Arduino通信)