Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm developing a software to comunicate to a access control device. I need, after meet some requirements, to send the following command: "##$01W#0001$0D$0A" to rs232 port using SerialPort.Write.

I've tried to send the command via Hercules Utility, just to check if the command is ok, and it works fine.
But my code doesn't seem to work at all. What am i doing wrong?
C#
SerialPort.Write("##$01W#0001$0D$0A")
Posted
Updated 6-Sep-12 6:49am
v4
Comments
[no name] 6-Sep-12 12:44pm    
You might try converting your ASCII string to bytes. 0D and 0A are hex codes for carriage returns and line feeds so you are probably not sending your code in the correct format.
Ed Nutting 6-Sep-12 12:45pm    
Same thought at the same time aah... great minds think alike :)

Ed
(Or is it that fools seldom differ? ;p )
[no name] 6-Sep-12 12:48pm    
Given a choice, I prefer GMTA... :-)
Ed Nutting 6-Sep-12 12:44pm    
Have you got the correct Baud rate? That often causes problems. Also, check that the encoding the device uses for encoding/decoding the command is the same as the one you are using. Have you got a way of confirming that your access control device is working properly? Can you send it the command from anything else and get it to work? If so, perhaps inspect the command sent by said other program and compare it to your's?

Hope this helps,
Ed
Ganesh Nikam 7-Sep-12 23:50pm    
need more details

As mentioned by Wes Aday and Ed Nutting:
C#
string s = "##$01W#0001$0D$0A";
byte[] data = Encoding.ASCII.GetBytes(s);
serialPort.Write(data, 0, data.Length);
 
Share this answer
 
Please check this
$0D is special character and for this we need to be use "\r" (carriage return in C#
& for $0A is line feed "\r\n" need to be use from C# code..
 
Share this answer
 
Comments
CHill60 20-Feb-15 8:51am    
Wes Aday and Ed Nutting pointed this out over 2 years ago and Amund posted the solution over 2 years ago.

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