I have been frustrated for days trying to make it work. What I have found relevant doesn't seem to help either. I'm a bit new here and with this, so please be kind. I want to be able to send a message or date from my NodeMCU acting as a client to my C# console application acting as a server through a wifi network using UDP. But it doesn't seem to be working at all.
Here is my C# server:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDP_Server
{
class Program
{
private const int listenPort = 12346;
private static void StartListener()
{
bool done = false;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
try
{
while (!done)
{
Console.WriteLine("Waiting for broadcast");
byte[] bytes = listener.Receive(ref groupEP);
Console.WriteLine("Received broadcast from {0} :\n {1}\n", groupEP.ToString(), Encoding.ASCII.GetString(bytes, 0, bytes.Length));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
}
}
public static int Main()
{
StartListener();
return 0;
}
}
}
And here is my NodeMCU client:
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
// WIFI
const char* ssid = "****"; // your network SSID (name)
const char* pass = "****"; // your network password
//testing with election server
const char* serverIP="192.168.254.111"; //IPV4 addresss?
unsigned int serverPort = 12346;
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
// NodeMCU pin mapping
//const uint8_t D0 = 16;
//const uint8_t D1 = 5;
//const uint8_t D2 = 4;
//const uint8_t D3 = 0;
//const uint8_t D4 = 2;
//const uint8_t D5 = 14;
//const uint8_t D6 = 12;
//const uint8_t D7 = 13;
//const uint8_t D8 = 15;
//const uint8_t D9 = 3;
//const uint8_t D10 = 1;
void setup(){
Serial.begin(115200);
Serial.println("starting");
start_wifi();
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void start_wifi() {
// setting up Station AP
WiFi.begin(ssid, pass);
// Wait for connect to AP
Serial.print("[Connecting]");
Serial.print(ssid);
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30){
break;
}
}
Serial.println();
printWifiStatus();
Serial.println("Connected to wifi");
Serial.print("Udp server started at port ");
Serial.println(localPort);
Udp.begin(localPort);
}
void loop() {
Udp.beginPacket(serverIP, serverPort);
Udp.write("Test Message");
delay(500);
Serial.println("OK");
}
I want my client to be able to send messages, received by my C# program. Thank you so much.
I forgot to put Udp.endPacket(); in the loop statement of my client.
In my app I am sending data to microcontroller. I send data, microcontroller do program, and send character ("K"). My application should wait for this character.After receiving this char, it should send data again.
I got problem with receiving this character. Is function BytesToRead right to reading character? My program always fall when it reach this my function wait
static void wait()
{
SerialPort COMport = new SerialPort();
int znak;
COMport.PortName = "COM6"; //
COMport.BaudRate = 1200;
COMport.DataBits = 8;
COMport.Parity = Parity.None;
COMport.StopBits = StopBits.One;
COMport.Open();
do
{
znak = COMport.BytesToRead;
} while (znak != 75); // ASCII K = 75
COMport.Close();
return;
}
The BytesToRead property of the COMport returns the number of characters that have been received by the COMport so your loop will continue until there are exactly 75 characters read. Take a look at the documentation for the SerialPort class. It will show you a good example of how to read/write characters from/to your COMport.
Why not just use while(COMport.ReadChar() != 'K') { /* Do Stuff */ }?
I have this code but I don't know how to get the data and put it in one variable :
protected override void OnStart(string[] args)
{
/* This WaitHandle will allow us to shutdown the thread when
the OnStop method is called. */
_shutdownEvent = new ManualResetEvent(false);
/* Create the thread. Note that it will do its work in the
appropriately named DoWork method below. */
_thread = new Thread(DoWork);
/* Start the thread. */
_thread.Start();
}
and then in the DoWork I have the following :
private void DoWork()
{
//opening serial port
SerialPort objSerialPort;
objSerialPort = new SerialPort();
objSerialPort.PortName = "COM2";
objSerialPort.BaudRate = 11500;
objSerialPort.Parity = Parity.None;
objSerialPort.DataBits = 16;
objSerialPort.StopBits = StopBits.One;
objSerialPort.Open();
So, I open the port but where to start getting the data ??? How to initialize the variable ? The received message will be of the form 52 45 41 44 45 52 30 31 where 41 44 45 53 30 is the message in hexadecimal while 52 45 is the header and 31 CRC.
Please let me know how to do it.
Thank you ....
Working with serial port is just like working with files or sockets:
while ((bytesRead = objSerialPort.Read(buffer, 0, buffer.Length)) > 0)
{
var checksum = buffer[bytesRead - 1];
if (VerifyChecksum(checksum, buffer, bytesRead)) // Check the checksum
{
DoSomethinWithData(buffer, bytesRead); // Do something with this bytes
}
}
byte[] buffer = new byte[1];
String message = "";
While (true)
{
if(objSerialPort.Read(buffer,0,1)>0)
{
message+= System.Text.Encoding.UTF8.GetChars(buffer).ToString();
//Or you could call another function here that will DoSomething with each byte coming in!
}
}
Should do the trick!
This is the 1st time am posting my query. I am in need of help. Any help is appreciated.
As i agree that i have given my prob as long story. But am sorry i am not getting how to make it short and my intention is give complete information regarding my prob.
Problem :
I have to communicate between two laptops using USB-to-Serial adapter on windows platform. I have written 2 programs one for sending and another for receiving. Programs were written in both C and C# programming languages.
Using C language :
I am able to successfully communicate using C-Programs mentioned below. But the problem is speed. It takes around 1 hour(60min) for just to pass 150MB. Anyone plz help me in improving the performance of this programs or you may suggest me other approaches which is robust and give high performance. I also mention some comments along with programs for self understanding.
Sender File on laptop with serial port :
#include <stdio.h>
#include <bios.h>
#include <conio.h>
#define COM1 0
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0
#define SETTINGS ( 0xE0 | 0x03 | 0x00 | 0x00)
int main(void)
{
int in, out, status, DONE = FALSE,i=0;
char c;
FILE *fp,*fp1;
unsigned long count = 0,shiftcount = 0;
clrscr();
fp = fopen("C:/TC/pic.jpg","rb"); //opened for reading actual content
fp1 = fopen("C:/TC/pic.jpg","rb"); //opened for reading the size of file
fseek(fp1,0L,2);
count = ftell(fp1) + 1; // file size
bioscom(0, SETTINGS, COM1); // initializing the port
printf("No. of Characters = %lu\n",count);
// since bioscom function can send or receive only 8bits at a time, am sending file size in
4 rounds so that we can send at max of 4GB file.
bioscom(1,count,COM1); // sneding 1st lower 8bits
bioscom(1,count>>8,COM1); // sending 2nd set of lower 8bits
bioscom(1,count>>16,COM1); // sending 3rd set of lower 8bits
bioscom(1,count>>24,COM1); // sending upper 8 bits
cprintf("... BIOSCOM [ESC] to exit ...\n");
while (!DONE)
{
status = bioscom(3, 0, COM1);// get the status of port
//printf("%d",status);
if (status & DATA_READY) //checks if data is ready
{
out = bioscom(2, 0, COM1); // receives the ack
if(!feof(fp))
{
c = fgetc(fp); //read character by character from file
bioscom(1,c,COM1);//send character to receiver
putch(c);//display
}
}
//to interrupt
if (kbhit())
{
if ((in = getch()) == '\x1B')
DONE = TRUE;
}
}
fclose(fp);
return 0;
}
Receiving file on laptop with USB port :
#include <stdio.h>
#include <bios.h>
#include <conio.h>
#define COM4 3
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0
#define SETTINGS ( 0xE0 | 0x03 | 0x00 | 0x00)
int main(void)
{
int in, out, status;
char c;
FILE *fp;
unsigned long shiftcount1=0,shiftcount2=0,shiftcount3=0,shiftcount4=0;
unsigned long count = 0, DONE = 1;
clrscr();
fp = fopen("C:/TC/pic1.jpg","wb");// file opened for writing
bioscom(0, SETTINGS, COM4);//initialize tyhe port
cprintf("... BIOSCOM [ESC] to exit ...\n");
//receives all the 32 bits of file size sent from sender
shiftcount1 = bioscom(2,0,COM4);
shiftcount2 = bioscom(2,0,COM4);
shiftcount3 = bioscom(2,0,COM4);
shiftcount4 = bioscom(2,0,COM4);
//send an ack
bioscom(1,'x',COM4);
count = shiftcount1 | (shiftcount2<<8) | (shiftcount3<<16) | (shiftcount4<<24);
printf("shift4 = %lu\tshift3 = %lu\tshift2 = %lu\tshift1 = %lu\n",shiftcount4,shiftcount3,shiftcount2,shiftcount1);
printf("File Size = %lu\n",count);
//loop till the size of the file
while (DONE < count)
{
status = bioscom(3, 0, COM4);// check the status
// printf("%d",status);
if (status & DATA_READY)//check for data ready at the port
{
out = bioscom(2, 0, COM4);//receive the data
DONE++;
fputc(out,fp);
putch(out);
bioscom(1,'x',COM4);//send an ack
}
if (kbhit())
{
if ((in = getch()) == '\x1B')
break;
}
}
fclose(fp);
return 0;
}
Sender file on laptop with USB port:
#include <stdio.h>
#include <bios.h>
#include <conio.h>
#include<dos.h>
#include<stdlib.h>
#include<time.h>
#define RTS 0x02
#define COM1 0
#define COM4 3
#define CURRCOM COM4
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0
#define SETTINGS ( 0xE0 | 0x03 | 0x00 | 0x00)
int main(void)
{
int in, out, status, DONE = FALSE,nextfile = 1;
char c;
FILE *fp,*fp1;
unsigned long count = 0,shiftcount = 0;
clock_t start,end;
start = clock();
clrscr();
fp = fopen("C:/TC/pic.jpg","rb");
fp1 = fopen("C:/TC/pic.jpg","rb");
fseek(fp1,0L,2);
count = ftell(fp1) + 1;
bioscom(0, SETTINGS, CURRCOM);
/* while(!feof(fp1))
{
c = fgetc(fp1);
count++;
} */
printf("No. of Cheracters = %lu\n",count);
bioscom(1,count,CURRCOM);
bioscom(1,count>>8,CURRCOM);
bioscom(1,count>>16,CURRCOM);
bioscom(1,count>>24,CURRCOM);
cprintf("\n... BIOSCOM [ESC] to exit ...\n");
while (!DONE)
{
status = bioscom(3, 0, CURRCOM);
if (status & DATA_READY)
{
out = bioscom(2,0,CURRCOM);
if(!feof(fp))
{
c = fgetc(fp);
bioscom(1,c,CURRCOM);
putch(c);
}
}
if (kbhit())
{
if ((in = getch()) == '\x1B')
DONE = TRUE;
}
}
fclose(fp);
end = clock();
printf("\nTotal time = %d\n",(end - start)/CLK_TCK);
return 0;
}
Receiver file on laptop with serial port :
#include <stdio.h>
#include <bios.h>
#include <conio.h>
#include<time.h>
#define COM1 0
#define DATA_READY 0x100
#define TRUE 1
#define FALSE 0
#define SETTINGS ( 0xE0 | 0x03 | 0x00 | 0x00)
int main(void)
{
int in, out, status;
char c;
FILE *fp;
int y = 0,esc;
unsigned long count=0,shiftcount1 = 0,shiftcount2 = 0,shiftcount3 = 0,shiftcount4 = 0, DONE = 1;
clock_t start,end;
start = clock();
clrscr();
fp = fopen("C:/TC/pic1.jpg","wb");
bioscom(0, SETTINGS, COM1);
cprintf("... BIOSCOM [ESC] to exit ...\n");
shiftcount1 = bioscom(2,0,COM1);
shiftcount2 = bioscom(2,0,COM1);
shiftcount3 = bioscom(2,0,COM1);
shiftcount4 = bioscom(2,0,COM1);
bioscom(1,'x',COM1);
count = shiftcount1 | (shiftcount2<<8) | (shiftcount3<<16) | (shiftcount4<<24);
printf("shift4 = %lu\tshift3 = %lu\tshift2 = %lu\t shift1 = %lu\n",shiftcount4,shiftcount3,shiftcount2,shiftcount1);
printf("file size = %lu\n",count);
while (DONE < count)
{
status = bioscom(3, 0, COM1);
//printf("%d",status);
if (status & DATA_READY)
{
out = bioscom(2, 0, COM1);
DONE++;
fputc(out,fp);
putch(out);
bioscom(1,'x',COM1);
}
if (kbhit())
{
if ((in = getch()) == '\x1B')
break;
}
}
fclose(fp);
end = clock();
printf("\nTotal time = %f\n",(end - start)/CLK_TCK);
return 0;
}
The above 4 programs behaves as, sender send a character and receives an ack for every character. I have followed this approach, bcoz other approaches were not working fine (in the sense the complete data is not sent, the amount of data sent is not judgeable, bcoz it will different every tym). when i used this approach it worked fine.
Using C# language :
Below two programs are written in C# using visual studio. I have used SerilaPort class , its properties and methods for communication. Using this, am able to communicate text and xml files on both the sides successfully.Also image files with .jpg extention, can be transferred from USB to serial end withot any loss of data(successful transmission), but if i transfer from serial to usb end, am able receive image with some data loss, even with the data loss am able to see the image.
Sender file on laptop with serial port :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Threading;
namespace Communication
{
class Program
{
static void Main(string[] args)
{
string name;
string message;
StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
//Thread readThread = new Thread(Read);
FileStream fs = new FileStream("C:/text.xml", FileMode.Open);
//StreamReader sr = new StreamReader(fs);
BinaryReader br = new BinaryReader(fs);
// Create a new SerialPort object with default settings.
SerialPort _serialPort = new SerialPort();
// Allow the user to set the appropriate properties.
_serialPort.PortName = "COM1";
_serialPort.BaudRate = 115200;
_serialPort.Parity = Parity.None;
_serialPort.DataBits = 8;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.None;
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
bool _continue = true;
//readThread.Start();
int len = (int)fs.Length;
char[] data = new char[len+1];
br.Read(data, 0, len);
for (int i = 0; i < len+1; i++)
{
_serialPort.Write(data, i, 1);
//Console.Write(data,i,1);
}
br.Close();
fs.Close();
_serialPort.Close();
}
}
}
Receiver file on laptop with USB port :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Threading;
using System.Collections;
namespace Communication
{
class Program
{
static void Main(string[] args)
{
SerialComm comm = new SerialComm();
comm.Init();
comm.ReadSerial();
comm.WriteToFile();
comm.ResClose();
Console.ReadKey();
}
}
class SerialComm
{
FileStream fs = null;
BinaryWriter file = null;
ArrayList al = null;
public Boolean Init()
{
if (fs == null)
{
fs = new FileStream("C:/text1.txt", FileMode.OpenOrCreate);
}
if (file == null)
{
file = new BinaryWriter(fs);
}
if (al == null)
{
al = new ArrayList();
}
return true;
}
public void ResClose()
{
file.Close();
fs.Close();
}
public Boolean ReadSerial()
{
SerialPort port;
StreamWriter sw;
ConsoleKeyInfo ck;
port = new SerialPort();
port.PortName = "COM4";
port.BaudRate = 115200;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Handshake = Handshake.None;
port.Open();
port.BaseStream.Flush();
port.DiscardInBuffer();
int c = 1;
while (c != 0)
{
c = port.ReadByte();
al.Add((byte)c);
}
return true;
}
public void WriteToFile()
{
int i = 0;
byte[] message = al.ToArray(typeof(byte)) as byte[];
file.Write(message, 0, message.Length - 1);
}
}
}
Please help me.
Thanks in advance.
Transmission speed:
Serial ports are simple, not fast. I am assuming that you are using 230kbps which is already more than many hardware can handle. Compression is only thing that might help, but compressing already compressed data (like .mp3) won't help much.
Data loss:
Serial ports are simple, not robust. Data loss is common, and only thing you can do about it is to have protocol to detect errors on incoming frames, and have ability to retry send if there is an error.
Conclusion:
Use TCP/IP instead.
Way too long a question. I've found only one actual question, and that's the performance bit.
Just use Ethernet or WiFi. "Serial port" (you probably mean RS-232) speeds are low. 0.1 Mbit/second is considered fast by RS-232 standards. You clock 1200 Mbit/3600 seconds, which is 0.3 Mbit/second. That is ultra-fast. I'm in fact surprised that you achieve that, your C# program is explicitly setting the speed to 0.1 Mbit/second.
I am trying to write an app in C to communication with a device through a COM port.
I am able to send data using Docklight (hyper terminal like program) with the following settings and I can verify that I am receiving it through Itronix LogicPort Logic Analyzer.
Baud rate:9600
Parity:None
Byte Size:8
Stop bits:1
I am having 2 problems with my C program:
1) sometimes CreateFile returns INVALID_HANDLE_VALUE
and the other problem I can't seem to get by is that
GetCommState()
SetCommState()
SetCommTimeouts()
always return ERROR_INVALID_PARAMETER 87 (0x57)
Since I couldn't get past this problem, I tried a program in C# and that works just fine, but my program must be in C to communicate with the rest of my code. This program also communicates with other programs in C through sockets.
in Device Manager the device I want to talk to is listed as COM6 under Ports(COM & LPT)
any help would be appreciated!
Below is the code for both programs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
byte[] TxBuffer = new byte[20];
SerialPort CommPort = new SerialPort("COM6", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CommPort.Open();
TxBuffer[0] = (byte)'|';
TxBuffer[1] = 0;
TxBuffer[2] = 0;
TxBuffer[3] = 0;
TxBuffer[4] = 1;
TxBuffer[5] = 100;
TxBuffer[6] = 1;
TxBuffer[7] = 6;
TxBuffer[8] = (byte)'|';
TxBuffer[9] = 1;
}
private void button1_Click(object sender, EventArgs e)
{
CommPort.Write(TxBuffer, 0, 10);
}
}
}
and the C code is:
somewhere
struct serial_dev {
HANDLE hSerial;
};
struct serial_dev sdev; //global
//somewhere in main
if (init_serial(&sdev, com_file) == -1)
return -1;
write_ser(&sdev, buffer, buf_size);
close_serial(&sdev);
// in serial.c
int init_serial(struct serial_dev *sdev, char *port_name)
{
DCB dcbSerialParams;
COMMTIMEOUTS timeouts;
int error = 0;
memset(&dcbSerialParams, 0, sizeof(dcbSerialParams));
memset(&timeouts, 0, sizeof(timeouts));
sdev->hSerial = CreateFile("COM6", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (sdev->hSerial == INVALID_HANDLE_VALUE)
{
error = GetLastError();
close_serial(sdev);
fprintf(stderr, "error: could not open serial port (error:%d)\n", error);
return -1;
}
memset(&dcbSerialParams, 0, sizeof(dcbSerialParams));
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(sdev->hSerial, &dcbSerialParams))
{
close_serial(sdev);
error = GetLastError();
fprintf(stderr, "error: could not get serial port state (%d)\n", error);
return -1;
}
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(sdev->hSerial, &dcbSerialParams))
{
close_serial(sdev);
error = GetLastError();
fprintf(stderr, "error: could not set serial port state (%d)\n", error);
return -1;
}
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 200;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(sdev->hSerial, &timeouts))
{
close_serial(sdev);
error = GetLastError();
fprintf(stderr, "error: could not set serial port timeouts (%d)\n", error);
return -1;
}
return 0;
}
void close_serial(struct serial_dev *sdev)
{
CloseHandle(sdev->hSerial);
}
int write_ser(struct serial_dev *sdev, void *buf, size_t len)
{
DWORD written;
BOOL success;
success = WriteFile(sdev->hSerial, buf, len, &written, NULL);
if(!success || written == 0)
{
printf("error writing to serial\n");
return -1;
}
printf("writing to serial OK!\n");
return written;
}
guys I figured it out... I removed the "UNICODE" and "_UNICODE" preprocessor macros from my project settings and set it to "Not Set" found answer here: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/1a77c595-10a5-4f14-8512-c67535c53a08/