这篇文章一定要写出来,实在不想每次调试arduino和processing串口通讯的时候都要测试半个小时了。。。

首先,这篇文章主要是讨论processing和arduino之间串口通讯的。不解释,做过arduino+processing的一定都遇到过这样的问题,arduino/processing想要给对方发送数据,或者从对方接收到数据,各种函数不知道要怎么写。。

先列一下二者串口的函数。。

processing

读取

read()
readChar()
readBytes()
readBytesUntil()
readString()
readStringUntil()

写入

write()

arduino

读取

read()
readBytes()
readBytesUntil()
readString()
readStringUntil()

写入

print()
println()
write()

下面根据使用场景列一下代码

arduino给processing发送一个0-255的数字

arduino:

serial.write();

processing:

serial.read();

arduino给processing发送一个字符

processing

     import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() 
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my  FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
}

void draw()
{
if ( myPort.available() > 0) {  // If data is available,
val = myPort.read();         // read it and store it in val
print(val);
}



}


void keyPressed(){

int i = 2;

myPort.write('2');
delay(100);

}

arduino

// Wiring/Arduino code:
// Read data from the serial and turn ON or OFF a light depending on the value

int val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 4

void setup() {
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
if(val == '2'){
Serial.write('0');

}else
Serial.print(val);
}
}

arduino给processing发送一个大于255的数字

arduino

processing

Serial.println(number);

processing

String inString = myPort.readStringUntil('\n');
inString = inString.trim();
int number = int(inString);

arduino 连接串口摄像头,获取照片信息,然后通过串口发送给processing

arduino

//  File SerialCamera_DemoCode_CJ-OV528.ino
//  8/8/2013 Jack Shao
//  Demo code for using seeeduino or Arduino board to cature jpg format
//  picture from seeed serial camera and save it into sd card. Push the
//  button to take the a picture .
//  For more details about the product please check http://www.seeedstudio.com/depot/

#include <SPI.h>
#include <arduino.h>
#include <SD.h>

#define PIC_PKT_LEN    128                  //data length of each read, dont set this too big because ram is limited
#define PIC_FMT_VGA    7
#define PIC_FMT_CIF    5
#define PIC_FMT_OCIF   3
#define CAM_ADDR       0
#define CAM_SERIAL     Serial1

#define PIC_FMT        PIC_FMT_VGA

File myFile;

const byte cameraAddr = (CAM_ADDR << 5);  // addr
unsigned long picTotalLen = 0;            // picture length


/*********************************************************************/
void setup()
{
    Serial.begin(115200);
    CAM_SERIAL.begin(115200);
    // Serial.println("Initializing SD card....");
    pinMode(10,OUTPUT);          // CS pin of SD Card Shield

    if (!SD.begin(10)) 
    {
        // Serial.print("sd init failed");
        return;
    }
    // Serial.println("sd init done.");
    initialize();
}
/*********************************************************************/
void loop()
{

  if (Serial.available()) { // If data is available to read,
  int val = Serial.read(); // read it and store it in val
  if(val == '2'){



          preCapture();
          Capture();
          GetData();
          sendData();
 
   }
   }     
    
}
/*********************************************************************/
void clearRxBuf()
{
    while (CAM_SERIAL.available())
    {
        CAM_SERIAL.read();
    }
}
/*********************************************************************/
void sendCmd(char cmd[], int cmd_len)
{
    for (char i = 0; i < cmd_len; i++) CAM_SERIAL.print(cmd[i]);
}
/*********************************************************************/
void initialize()
{
    char cmd[] = {0xaa,0x0d|cameraAddr,0x00,0x00,0x00,0x00} ;
    unsigned char resp[6];

    CAM_SERIAL.setTimeout(500);
    while (1)
    {
        //clearRxBuf();
        sendCmd(cmd,6);
        if (CAM_SERIAL.readBytes((char *)resp, 6) != 6)
        {
            continue;
        }
        if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x0d && resp[4] == 0 && resp[5] == 0)
        {
            if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
            if (resp[0] == 0xaa && resp[1] == (0x0d | cameraAddr) && resp[2] == 0 && resp[3] == 0 && resp[4] == 0 && resp[5] == 0) break;
        }
    }
    cmd[1] = 0x0e | cameraAddr;
    cmd[2] = 0x0d;
    sendCmd(cmd, 6);
    // Serial.println("\nCamera initialization done.");
}
/*********************************************************************/
void preCapture()
{
    char cmd[] = { 0xaa, 0x01 | cameraAddr, 0x00, 0x07, 0x00, PIC_FMT };
    unsigned char resp[6];

    CAM_SERIAL.setTimeout(100);
    while (1)
    {
        clearRxBuf();
        sendCmd(cmd, 6);
        if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
        if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x01 && resp[4] == 0 && resp[5] == 0) break;
    }
}
void Capture()
{
    char cmd[] = { 0xaa, 0x06 | cameraAddr, 0x08, PIC_PKT_LEN & 0xff, (PIC_PKT_LEN>>8) & 0xff ,0};
    unsigned char resp[6];

    CAM_SERIAL.setTimeout(100);
    while (1)
    {
        clearRxBuf();
        sendCmd(cmd, 6);
        if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
        if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x06 && resp[4] == 0 && resp[5] == 0) break;
    }
    cmd[1] = 0x05 | cameraAddr;
    cmd[2] = 0;
    cmd[3] = 0;
    cmd[4] = 0;
    cmd[5] = 0;
    while (1)
    {
        clearRxBuf();
        sendCmd(cmd, 6);
        if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
        if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x05 && resp[4] == 0 && resp[5] == 0) break;
    }
    cmd[1] = 0x04 | cameraAddr;
    cmd[2] = 0x1;
    while (1)
    {
        clearRxBuf();
        sendCmd(cmd, 6);
        if (CAM_SERIAL.readBytes((char *)resp, 6) != 6) continue;
        if (resp[0] == 0xaa && resp[1] == (0x0e | cameraAddr) && resp[2] == 0x04 && resp[4] == 0 && resp[5] == 0)
        {
            CAM_SERIAL.setTimeout(1000);
            if (CAM_SERIAL.readBytes((char *)resp, 6) != 6)
            {
                continue;
            }
            if (resp[0] == 0xaa && resp[1] == (0x0a | cameraAddr) && resp[2] == 0x01)
            {
                picTotalLen = (resp[3]) | (resp[4] << 8) | (resp[5] << 16);
                // Serial.print("picTotalLen:");
                Serial.println(picTotalLen);
                break;
            }
        }
    }

}
/*********************************************************************/
void GetData()
{
    unsigned int pktCnt = (picTotalLen) / (PIC_PKT_LEN - 6);
    if ((picTotalLen % (PIC_PKT_LEN-6)) != 0) pktCnt += 1;

    char cmd[] = { 0xaa, 0x0e | cameraAddr, 0x00, 0x00, 0x00, 0x00 };
    unsigned char pkt[PIC_PKT_LEN];

    char picName[] = "pic.jpg";

    if (SD.exists(picName))
    {
        SD.remove(picName);
    }

    myFile = SD.open(picName, FILE_WRITE);
    if(!myFile){
        // CAM_SERIAL.println("myFile open fail...");
    }
    else{
        CAM_SERIAL.setTimeout(1000);
        for (unsigned int i = 0; i < pktCnt; i++)
        {
            cmd[4] = i & 0xff;
            cmd[5] = (i >> 8) & 0xff;

            int retry_cnt = 0;
            retry:
            delay(10);
            clearRxBuf();
            sendCmd(cmd, 6);
            uint16_t cnt = CAM_SERIAL.readBytes((char *)pkt, PIC_PKT_LEN);

            unsigned char sum = 0;
            for (int y = 0; y < cnt - 2; y++)
            {
                sum += pkt[y];
            }
            if (sum != pkt[cnt-2])
            {
                if (++retry_cnt < 100) goto retry;
                else break;
            }

            myFile.write((const uint8_t *)&pkt[4], cnt-6);
            //if (cnt != PIC_PKT_LEN) break;
        }
        cmd[4] = 0xf0;
        cmd[5] = 0xf0;
        sendCmd(cmd, 6);
    }
    myFile.close();
}




