Programming serial port arduino with Visual C++ - c++

I want to read analog sensor from arduino to my pc.
Arduino program are:
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
char request[1];
if(Serial.available()){
request[0]=Serial.read();
Serial.print(analogRead(atoi(request)));
Serial.print("\n\r");
};
delay(2);
}
and my visual C++
#include <windows.h>
#include "stdafx.h"
#include "SerialClass.h"
char buffer[20];
char buf0[200];
int _tmain(int argc, _TCHAR* argv[])
{
Serial oSerial("COM6:");
while(1){
sprintf_s(buffer,"0");
oSerial.WriteData(buffer,1);
Sleep(1000);
oSerial.ReadData(buf0,4);
printf("Sensor 0: %s \n",buf0);
Sleep(1000); }
}
and result of my program is not stable, I put 5V in input0, so it must be 1023:
sensor 0 :
sensor 0 : 10230
sensor 0:
100
sensor 0: 23
0
sensor 0: 10230
I try in my arduino with serial monitor the program is working. so may be the problem is in the c++ program
any one have idea?

I believe the problem is caused because your PC is looking for 4 chars and the Arduino is sending 3-6 chars per request.
I would recommend you receive chars into your buffer until you receive the carriage return (\r) sent by the Arduino. Then you can output the complete string. You will need to handle the control characters.
Untested example:
Replace
oSerial.ReadData(buf0,4);
With
int x=0;
int char_rev;
while(buf0[x]!='\r') {
char_rev = oSerial.ReadData(buf0[x],1);
if (char_rev==1) {
x++;
}
}
buf0[x]=0;

Related

CAN BUS STM32 Nucleo F429ZI CAN Write Fail

It is Veysel My board is STM32 Nucleo F429ZI and I use Mbed-os 6.6.0. I can read from my car CANBus data but I am not able to write on the bus. I tried it with two STM32 (same boards) and my trancivers are SN65HVD230
So I know that I can read on the CANBus , but I cannot write.
I have tried , https://os.mbed.com/users/WiredHome/notebook/can---getting-started/ with one STM32 and I have tried
#include "mbed.h"
//Ticker ticker;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
//CAN can1(PD_0, PD_1);
CAN can2(PB_8, PB_9);
DigitalOut led3(LED3);
char counter = 0;
void messageReceivedISR() {
CANMessage msg;
if(can2.read(msg)) {
printf("Message received: %d, from %d\r\n", msg.data[0], msg.id);
if(msg.id == 1100)
led2 = !led2;
if(msg.id == 1102)
led3 = !led3;
}
}
int main() {
can2.frequency(1000000);
can2.reset();
can2.attach(&messageReceivedISR, CAN::RxIrq);
while(1) {
if(can2.write(CANMessage(1130, &counter, 1))) {
printf("loop send()\r\n");
counter++;
printf("Message sent: %d\r\n", counter);
led1 = !led1;
}
// wait_ms(500);
}
}
I switch writing address for both stm32
This is fulling mailbox three times and stop. None read.
I have tried directly connect with one jumper from Crx to Ctx but nothing changes.
I also tried it with STM32 to Teensy ( Teensy is controing my cars LED Screen it can also read ) , but failed.
Please Help ?
for me it sounds like you forgot to drive the "RS"-pin of the CAN Transciever. This is a bit misleading in the datasheet, but it is not only "slope" control, but it also sets the Transciever to sleep. I'd recommend to use a 10k slope control resistor unlesss needed otherwise as shown here: Schematics

Why is the Serial communication via BLE failing on the first run of the code but working fine for consecutive runs?

I'm trying to communicate with the STM32 microcontroller via serial communication. Before I was using the USB cable and the communication worked flawlessly. However when I switched the same serial mode from USB to BLE (HM-10), the microcontroller fails to respond correctly for the first try, but it is okay after that. What could be a problem?
I tried searching if it was something to do with buffers or start/stop bits. But it didn't help me much because I had no clue about how to implement them through code. I'm using the MbedOS to program my dev board.
CODE:
#include "mbed.h"
DigitalOut led1(LED1);
#define MAX_INPUT_LENGTH 2
int code[MAX_INPUT_LENGTH];
Serial bt(PD_5, PD_6);
//Serial pc(USBTX, USBRX);
int main()
{
while(1)
{
while(bt.readable()==1)
{
volatile char str[2];
bt.scanf("%2s",str);
int index = 0;
while(index<=MAX_INPUT_LENGTH)
{
code[index] = str[index] - '0'; // convert to an int.
index++; // increase the index.
}
if(code[0]==0 && code[1]==1)
{
bt.printf("\n01 RECIEVED.");
for(int x=0;x<15;x++)
{
led1 = 1;
wait(0.25);
led1 = 0;
wait(0.25);
}
}
else
bt.printf("\nINCORRECT VALUE RECIEVED.");
}
}
}
I expected the output from ble serial to work the same way as the usb serial, but it failed for the first run.

