Question Summary:
It works, but on my specific case I am get into problems due to data encoding. Data is sent through the arduino as "coin" but its printed output in my console application is "2". Then I have the issue of sending the string "1234" from my console application, and not being able to receive it in the arduino. I think the data is being set as a different encoding so that the statement is nos looking for the right information. It is that simple and that complicated. (oh stackoverflow!)
Serial port is open and baud rate is set
Arduino code snippets:
Serial.println('coin');
if( Serial.readString() == "1234");
<<<--- how can i relax this condition as i did in the example below. (it does work on the Serial monitor but not on the software, is it an encoding situation?)
VS code snippets:
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
if (indata.Contains("coin")) <<<--- it is a relaxed contains condition, **works** in my case, since I dont really know excaclty what it is receiving.
SerialPort.Write("1234");
(how do I make sure it receives the characters i send be it ANSI hex or whatever.
thanks for your help.
Start of Regular post
Good afternoon,
I have arduino code:
int ledPin = 13;
const int coinInt = 0;
int incomingByte = 0;
#define RELAY_ON 1
#define RELAY_OFF 0
#define Relay_2 7
int waittime;
volatile int coinsValue = 00;
volatile int coinsChange = 0; //A Coin has been inserted flag
void setup() {
digitalWrite(Relay_2, RELAY_ON);
pinMode(Relay_2, OUTPUT);
delay(1000);
Serial.begin(9600);
attachInterrupt(coinInt, coinInserted, RISING); //If coinInt goes HIGH (a Pulse), call the coinInserted function
}
void coinInserted() {
coinsValue = coinsValue + 01;
coinsChange = 1; //Flag
}
void loop() {
if(coinsChange == 1)
{
coinsChange = 0;
if(coinsValue >= 50) {
Serial.println('coininsertion');
coinsValue = 0;
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(ledPin, HIGH);
}
if (Serial.available() > 0) {
incomingByte = Serial.read();
delay(250);
digitalWrite(ledPin, LOW);
digitalWrite(Relay_2, RELAY_ON);
}
}
}
and c# console application code:
using System;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
class PortDataReceived
{
public static void Main()
{
SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.RtsEnable = true;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
mySerialPort.Open();
Console.WriteLine("Press a key to continue...");
Console.WriteLine();
Console.ReadKey();
mySerialPort.Close();
}
private static void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
// Thread.Sleep(100);
// Console.Write(indata);
Thread.Sleep(100);
if (indata.Contains("coininsertion"))
{
Thread.Sleep(100);
//Process.Start(@"C:\\Users\laptop\Documents\rb_software\print.ahk");
Process process = new Process();
// Configure the process using the StartInfo properties.
process.StartInfo.FileName = @"C:\\Users\laptop\Documents\rb_software\print.ahk";
//process.StartInfo.Arguments = "-n";
//process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
process.Start();
Thread.Sleep(100);
process.WaitForExit();
Thread.Sleep(1000);// Waits here for the process to exit.
sp.Write("a");
}
Console.Write(indata);
}
}
A relay closes in the arduino code at the very end:
void loop() {
if(coinsChange == 1)
{
coinsChange = 0;
if(coinsValue >= 50) {
Serial.println('coininsertion');
coinsValue = 0;
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(ledPin, HIGH);
}
if (Serial.available() > 0) {
incomingByte = Serial.read();
delay(250);
digitalWrite(ledPin, LOW);
digitalWrite(Relay_2, RELAY_ON);
}
}
}
Or at least its supposed to close. Since I sent a character over serial connection in my c++ code near the end:
process.WaitForExit();
Thread.Sleep(1000);// Waits here for the process to exit.
sp.Write("a");
Anyhow, I would appreciate if someone could tell me why it is not closing the switch. Namely turning Relay_2 "ON".
That is all. thanks again.