I need some help because I don't know what to do. The concept of my project is to send a data from VS to Arduino to get the current date so I can print it in a thermal printer connected to arduino. I am able to print the date directly but I want to store this information to the Arduino through SerialPort when the program starts.
port.Write("#TEXT" + System.DateTime.Now.ToString("yyyy/MM/dd") + "#\n");
How can I make the arduino store it in a variable so I can print the current date?
Here is my code for Arduino:
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define TX_PIN 13 // Arduino transmit YELLOW WIRE labeled RX on printer
#define RX_PIN 12 // Arduino receive GREEN WIRE labeled TX on printer
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
String commandString = "";
boolean isConnected = false;
void setup() {
Serial.begin(19200);
mySerial.begin(19200);
printer.begin();
}
void loop() {
if(stringComplete)
{
stringComplete = false;
getCommand();
if(commandString.equals("STAR"))
{
printer.println("Starting...");
}
else if(commandString.equals("TEXT"))
{
String text = getTextToPrint();
printText(text);
}
inputString = "";
}
}
void getCommand()
{
if(inputString.length()>0)
{
commandString = inputString.substring(1,5);
}
}
String getTextToPrint()
{
String value = inputString.substring(5,inputString.length()-2);
return value;
}
void printText(String text)
{
if(text.length()<16)
{
printer.feed();
printer.justify('C');
printer.setSize('M');
printer.println(text);
printer.feed();
printer.feed();
printer.feed();
printer.feed();
printer.feed();
printer.feed();
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
And here is my code for my Visual Studio.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace ComputerToArduino
{
public partial class Form1 : Form
{
bool isConnected = false;
String[] ports;
SerialPort port;
public Form1()
{
InitializeComponent();
disableControls();
getAvailableComPorts();
foreach (string port in ports)
{
comboBox1.Items.Add(port);
Console.WriteLine(port);
if (ports[0] != null)
{
comboBox1.SelectedItem = ports[0];
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (!isConnected)
{
connectToArduino();
} else
{
disconnectFromArduino();
}
}
void getAvailableComPorts()
{
ports = SerialPort.GetPortNames();
}
private void connectToArduino()
{
isConnected = true;
string selectedPort = comboBox1.GetItemText(comboBox1.SelectedItem);
port = new SerialPort(selectedPort, 19200, Parity.None, 8, StopBits.One);
port.Open();
button1.Text = "Disconnect";
enableControls();
}
private void disconnectFromArduino()
{
isConnected = false;
port.Close();
button1.Text = "Connect";
disableControls();
}
private void button2_Click(object sender, EventArgs e)
{
if (isConnected)
{
port.Write("#TEXT" + System.DateTime.Now.ToString("yyyy/MM/dd") + "#\n");
}
}
private void enableControls()
{
button2.Enabled = true;
groupBox3.Enabled = true;
}
private void disableControls()
{
button2.Enabled = false;
groupBox3.Enabled = false;
}
private void groupBox3_Enter(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Also, I need help with sending data the other way around. I want to send data from the arduino to VS and store it in a variable.