Arduino does not support messages larger than 65535 characters using websocket protocol?

I'm using the following Arduino websocket library, I had a problem when trying to send messages over than 65535 characters, I got handshake fail error.
As long as the message doesn't exceeds this length, it worked perfectly
There's a note on the main web page of the library that states:
Because of limitations of the current Arduino platform (Uno at the time of this writing),
this library does not support messages larger than 65535 characters.
In addition, this library only supports single-frame text frames.
It currently does not recognize continuation frames, binary frames, or ping/pong frames.
In the client header file named WebSocketClient.h there's the following comment:
// Don't allow the client to send big frames of data. This will flood the arduino memory and might even crash it.
#ifndef MAX_FRAME_LENGTH
#define MAX_FRAME_LENGTH 256
#endif
I'm using this old library because it is the only one worked for me on my Arduino WIFI shield, I couldn't find other libraries that support WiFi shield since most of the webscket libraries are written for Arduino Eathernet Shield support, which I don't have.
My Arduino Code is
/*DS18 Libs*/
#include <dht.h>
#include <OneWire.h>
#include <DallasTemperature.h>
/*Websocket Libs*/
#include <WebSocketServer.h>
#include <WebSocketClient.h>
#include <sha1.h>
#include <MD5.h>
#include <global.h>
#include <Base64.h>
#include <SPI.h>
#include <WiFiUdp.h>
#include <WiFiServer.h>
#include <WiFiClient.h>
#include <WiFi.h>
#include <string.h>
char ssid[] = "AMM";
char pass[] = "027274792";
int status = WL_IDLE_STATUS;
IPAddress server(192, 168, 1, 3);
WiFiClient WiFiclient;
WebSocketClient WSclient;
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
//Humidture
dht DHT;
#define DHT11_PIN 4
void setup()
{
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
//check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
/* Connect to the websocket server*/
if (WiFiclient.connect(server, 8080)) {
Serial.println("Connected");
}
else {
Serial.println("Connection failed.");
while (1) {
// Hang on failure
}
}
// Handshake with the server
WSclient.path = "/MyServer/endpoint/testtest/device/d6220ae7-caa9-48b5-92db-630c4c296ec4";
WSclient.host = "192.168.1.3:8080";
if (WSclient.handshake(WiFiclient)) {
Serial.println("Handshake successful");
}
else {
Serial.println("Handshake failed.");
while (1) {
// Hang on failure
}
}
/*DS18*/
sensors.begin();
}
void loop()
{
WSclient.sendData("{\"service_code\":\"89c4da72-a561-47db-bf62-8e63f8c4bbf0\",\"data\":[" + getHumidtureValue() + "],\"service_type\":\"TemperatureHumidityAnalysis\"}");
WSclient.sendData("{\"service_code\":\"bdc0f984-6550-4712-881f-b09071da5a73\",\"data\":" + getCBodyTempretureValue() + ",\"service_type\":\"TemperatureGaugeMonitor\"}");
//line-3 commented WSclient.sendData("{\"service_code\":\"8c212432-a86e-4c18-a956-9dc0dbb648d4\",\"data\":[" + getHumidtureValue() + "],\"service_type\":\"HumidityGaugeMonitor\"}");
}
String getCBodyTempretureValue()
{
sensors.requestTemperatures(); // Send the command to get temperatures
char charVal[10];
return dtostrf(sensors.getTempCByIndex(0), 4, 2, charVal);
}
String getHumidtureValue()
{
String str = "";
for (int i = 0; i < 2; i++)
{
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error,\t");
break;
default:
Serial.println("Unknown error,\t");
break;
}
char charVal[10];
double tempF = (DHT.temperature * 9) / 5 + 32;
str = dtostrf(tempF, 3, 1, charVal);
str = str + "," + dtostrf(DHT.humidity, 3, 1, charVal);
Serial.println(str);
delay(200);
}
return str;
}
The code above works perfectly, when I uncomment the third send statement in the loop function, I got the handshake failed error.
-Is it safe to modify the value of MAX_FRAME_LENGTH for the new versions of Arduino board, considering this library is an old one?
-Is there any other libraries better than this one that can support websocket on WiFi shield?
Any solution or idea will appreciated.
Thanks in advance.
Without having looked at the code of the library it is likely not safe to change the max frame length, because the websocket protocol encodes the payload length differently depending on how long it is:
Payload length: 7 bits, 7+16 bits, or 7+64 bits.
The length of the "Payload data", in bytes: if 0-125, that is the payload length. If 126, the following 2 bytes interpreted as a 16-bit unsigned integer are the payload length. If 127, the following 8 bytes interpreted as a 64-bit unsigned integer (the most significant bit MUST be 0) are the payload length.
When the library says it doesn't support payload length above 65535 byte, it likely means that it has no implementation for the 64-bit length encoding.
After many trials and many times the program behaves very strangely, which drives me crazy, I found the problem is that I'm using too much strings in my program which makes the Arduino-Uno easily runs out of RAM.
The main reason I got handshake failed error is that the Arduino cannot read the "Sec-WebSocket-Accept" header of the handshake response message (as many other headers also) which I made sure that they are sent, by debugging code on the server.
Actually this problem and many other strange behaviors keep happening until I reduce the amount of the memory used during program run.

