I have a small program to receive UPD Packets. I have some bit problem in receiving packets . I actually need to revce packets by id for example "position (packet id: 196) , Location :187 etc" With my code i get some data but i dont know how to find the id and how to get real data inside the packet.
Packet Header
|SYNC Byte|SYNC Byte|Data Length|Identifier|Data Bytes|Checkssum|
|1(0x24) |2(0x40) |Byte |Bye | |Byete |
Field :Sync Bytes SIZE: 2 Bytes
Field :Data Length SIZE: 1 Byte
Field :Packet Identifier SIZE: 1 byte
Field :Data SIZE: 0-255 bytes
Field :Checksum SIZE: 1 byte
enter code here
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace SimpleUdpReciever
{
class pProgram
{
static void Main(string[] args)
{
int localPort = 18006;
IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);
bool flag = false;
for (int i = 0; i < args.Length; i++)
{
string cmd = args[i];
string value;
int tempInt;
IPAddress tempAddress;
switch (cmd)
{
case "-lp":
value = GetValue(args, ref i);
if (int.TryParse(value, out tempInt))
localPort = tempInt;
break;
case "-rp":
value = GetValue(args, ref i);
if (int.TryParse(value, out tempInt))
remoteSender.Port = tempInt;
break;
case "-rh":
value = GetValue(args, ref i);
if (IPAddress.TryParse(value, out tempAddress))
remoteSender.Address = tempAddress;
else if (int.TryParse(value, out tempInt) && tempInt == 0)
remoteSender.Address = IPAddress.Any;
break;
case "-?":
default:
PrintHelpText();
flag = true;
break;
}
}
// Exit application after help text is displayed
if (flag)
return;
// Create UDP client
UdpClient client = new UdpClient(localPort);
UdpState state = new UdpState(client, remoteSender);
// Start async receiving
client.BeginReceive(new AsyncCallback(DataReceived), state);
// Wait for any key to terminate application
Console.ReadKey();
client.Close();
}
private static void DataReceived(IAsyncResult ar)
{
UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);
bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address)) || wantedIpEndPoint.Address.Equals(IPAddress.Any);
bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port) || wantedIpEndPoint.Port == 0;
if (isRightHost && isRightPort)
{ string test5 = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("test5 = {0}", test5);
}
// Restart listening for udp data packages
c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
}
private static string GetValue(string[] args, ref int i)
{
string value = String.Empty;
if (args.Length >= i + 2)
{
i++;
value = args[i];
}
return value;
}
private static void PrintHelpText()
{
Console.WriteLine("Simple Udp Receiver is an application that prints incoming data to screen.");
Console.WriteLine("Data is converted to ASCII before printing.");[![enter image description here][1]][1]
Console.WriteLine("Command switches:");
Console.WriteLine("-? : Displays this text.");
Console.WriteLine("-lp : Set local receiving port. \"-lp 4001\" Default: 11000");
Console.WriteLine("-rp : Set remote sender port. \"-rp 4001\" Default: 0 (Any port)");
Console.WriteLine("-rh : Set remote sender ip. \"-rh 192.168.1.10\" Default: 0 (Any ip)");
Console.WriteLine("\n Example of usage:\nSimpleUdpReciver.exe -lp 11000 -rh 192.168.10.10 -rp 4001");
}
}
}
Out put Looks like this whiole running this program : $# za� �X
�� �
I am receiving data (in byte) from serial port, and now I want to write a if else in the data receive handler.
If(condition)
{}
else
{}
condition for if is: number of bytes received / 8 = interger.
For example, I received 16 bytes of data, so 16/2=8, is an integer. I received 20 bytes of data, 20/8=2.5, it s not an integer.
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
byte[] b = new byte[800];
int Received;
SerialPort sp = (SerialPort)sender;
if (condition)
{
MessageBox.Show(" Transmission error!");
}
else
{
Received = sp.Read(b,0,8);
if (Received < 8)
return;
float f11 = BitConverter.ToSingle(b, 0);
float f22 = BitConverter.ToSingle(b, 4);
Invoke(new Action(() =>
{
textBox3.Text += f11.ToString() + " "+f22.ToString()+"\r\n";
//MessageBox.Show(" New Message Received!");
}));
Received = 0;
}
}
You can use % operator:
if (number_of_bytes_received % 8 == 0)
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 want to call an c++ exe file into my c# application that takes a command line argument and returns the result so that i can use it in my c# application but i don't know how to do it .
here's the simple sample that i tried and failed :
c++ code : returner.exe
#include<iostream>
#include<cstdlib>
using namespace std;
int main(string argc , string argv)
{
int b= atoi(argv.c_str());
return b;
}
c# code :
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo stf = new ProcessStartInfo("returner.exe", "3");
stf.RedirectStandardOutput = true;
stf.UseShellExecute = false;
stf.CreateNoWindow = true;
using (Process p = Process.Start(stf))
{
p.WaitForExit();
int a = p.ExitCode;
label1.Text = a.ToString();
}
}
i expect to see 3 in the lable . but it's always 0 . what should i do ?
The signature of your main is incorrect, it should be:
int main(int argc, char *argv[])
{
// you are better to verify that argc == 2, otherwise it's UB.
int b= atoi(argv[1]);
return b;
}
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/