So I have this CH376S USB read/write module wired and found a script for it that worked.
All the module has to do is check if a file named TeamOne.txt or TeamTwo.txt is on the USB when one is plugged in, then it returns 1 or 2 to the function that called it or something that gets the same result
The only problem is that the script has 504 lines of text and the Arduino memory is almost full cause of that.
So I decided to remove a bit of the unnecessary stuff and shorten things.
I got the code from 504 lines to 167 lines but now it gives me an error on the serial monitor, not when verifying and uploading.
Does anyone know how to get it working?
I used the serial monitor to call the function for testing purposes now.
The original code can be found here: http://arduinobasics.blogspot.be/2015/05/ch376s-usb-readwrite-module.html
This is my shortened, broken, code:
#include <SoftwareSerial.h>
byte computerByte; //used to store data coming from the computer
byte USB_Byte; //used to store data coming from the USB stick
int LED = 13; //the LED is connected to digital pin 13
int timeOut = 2000; //TimeOut is 2 seconds. This is the amount of time you wish to wait for a response from the CH376S module.
String wrData = "What is the meaning of life ?"; //We will write this data to a newly created file.
String wrData2 = "42"; //We will append this data to an already existing file.
SoftwareSerial USB(10, 11); // Digital pin 10 on Arduino (RX) connects to TXD on the CH376S module
// Digital pin 11 on Arduino (TX) connects to RXD on the CH376S module
// GND on Arduino to GND on CH376S module
// 5V on Arduino to 5V on CH376S module
//==============================================================================================================================================
void setup() {
Serial.begin(9600); // Setup serial communication with the computer (using a baud rate of 9600 on serial monitor)
USB.begin(9600); // Setup serial communication with the CH376S module (using the default baud rate of 9600)
pinMode(LED,OUTPUT); // Define digital pin 13 as an OUTPUT pin - so that we can use it with an LED
digitalWrite(LED,LOW); // Turn off the LED
}
void loop() {
if(Serial.available()){
computerByte = Serial.read(); //read any incoming bytes from the Serial monitor, and store this byte in the variable called computerByte
if(computerByte==53){ //5
printCommandHeader("COMMAND5: Read File: TEST4.TXT");
readFile("TEST4.TXT"); // Read the contents of this file on the USB disk, and display contents in the Serial Monitor
}
}
if(USB.available()){ // This is here to capture any unexpected data transmitted by the CH376S module
Serial.print("CH376S has just sent this code:");
Serial.println(USB.read(), HEX);
}
}
//END OF LOOP FUNCTION ========================================================================================================================================
void printCommandHeader(String header){
Serial.println("======================");
Serial.println("");
Serial.println(header);
Serial.println("----------------------");
}
void readFile(String fileName){
USB.write(0x57);
USB.write(0xAB);
USB.write(0x05); //Reset the module
USB.write(0x57);
USB.write(0xAB);
USB.write(0x15);
USB.write(0x06); //Set to USB Mode
USB.write(0x57);
USB.write(0xAB);
USB.write(0x30); //Check that communication with the USB device is possible
USB.write(0x57);
USB.write(0xAB);
USB.write(0x31); //Prepare the USB for reading/writing - you need to mount the USB disk for proper read/write operations.
USB.write(0x57);
USB.write(0xAB);
USB.write(0x2F);
USB.write(0x2F); // Every filename must have this byte to indicate the start of the file name.
USB.print(fileName); // "fileName" is a variable that holds the name of the file. eg. TEST.TXT
USB.write((byte)0x00); // you need to cast as a byte - otherwise it will not compile. The null byte indicates the end of the file name.
delay(20); //Set File name
USB.write(0x57);
USB.write(0xAB);
USB.write(0x32); //Open the file for reading
//int fs = getFileSize(); Get the size of the file
fileRead(); //***** Send the command to read the file ***
USB.write(0x57);
USB.write(0xAB);
USB.write(0x36);
USB.write((byte)0x00); //Close the file
}
void fileRead(){
Serial.println("Reading file:");
byte firstByte = 0x00; //Variable to hold the firstByte from every transmission. Can be used as a checkSum if required.
byte numBytes = 0x40; //The maximum value is 0x40 = 64 bytes
while(setByteRead(numBytes)){ //This tells the CH376S module how many bytes to read on the next reading step. In this example, we will read 0x10 bytes at a time. Returns true if there are bytes to read, false if there are no more bytes to read.
USB.write(0x57);
USB.write(0xAB);
USB.write(0x27); //Command to read ALL of the bytes (allocated by setByteRead(x))
if(waitForResponse("reading data")){ //Wait for the CH376S module to return data. TimeOut will return false. If data is being transmitted, it will return true.
firstByte=USB.read(); //Read the first byte
while(USB.available()){
Serial.write(USB.read()); //Send the data from the USB disk to the Serial monitor
delay(1); //This delay is necessary for successful Serial transmission
}
}
if(!continueRead()){ //prepares the module for further reading. If false, stop reading.
break; //You need the continueRead() method if the data to be read from the USB device is greater than numBytes.
}
}
Serial.println();
Serial.println("NO MORE DATA");
}
boolean setByteRead(byte numBytes){
boolean bytesToRead=false;
int timeCounter = 0;
USB.write(0x57);
USB.write(0xAB);
USB.write(0x3A);
USB.write((byte)numBytes); //tells the CH376S how many bytes to read at a time
USB.write((byte)0x00);
if(waitForResponse("setByteRead")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
if(getResponseFromUSB()==0x1D){ //read the CH376S message. If equal to 0x1D, data is present, so return true. Will return 0x14 if no data is present.
bytesToRead=true;
}
}
return(bytesToRead);
}
boolean continueRead(){
boolean readAgain = false;
USB.write(0x57);
USB.write(0xAB);
USB.write(0x3B);
if(waitForResponse("continueRead")){ //wait for a response from the CH376S. If CH376S responds, it will be true. If it times out, it will be false.
if(getResponseFromUSB()==0x14){ //CH376S will send 0x14 if this command was successful
readAgain=true;
}
}
return(readAgain);
}
//waitForResponse===================================================================================
//is used to wait for a response from USB. Returns true when bytes become available, false if it times out.
boolean waitForResponse(String errorMsg){
boolean bytesAvailable = true;
int counter=0;
while(!USB.available()){ //wait for CH376S to verify command
delay(1);
counter++;
if(counter>timeOut){
Serial.print("TimeOut waiting for response: Error while: ");
Serial.println(errorMsg);
bytesAvailable = false;
break;
}
}
delay(1);
return(bytesAvailable);
}
//getResponseFromUSB================================================================================
//is used to get any error codes or messages from the CH376S module (in response to certain commands)
byte getResponseFromUSB(){
byte response = byte(0x00);
if (USB.available()){
response = USB.read();
}
return(response);
}
Serial monitor:
======================
COMMAND5: Read File: TEST4.TXT
----------------------
Reading file:
TimeOut waiting for response: Error while: setByteRead
NO MORE DATA