interfacing c++ to control motor stepper with arduino

I have tried to control motor stepper via arduino uno board with slider in visual C++.
but the servo didnot move at all.
here the program at PC side:
void CENVSConfigDlg::OnBnClickedButton1()
{
SetTimer(cTimer1,80,NULL);
}
void CENVSConfigDlg::OnTimer(UINT_PTR ID)
{
if(ID==cTimer1){
DWORD nbytes;
char buf[5];
sprintf(buf, "%d \n", val_test);
/* Open serial port. */
if(!WriteFile( hnd_serial, (void*)buf, 5, &nbytes, NULL )){MessageBox(L"Write Com Port fail!");return;}
}
and the program in arduino:
#include <Servo.h>
Servo servoMain;
int index=0;
String inputString;
void setup()
{
Serial.begin(9600);
servoMain.attach(9);
}
void loop()
{
int data;
while (Serial.available())
{
char inChar = (char)Serial.read();
if (inChar == '\n' || inChar == 'z')
{
data=stringToInt(inputString);
Serial.println(data); //
inputString="";
servoMain.write(data); //tambahannya
delay(50);
break;
}
if (inChar != 0)
{
inputString += inChar;
}
}
}
int stringToInt(String s)
{
char char_string[s.length()+1];
s.toCharArray(char_string, s.length()+1);
return atoi(char_string);
}
the pc i think is sending the data, but why the motor didnot working? any idea?
First off, does the serial link work at all? The number of ways that an RS232 link can not work, for both hardware and software reasons, is legendary. Unless you can show that the hardware can transfer data, it's pointless to look at your dedicated software. If you have a scope, use that to see if anything is being transmitted and then check that it arrives at the correct pin on the arduino input. If you have no access to a scope, a small LED and a 4.7K resistor can be used to indicate that there is data on the line - it will flicker with the data.

Why is no serial data available on my Arduino?

I've run the simple serial program on my Arduino Uno, which just echos whatever you type to it. This works perfectly when run in the Arduino Sketch IDE (v22).
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(115200); // opens serial port, sets data rate
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
(Code taken from http://arduino.cc/en/Serial/Available)
However, I prefer not to use the Arduino IDE and would rather compile my C++ code with avr-g++, so I wrote this, which should function exactly the same as above:
extern "C" {
#include <avr/io.h>
}
#include <HardwareSerial.h>
extern "C" void __cxa_pure_virtual() { while(1); }
int main (void)
{
int incomingByte = 0;
Serial.begin(115200);
while (1) {
if (Serial.available() > 0) {
incomingByte = Serial.read();
//print as an ASCII character
Serial.print("received: ");
Serial.println(incomingByte, DEC);
}
}
return 1;
}
I compile and run it, but it doesn't work. I never see my text echoed back to me. I tried printing out the value of Serial.available() in the while(1) loop, and it's always zero. Whenever I type on the keyboard, I see the RX LED light up, but nothing happens after that. I can edit my code to successfully call Serial.println() as long as it's outside the Serial.available() conditional.
I've confirmed that my baud rate in my serial software is also set to 115200. And yes, my serial software is pointing to the right serial port.
What am I missing?
Arduino's original glue code looks like this:
#include <WProgram.h>
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
The init() stuff is missing in your code. init() is defined in $ARDUINO_HOME/hardware/arduino/cores/arduino/wiring.c, you can either link against it directly or just copy the code of init() into your code.
You probably have not properly initialized the UART port on the chip. This has to be done manually for microcontrollers, and the Arduino IDE was probably doing it for you. Check the AVR datasheet for your chip, specifically the serial port section.
Found the answer to my own question:
It turns out the HardwareSerial.h library relies on interrupts. This is something that is automagically taken care of for you when building with the Arduino IDE. If you aren't using the Arduino IDE (like me), then you must remember to enable interrupts on your own.
Just #include <avr/interrupt.h>, and call sei(); to turn on interrupts before you try to use the Serial Library.
cheers!