需要CAS CL5200规模的帮助。 我已经看过关于这个问题的所有帖子,但没有一个是关于CAS CL5200与USB电缆连接的缩放连接创建一个虚拟COM端口(COM41)
这是我遵循的步骤 -
代码:
public partial class Form1 : Form
{
private const int BaudRate = 19200; // BaudRate Constant
private SerialPort _serialPort;
private void button1_Click_1(object sender, EventArgs e)
{
if (_serialPort != null && _serialPort.IsOpen)
_serialPort.Close();
if (_serialPort != null)
_serialPort.Dispose();
_serialPort = new SerialPort("COM41", BaudRate, Parity.None, 8, StopBits.One); // Creates new SerialPort using the name selected in the combobox
_serialPort.Handshake = Handshake.None;
// _serialPort.DataReceived += SerialPortOnDataReceived; // this event happens everytime when new data is received by the ComPort
_serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
_serialPort.RtsEnable = true;
_serialPort.DtrEnable = true;
_serialPort.Open();
textBox1.Text = "Listening on " + _serialPort.PortName + "...\r\n";
// byte[] towrite = GetBytes("W"); // get the weight from scale wiyh "W" as byte[]
// _serialPort.Write(towrite,0,1);
_serialPort.WriteLine("W");
}
private delegate void Closure();
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (InvokeRequired) // makes sure the function is invoked to work properly in the UI-Thread
BeginInvoke(new Closure(() => { _serialPort_DataReceived(sender, e); })); //<-- Function invokes itself
else
while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
{
//textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte());
textBox1.Text += _serialPort.ReadExisting();
//<-- bytewise adds inbuffer to textbox
}
}
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
}