Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Trying to use stx and etx controls to send command to serial device using the SimpleSerial code from csharp.simpleserial.com. When I do a loop back test, I receive the control command in full like I want (┐01P00104##└)but when I connect to the device I get no response. I've tested the command in PuTTy and HyperTerminal and they both worked fine.

P.S. When I attempt to use the "alt+02" and "alt+03" keys for stx and etx when writing in Notepad, I get ☻01P00204##♥ and hyperterminal and PuTTy accept it and work with the device even though they are not the characters I am familiar with such as the small ┐ for stx and └ for etx.

using System; 
2.using System.Collections.Generic; 
3.using System.ComponentModel; 
4.using System.Data; 
5.using System.Drawing; 
6.using System.Text; 
7.using System.Windows.Forms; 
8.using System.IO.Ports; 
9.using System.Threading; 
10. 
11.namespace SimpleSerial 
12.{
13.
14.     public partial class Form1 : Form 
15.     { 
16.         // Add this variable  
17.         string RxString; 
18.         string stx = "\u0002"; 
19.         string etx = "\u0003"; 
20.  
21.        
22.         public Form1() 
23.         { 
24.             InitializeComponent(); 
25.         } 
26. 
27.         private void buttonStart_Click(object sender, EventArgs e) 
28.         { 
29.             serialPort1.PortName = "COM3"; 
30.             serialPort1.BaudRate = 9600; 
31.             serialPort1.Parity = Parity.None; 
32.             serialPort1.DataBits = 8; 
33.             serialPort1.StopBits = StopBits.One; 
34.             serialPort1.Handshake = Handshake.None; 
35.                
36.             serialPort1.Open(); 
37.             if (serialPort1.IsOpen) 
38.             { 
39.                 buttonStart.Enabled = false; 
40.                 buttonStop.Enabled = true; 
41.                 textBox1.ReadOnly = false; 
42.             } 
43.         } 
44. 
45.         private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
46.         { 
47.             if (serialPort1.IsOpen) 
48.             { 
49.                 serialPort1.Write(stx + "01P00102##" + etx); 
50.             } 
51.         } 
52.
53.         private void buttonStop_Click(object sender, EventArgs e) 
54.         { 
55.             if (serialPort1.IsOpen) 
56.             { 
57.                 serialPort1.Close(); 
58.                 buttonStart.Enabled = true; 
59.                 buttonStop.Enabled = false; 
60.                 textBox1.ReadOnly = true; 
61.             } 
62. 
63.         } 
64. 
65.         private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
66.         { 
67.             if (serialPort1.IsOpen) serialPort1.Close(); 
68.         } 
69. 
70.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
71.         { 
72.             // If the port is closed, don't try to send a character. 
73.            if (!serialPort1.IsOpen) return; 
74.
75.             // If the port is Open, declare a char[] array with 1 element. 
76.             char[] buff = new char[1]; 
77.
78.             // Load element 0 with the key character. 
79.             buff[0] = e.KeyChar; 
80.
81.             // Send the 1 character buffer. 
82.             serialPort1.Write(buff, 0, 1); 
83.
84.             // Set the KeyPress event as handled so the character won't 
85.             // display locally. If you want it to display, omit the next line. 
86.             e.Handled = false; 
87.         } 
88.
89.         private void DisplayText(object sender, EventArgs e) 
90.         { 
91.             textBox1.AppendText(RxString); 
92.         } 
93.
94.         private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
95.         { 
96.             RxString = serialPort1.ReadExisting(); 
97.             this.Invoke(new EventHandler(DisplayText)); 
98.         } 
99.      } 
100.}
Posted

1 solution

Start by checking your comms settings: 9600 baud is likely to be right, but 8 bpc and no parity may be causing a problem, since STX and ETX would have opposite party bit values if the remote device is using parity - so one or the other would be rejected.
Look carefully at the settings you are using in hyperterminal and make sure they are all same.
It's also worth checking exactly what the other programme are sending it you can, as the C# code will be using Unicode which is not quite the same as ASCII which the device is likely to be expecting. Try setting up the TX data as a byte array and send that instead of using a string to avoid code set differences adding to your problems.

[edit] Tablet autocorrect corrected[/edit]
 
Share this answer
 
v2
Comments
Member 11780461 8-Jul-15 17:30pm    
Checked the setting and they are the same as the working terminal. Any chance you code show me how to make it a byte array?
OriginalGriff 9-Jul-15 4:33am    
private const byte stx = 0x02;
private const byte etx = 0x03;
private byte[] WrapString(string send)
{
int length = send.Length;
byte[] data = new byte[length + 2];
data[0] = stx;
data[length + 1] = etx;
Array.Copy(System.Text.Encoding.ASCII.GetBytes(send), 0, data, 1, length);
return data;
}

...
byte[] data = WrapString("01P00102##");
Member 11780461 9-Jul-15 10:43am    
I am placing this is in wrong?

private void linkLabel_HC1_100_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
private const byte stx = 0x02;
private const byte etx = 0x03;
private byte[]WrapString(string send)
{
int length = send.Length;
byte[] data = new byte[length + 2];
data[0] = stx;
data[length + 1] = etx;
Array.Copy(System.Text.Encoding.ASCII.GetBytes(send), 0, data, 1, length);
return data;
}
byte[] data = WrapString("01P00102##");
}
OriginalGriff 9-Jul-15 10:54am    
Yes!
The last bit is the bit which wraps your chosen data and converts it to a byte array ready for you send to the serial port.
And you can't put method declarations inside methods in C#!
Member 11780461 9-Jul-15 11:32am    
http://www.codeproject.com/Questions/1008236/Sending-ASCII-Controls-STX-and-ETX

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