pySerial在Arduino和SRAM芯片之间将数据发送到Arduino和SPI之间的冲突

时间:2019-04-19 13:21:36

标签: python arduino ram pyserial spi

我正在通过pySerial将阵列发送到Arduino Nano的过程中,一旦接收到,Nano就会使用SPI直接发送到SRAM芯片。但是,当执行与SPI相关的代码(例如设置用于发送单个字节的传输模式)时,将阻止从Python传输数据。

我知道问题并非出自同时使用两个引脚,因为它们使用不同的引脚,因此我认为函数定义存在问题。我觉得不得不说我在python和arduino方面的专业知识很少。对于长代码的道歉以及对此的任何帮助,将不胜感激!

# Python code
import serial      
import time                                    #To use delay functions   
ArduinoNano = serial.Serial('com6',9600)       #Create Serial port object called ArduinoNano
x = ArduinoNano.readline()                     #Read the serial data and print it
print(x)

gImage_1 = bytes([
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,
0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XFF,0XC0,
0X00,0X00,0X00,0X00,0X00,0X00]);

t = 0
for t in gImage_1:
    ArduinoNano.write(bytes([t]))
    y = ArduinoNano.readline()
    print(y)


----------
# Arduino Code
#include <SPI.h>

#define WRSR        1       // Write to the Status Register
#define WRITE       2       // Write command
#define ByteMode    0x00    // Byte mode (read/write one byte at a time)
#define SS1         10      // Slave Select Line for Nano

uint16_t address = 0;       // create a 16 bit variable to hold the address
byte incomingData = 0;      // data from pySerial
int dataCount = 0;

void SetMode(char Mode);
void WriteByte(uint16_t address, byte data_byte);

void setup() 
{
  pinMode(SS1, OUTPUT);                          
  Serial.begin(9600);
  SPI.begin;
  Serial.println("Hello! Waiting... ");
  SetMode(ByteMode);
}

void loop() 
{
  while(Serial.available() > 0)
  {
    address = dataCount;
    incomingData = Serial.read();
    WriteByte(address, incomingData);
    Serial.println(incomingData);
    dataCount += 1;
  }
}

void SetMode(char Mode)                          // Select for single or multiple byte transfer
{                         
  digitalWrite(SS1, LOW);                        // set SPI slave select LOW;
  SPI.transfer(WRSR);                            // command to write to status register
  SPI.transfer(Mode);                            // set for byte mode
  digitalWrite(SS1, HIGH);                       // release chip select to finish command
}

void WriteByte(uint16_t address, byte data_byte) 
{
  digitalWrite(SS1, LOW);                        // set SPI slave select LOW;
  SPI.transfer(WRITE);                           // send WRITE command to the memory chip
  SPI.transfer((byte)(address >> 8));            // send first byte of address
  SPI.transfer((byte)address);                   // send last byte of address
  SPI.transfer(data_byte);                       // write the data to the memory location
  digitalWrite(SS1, HIGH);                       // set SPI slave select HIGH
}

0 个答案:

没有答案