I am using HM10(Bluetooth module) and the NUCLEO board.
I am trying to send data (like A, B... just characters) from my android phone using "Serial Bluetooth terminal" app. I succeeded in connecting the phone and HM10, but data is not sent to HM10. I never know what is problem. Plz, give me some advice.
#include "mbed.h"
// set up the HM10 Serial connection
BufferedSerial hm10(D10,D2,9600); //TX, RX
BufferedSerial pc(CONSOLE_TX,CONSOLE_RX,9600);
DigitalOut led1(D3);
DigitalOut led2(D4);
char buf[80];
int main(){
sprintf(buf,"\r\nHello New Serial Function in mbed-os v.%.1f\r\n",6.0);
pc.write(buf, strlen(buf));
while(true) {
sprintf(buf,"waiting for data");
pc.write(buf, strlen(buf));
int num = hm10.read(buf,sizeof(buf));
led1=!led1; // for debugging
pc.write(buf,num);
//memset(buf,0,sizeof(buf));
}
}
Related
I'm trying to read a MODBUS sensor via an ESP32.
I'm using the following library: https://github.com/emelianov/modbus-esp8266
I have the following code:
#include <ModbusRTU.h>
#include <SoftwareSerial.h>
SoftwareSerial modBusSerial;
ModbusRTU modbus;
#define startReg 100
#define endReg 123
uint16_t res[endReg - startReg + 1];
// Callback to monitor errors in the modbus
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) {
if (event != Modbus::EX_SUCCESS) {
Serial.print("Request result: 0x");
Serial.print(event, HEX);
}
return true;
}
void setup() {
Serial.begin(115200); // Default serial port (Hardware serial)
modBusSerial.begin(9600, SWSERIAL_8E1, MB_RX, MB_TX); // modbus configuration SWSERIAL_8E1 = 8 bits data, even parity and 1 stop-bit
modbus.begin(&modBusSerial);
modbus.master();
Serial.println("starting modbus...");
while (true) {
Serial.println(modBusSerial.read());
res[endReg - startReg] = 0; // string terminator to allow to use res as char*
if (!modbus.slave()) {
modbus.readIreg(16, startReg, res, endReg - startReg, cb);
}
modbus.task();
Serial.print("result: ");
Serial.println((char*) res);
delay(1000); // every second
}
}
Response I get;
When I do the exact same in QModMaster, I do get the correct output. Anyone any idea what I'm doing wrong here?
These are the settings I use;
I am aware of the "wrong" address in my code. I have 2 identical sensors but one is connected to my computer while the other one is connected to my ESP32.
Thanks in advance!
I am using STM32F103C8 board and CubeMX to create the code. I have connected the M66 to STM32 to UART2 port. I try to send some commands to Quectel M66 via STM32's UART port. It receives the command but throws some junk characters. I have set the baud rate as 9600 for all UART ports. This is my code
void M66_Check()
{
char *buffer = "ATI\r\n";
char *rec_buffer = NULL;
rec_buffer = (char*)malloc(200 * sizeof(char));
if(HAL_UART_Transmit(&huart2,buffer,strlen(buffer),200) == HAL_OK)
{
printf("AT Command sent successfully\r\n");
HAL_Delay(1000);
}
else
{
printf("Not Sent\r\n");
}
HAL_UART_Receive(&huart2,rec_buffer,50,200);
printf("About to print Response from M66 \r\n");
HAL_Delay(2000);
printf(rec_buffer);
}
This is what I am getting...Result in Putty
Any help would be greatly appreciated
There is the only way - read the answer and parse it. Then you will know if the command was executed ok.
But your response shows that your UART is running another speed (9600) than the modem. If you have not changed anything it should be 115200 as it is default modem UART speed
I have found out the problem... I needed to transmit and receive in these formats....
I missed those (uint8_t *).
To Transmit -
HAL_UART_Transmit(&huart2, (uint8_t *)buffer, strlen(buffer), 100);
To Receive -
HAL_UART_Receive(&huart2, (uint8_t *)rec_buffer,50,2000);
Now there is no problem and I am getting the response correctly.
Thanks for your help...
I've written two pieces of code, to create a serial communication between Arduino and a Raspberry Pi using C++. The codes are:
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 baud
}
void loop() {
Serial.println("Hello from arduino");
delay(500);
}
And the C++ code in Raspberry is :
#include <iostream>
#include <wiringPi.h>
#include <wiringSerial.h>
using namespace std ;
int serialDeviceId=0;
int main(void)
{
int pin=7;
serialDeviceId= serialOpen("/dev/ttyACM1",9600);
if(serialDeviceId==-1)
{
cout<<"Unable to open serial device"<<endl;
return 1;
}
if(wiringPiSetup()==-1)
{
return 0 ;
}
pinMode(pin,OUTPUT); // designing pin as an output
while(1)
{
digitalWrite(pin,0);
delay(500);
digitalWrite(pin,1);
delay(500);
}
return 0;
}
So now I would like to read the data from the serial port using always that wiringpi and I've found that I can use SerialGetchar, but I don't know exactly how to use it. I just need to need this part in my code so that I can receive that "Hello from from arduino" written in the serial, from my Arduino code.
This code should work for you on the Raspberry Pi. It is extremely simple! Make sure you get the correct device special file by running:
dmesg
and looking for line like this:
[610106.464745] usb 1-1.4: new full-speed USB device number 4 using dwc_otg
[610106.608642] usb 1-1.4: New USB device found, idVendor=2341, idProduct=0043
[610106.608655] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=220
[610106.608663] usb 1-1.4: Manufacturer: Arduino (www.arduino.cc)
[610106.608671] usb 1-1.4: SerialNumber: 55731323536351E002E1
[610106.686193] cdc_acm 1-1.4:1.0: ttyACM0: USB ACM device
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>
int main ()
{
int fd ;
if((fd=serialOpen("/dev/ttyACM0",9600))<0){
fprintf(stderr,"Unable to open serial device: %s\n",strerror(errno));
return 1;
}
for (;;){
putchar(serialGetchar(fd));
fflush(stdout);
}
}
I assume you have a USB cable connecting one of the RaspberryPi USB ports to the USB port on the Arduino.
I'm working on a code to communicate two arduinos, one with ethernet shield and another with an ENC28J60 ethernet module. I'm not a newbie in arduino neither an wise/expert yet. But i'm a complete -and less than a- newbie in UDP communication.
Here is the question: my code works fine, it sends and receives UDP packets from one to another and viceversa. But after every packet is sent, it increment in one the "Udp.remotePort" value (that viewing from the "udp-reader" side). It starts from 1024 up to ~32000 (and starts over after reach the highest value). I have researched about UDP and i understand that the first 0-1023 are reserved for specifics services p.e. 80 http, 21 ftp. But i think it should not be incremented after every send. Or it should?
I don't paste the code because as i said it works OK. I just would like to know what could be wrong from your experience.
The sentence i'm using to write the packets is:
udp.beginPacket(IPAddress([ip address]), [port no]);
The libraries i'm using:
UIPEthernet.h https://github.com/UIPEthernet/UIPEthernet for ENC28J60
Ethernet.h for ethernet shield
EDIT: This is the code of the UDP sender (ENC28J60). Basically is the example code of the library as i said it works correctly in terms of communication. I only changed the IPs: 192.168.1.50 which is the UDP sender and 192.168.1.51 which is the UDP destination.
#include <UIPEthernet.h>
EthernetUDP udp;
unsigned long next;
void setup() {
Serial.begin(115200);
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
Ethernet.begin(mac,IPAddress(192,168,1,51));
// Also i used: Ethernet.begin(mac,IPAddress(192,168,1,51), 5000);
// with the same result
next = millis()+2000;
}
void loop() {
int success;
int len = 0;
if (((signed long)(millis()-next))>0)
{
do
{
success = udp.beginPacket(IPAddress(192,168,1,50),5000);
Serial.print("beginPacket: ");
Serial.println(success ? "success" : "failed");
//beginPacket fails if remote ethaddr is unknown. In this case an
//arp-request is send out first and beginPacket succeeds as soon
//the arp-response is received.
}
while (!success && ((signed long)(millis()-next))<0);
if (!success )
goto stop;
success = udp.write("hello world&from&arduino");
Serial.print("bytes written: ");
Serial.println(success);
success = udp.endPacket();
Serial.print("endPacket: ");
Serial.println(success ? "success" : "failed");
do
{
//check for new udp-packet:
success = udp.parsePacket();
}
while (!success && ((signed long)(millis()-next))<0);
if (!success )
goto stop;
Serial.print("received: '");
do
{
int c = udp.read();
Serial.write(c);
len++;
}
while ((success = udp.available())>0);
Serial.print("', ");
Serial.print(len);
Serial.println(" bytes");
//finish reading this packet:
udp.flush();
stop:
udp.stop();
next = millis()+2000;
}
}
EDIT 2: This is a capture of testing with SocketTest listening on port 5000, and after a packet received, the next one arrives with the remote port incremented on 1 each time
You must be creating a new UDP socket per sent datagram. Don't do that. Use the same one for the life of the application.
I have a device that listen to UDP packets on port IN_PORT and echo the message on port OUT_PORT. I can communicate with it using a test software like Packet Sender.
I have to write a C++ library (Win32 at the moment) to communicate with the device. I made several tests but I still wasn't able to communicate. My guess is to use this workflow:
create socket
fill sockaddr_in structure with the device address, AF_INET family and the listeng port (OUT_PORT)
bind the socket
change sockaddr_in.sin_port with IN_PORT and send a packet (using sendto)
wait for an answer (using recvfrom)
repeat from 4
This works if I simulate the device with the Packet Sender utility working locally (device address = 127.0.0.1). I can't use the same workflow to connect to a remote address, even in the same subnet (e.g. my PC address: 192.168.1.2, remote PC address 192.168.1.5), since I get WSAEADDRNOTAVAIL error.
I've tested several different workflows, and read several discussions on the topic here and there, but none works, awfully.
Can someone give me some hints on the subject.
Thanks!
MIX
Your work flow is slightly wrong. It should be more like this instead:
create socket
fill sockaddr_in structure with the address of the local network adapter that is communicating with the device, AF_INET family, and the listenig port (OUT_PORT)
bind the socket
change sockaddr.sin_addr with device address, and sockaddr_in.sin_port with IN_PORT, and send a packet (using sendto)
wait for an answer (using recvfrom)
repeat from 4
I changed my code following Remy Lebeau hints. It works now. If someone will like to have a look and spot some weak points, or suggest improvements, I'll be glad (a code that "just works" is never enough; it must also "shine"!). Comments mark the previous (wrong) version of the code.
#pragma comment (lib, "Ws2_32.lib")
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <STRING>
#define IN_PORT 18
#define OUT_PORT 17
#define LOCAL_IP "10.0.10.108"
#define DEVICE_IP "10.0.10.104"
#define DEFAULT_BUFLEN 1024
int main(int argc, char *argv[])
{
WSADATA wsaData;
SOCKET sck;
struct sockaddr_in sckAddrInfo;
bool terminate;
char dataBuffer[DEFAULT_BUFLEN];
int rcvDataLength;
int sckAddrInfoLength;
WSAStartup(MAKEWORD(2,2), &wsaData);
sck = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
memset((&sckAddrInfo), 0, sizeof(sckAddrInfo));
sckAddrInfo.sin_family = AF_INET;
sckAddrInfo.sin_port = htons(IN_PORT);
//sckAddrInfo.sin_addr.s_addr = inet_addr(DEVICE_IP); // WRONG! Must bind local address
sckAddrInfo.sin_addr.s_addr = inet_addr(LOCAL_IP);
bind(sck, (struct sockaddr*)(&sckAddrInfo), sizeof(sckAddrInfo));
terminate = false;
sckAddrInfoLength = sizeof(sckAddrInfo);
while(!terminate)
{
printf("Write echo request: ");
gets(dataBuffer);
sckAddrInfo.sin_addr.s_addr = inet_addr(DEVICE_IP); // Must set device address, too, not just output port
sckAddrInfo.sin_port = htons(OUT_PORT);
sendto(sck, dataBuffer, strlen(dataBuffer), 0, (struct sockaddr*)(&sckAddrInfo), sizeof(sckAddrInfo));
memset(dataBuffer, '\0', DEFAULT_BUFLEN);
rcvDataLength = recvfrom(sck, dataBuffer, DEFAULT_BUFLEN, 0, (struct sockaddr*)(&sckAddrInfo), &sckAddrInfoLength);
printf("Device answer: %s\n", dataBuffer);
if(strcmp(dataBuffer, "quit") == 0)
terminate = true;
}
closesocket(sck);
WSACleanup();
return 0;
}