Get the length of the data received in a buffer with ReadExisting - vb.net

I need get the length of the received buffer in a serial port in VB. I am using serial1.ReadExisting(). The length of the data is variable and does not have a delimiter byte established is a reader rfid. The result of serial1.ReadExisting() should be an array. How can I put this into variable that changes length, or how get the length of of serial1.ReadExisting()?

Related

MPEG-TS pointer_field max value

What is the max value for the Pointer_filed (ISO/IEC 13818-1 2.4.4.1) in MPEG 2 standart?
I write my own library on C# for parsing ts files and found this:
As we can see here pointer_field for this table is 0xb5 bytes. EIT table header begin with 0x4E 0xF2 but end in another table and i can't get EIT section length for this table.
ps I get this EIT stream from Eutelsat 36B satellite.
It is an 8-bit field, so the max value would be 255.
Reading ISO/IEC 13818-1 2.4.4.1:
pointer_field – This is an 8-bit field whose value shall be the number
of bytes, immediately following the pointer_field until the first byte
of the first section that is present in the payload of the Transport
Stream packet (so a value of 0x00 in the pointer_field indicates that
the section starts immediately after the pointer_field). When at least
one section begins in a given Transport Stream packet, then the
payload_unit_start_indicator (refer to 2.4.3.2) shall be set to 1 and
the first byte of the payload of that Transport Stream packet shall
contain the pointer. When no section begins in a given Transport
Stream packet, then the payload_unit_start_indicator shall be set to 0
and no pointer shall be sent in the payload of that packet.
The rest of your EIT table is contained in the following packet.

UDP message size difference

Say that A sends B a UDP message of size N like
sockaddr_in to;
to.sin_family=AF_INET;
to.sin_port=htons(port);
to.sin_addr.s_addr=inet_addr(address);
sendto(sock,(const char*)buffer,N,0,(sockaddr*)&to,sizeof(to));
Now B receives this message expecting it to be of size N_1
sockaddr from;
socklen_t length_from=sizeof(from);
recvfrom(sock,(char*)buffer,N_1,0,&from,&length_from);
What happens when N_1!=N ?
What happens when N_1!=N ?
If the receive buffer is larger than the incoming datagram, the entire datagram is transferred into the buffer and the actual length is returned as the return value of recfvrom(). You're presently ignoring it. Don't do that.
If the receive buffer is smaller than the incoming datagram, it is truncated to fit into the receive buffer and he excess beyond that is discarded. The actual length of data transferred into the buffer is returned.

How can I convert a string representation of HEX into a byte for sending over serial in VB?

For homework I need to send 4 bytes to the device over a serial connection. My app has 4 text boxes for inputting the hexadecimal byte that needs to be sent. I want to be able to enter something like this:
0x01
0x81
0x82
0x81
into the 4 boxes. How do I take the string and convert it to a byte that can be sent over serial? I already sort of have it sending... something, but it's not:
byte(1) = Convert.toByte(textbox1)
Perhaps I'm inputting the hex into my text box incorrectly?
MSDN says for Convert.toByte(string value)
Parameters
value
Type: System.String
A string that contains the number to convert.
Thanks!

How do I limit BitConverter.GetBytes() to return only a certain amount of bytes using VB.NET?

I do:
Dim BytArr() as Byte = BitConverter.GetBytes(1234)
Since, by default, they are 32 bits, it returns 4 byte elements.
I want to be able to control it to return only like two bytes. Maybe only three bytes. Are there any built-in functions to control it?
I don't want to rely on using shifting >> 8 >> 16 >> 24 >> 32, etc..
I also don't want to rely on type casting the data in GetBytes() to a specific datatype.
It is not that GetBytes defaults to 32 bits, it is that GetBytes returns an array of the size required to hold the data type. If you pass a Long then you will get a 8 elements in your array.
The best way to control this is indeed casting the data you pass in. Otherwise you could truncate some of the number.
That being said, you could do something like this:
Dim BytArr() as Byte = Array.Resize(BitConverter.GetBytes(1234), 2)
But if the value you passed in exceeded what could be stored in 2 bytes (in this case) then you will have some very broken code.

How can I write a signed byte to a serial port in VB

I need to be able to write signed bytes to a serial port using
SerialPort.Write() method, except that method only takes byte[] arrays of unsigned bytes, how would i write a signed byte to the serial port?
For what I'm working on the particular command takes values from -1700 to 1700.
thanks
nightmares
The serial communication channel has no concept of signed or unsigned, only a concept of 1's and 0's on the wire. It is your operating system (and ultimately your CPU architecture) that assigns a numeric value to those 1's and 0's, on both the sending and receiving side.
The value range you state cannot be represented in a byte (per my comment and your reply). You need to understand what bit pattern the receiving device expects for a given number (is the other device big endian or little endian?), and then you can send an appropriate sequence of byte[] to represent the number you want to transmit.
If both devices have the same endianness, you can setup an array of short then copy to an array of byte like this:
short[] sdata = new short[] { 1, -1 };
byte[] bdata = new byte[sdata.Length * 2];
Buffer.BlockCopy(sdata, 0, bdata, 0, bdata.Length);
However, be sure and test for a range of values. Especially if you are dealing with embedded devices, numeric encoding may not be exactly as on an Intel PC.