Click here to Skip to main content
14,920,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.
I have to write an Hex. line and read the answer of the port (I am connecting to the main board of a TV, work stuff) the line is composed by this hex.

0xC0 0x63 7 0x66 0x00 0x01

But I have tried so many solutions (VB and C#) and I can not write to the serial port and as result I can not read also.

Does any on has a clue about this?
Here is the code that I'm currently using:

VB
Dim code As String
code = "C06307020001"
SerialPort1.Open()
SerialPort1.Write(code)

code = "C07307350000"
SerialPort1.WriteLine(code)

TextBox1.Text = Hex(SerialPort1.ReadByte())
Posted
Comments
Sascha Lefèvre 20-Apr-15 17:54pm
   
I assume you're not supposed to write it as a string but as bytes.
EddyGuzman 20-Apr-15 17:56pm
   
Hello.
I have tried to write as this: using (SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8))
{
byte[] bytesToSend = new byte[6] { 0XC0,0x63,7,0x02,0x00,0x01 };

port.Open();
port.Write(bytesToSend, 0, 6);

bytesToSend = new byte[6] { 0xC0, 0x63, 7, 0x66, 0x00, 0x01 };

port.Write(bytesToSend, 0, 6);
}

and didn't work...I've tried to make it with the serial port object and with one in code, but it's not working
Sergey Alexandrovich Kryukov 20-Apr-15 18:26pm
   
Byte (not-string) cannot be "hexadecimal". :-)
—SA
Sascha Lefèvre 20-Apr-15 18:06pm
   
"and with one in code" - what do you mean by that?

Do you have some sort of technical description that you could quote here?

It could be that you have to send the bytes in reverse or maybe pairs of two switched (like so: 0x63 0xC0 0x66 7 1 0)
EddyGuzman 20-Apr-15 18:20pm
   
I have to communicate with a device through serial port.
This device has to read two sequences of hexadecimal data:
One to open the factory mode: 0xC0 0x63 7 0x02 0X00
One to Get data: 0xC0 0x73 7 0x35 0x00 0x00
Sascha Lefèvre 20-Apr-15 18:40pm
   
BTW: Hexadecimal is not a property of data, it just means that it's represented in base-16.
Hex 1 = Decimal 1
Hex A = Decimal 10
Hex F = Decimal 15
Hex 10 = Decimal 16
Hex FF = Decimal 255
The prefix "0x" is just to indicate that it's a number in Hex.
Instead of initializing a byte like so:
byte b = 0x10;
you could as well write it like so:
byte b = 16;
Sascha Lefèvre 20-Apr-15 18:43pm
   
I can't help you much more without knowing more details about it. I just noticed though that you quoted different byte values in your comment and in your question. Maybe double-check that you're actually sending the right data. And maybe try my suggestion regarding the byte-order.

Umm, from the look of things the 'widget' might be getting confused
as you appear to be sending a string when a you want bytes.
Try the following:
C#
private  string[] Data = new string[6];
 
Data[0] = "0xC0"; 
Data[1] = "0x63" 
Data[2] = "0x?7";
Data[3] = "0x66";
Data[4] = "0x00";
Data[5] = "0x01";

Keeping the bytes as separate send them like
C#
for(int a = 0; a<=6; a++)
{ 
      SerialPort1.Write(Data[a]);
}
SerialPort1.Write("\r\n");

By sending out the commands as separate bytes ending with line feed & new line to terminate the command string...

C#
TextBox1.Text = ReadLine();

If the widget responds with a Line feed, New Line.

Just an idea! also have a look at
Serial Comms in C# for Beginners[^]
   
v3
First of all because you need a binary communication consider that you cannot use the string methods.
It seems that the problem is the Encoding that is set by default to ASCIIEncoding that sets to '0' the MSbit of each sent or received bytes. The solution is to set it to a codepage 28591 or 1252:
VB
SerialPort.Encoding = System.Text.Encoding.GetEncoding(28591)
'or
SerialPort.Encoding = System.Text.Encoding.GetEncoding(1252)

The same problem in C#.
For more details see[^].
To code hex values in VB the correct syntax is:
VB
Dim data As Byte() = {&HC0, &H63, &H07, &H66, &H00, &H01}

To send data use the Write method and to read use the Read or ReadByte methods. The first if you expect a known lenght stream of bytes or the second if you want to read a byte at time and do other controls (i.e. time-ot, EOT End Of Transmission code, etc).
   
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900