void sendData(){
    File photoFile = SD.open("pic.jpg");   
    
    if (photoFile) {   
        while (photoFile.position() < photoFile.size()) {   
    
      Serial.write(photoFile.read());   
        }

    }else{
      Serial.println("open photoFile failed");
    }   
    
     photoFile.close();   



}    

processing

    import processing.serial.*;  

    Serial myPort;  
    OutputStream output;  


    int flag = 0;
    int count = -1;

    String inString = "0";
    int pic_number;


    PImage img;


    void setup() {  

        size(800, 600);  

        //println( Serial.list() );  
        myPort = new Serial( this, Serial.list()[2], 115200);  
        myPort.clear();  


    }  


    void draw() {  



         
            while ( myPort.available () > 0 ) {
                if (flag == 0) {
                    inString = myPort.readStringUntil('\n');
                    inString =  trim(inString);
                    pic_number = int(inString);
                    println("pic_number: "+pic_number);
                    flag = 1;
                }else {
                    try {  
                    output.write(myPort.read());

                    }   
                    catch (IOException e) {  
                        e.printStackTrace();  
                    }  


                    count++;
                

                    if (count == pic_number) {


                        try {   
                            output.flush(); // Writes the remaining data to the file   //<>//
                            output.close(); // Finishes the file  

                        }   

                        catch (IOException e) {  
                            e.printStackTrace();  
                        }  


                        img = loadImage("pic.jpg");
                        image(img, 0, 0);
                        break;
                    }


                }
            }  
        


       




    }  


    void keyPressed() {  
        if(key == 'a'){
            output = createOutput("pic.jpg"); 
            myPort.write('2');
            flag = 0;
            count = 0;


            println("taking a photo");
        }
    }    

Tips:

这个函数很吊,trim()可以把两边的特殊符号去掉

用arduino自带的串口调试软件发送东西的时候相当于serial.println();所以会在内容后面带上\t\n所以用serial.read的话是不能完全读完的,像这样

while(Serial.available() > 0){
  int temp = Serial.readString();
}

会得到奇怪的东西,这样就可以了。。。

while(Serial.available() > 0){
  String temp = Serial.readString();
}

一个神奇的函数

可以节省很多代码,来源是arduino样例代码里面的SerialCallResponse
split()

processing读取大量数据

最近遇到一个问题,就是processing和arduino通讯传输大量数据然后发现有点问题。。我发现可能processing3.0对串口一些变化?因为这段代码我以前是可以工作的

temp = myPort.readChar();
while( temp == 0xffff)
temp = myPort.readChar();

但是现在我一定要在while里面有个delay(1);才能工作。而且时间很长,要用几百ms才能查询到数据。。。
目前还不知道为什么。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容