Главный вопрос: How to display weight from weighing scale into a textbox via serial port RS-232 or usb converter?Как преобразовать Hex в ASCII на основе этого кода?
Теперь я пытаюсь получить значение hex и преобразовать его в ascii и отображение.
Основной код этого
public partial class MainForm : Form
{
private SerialPort _serialPort; // formda kullanilacak degisken
private const int BaudRate = 9600; // BaudRate Constant. default 9600 ile oynanabilir
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.MinimizeBox = false;
string[] portNames = SerialPort.GetPortNames(); // bütün kullanilabilecek com portlari okur
foreach (var portName in portNames)
{
comboBox1.Items.Add(portName); // Adds Ports to combobox
}
if (comboBox1.SelectedIndex != -1)
{
comboBox1.SelectedIndex = 0; // Selects first entry (convenience purposes)
}
}
private void button1_Click(object sender, EventArgs e)
{
// This block ensures that no exceptions happen
if (_serialPort != null && _serialPort.IsOpen)
_serialPort.Close();
if (_serialPort != null)
_serialPort.Dispose();
// End of Block
_serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One); //<-- Creates new SerialPort using the name selected in the combobox
_serialPort.DataReceived += SerialPortOnDataReceived; //<-- this event happens everytime when new data is received by the ComPort
_serialPort.Open(); //<-- make the comport listen
textBox1.Text = string.Format("Listening on {0}...", comboBox1.Text);
//here i am trying @Adam Casey 's code and serialReceived thing doesn't work.
byte[] serialReceived;
string reading = Encoding.UTF8.GetString(serialReceived);
textBox2.Text = reading.Substring(13);
}
private delegate void Closure();
private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
if (InvokeRequired) //<-- Makes sure the function is invoked to work properly in the UI-Thread
BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); })); //<-- Function invokes itself
else
{
while (_serialPort.BytesToRead > 0) //<-- repeats until the In-Buffer is empty
{
textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte()); //<-- bytewise adds inbuffer to textbox
}
}
}
Я слушаю com port 1 и получаю такие значения, как Прослушивание на COM1 ... 30 30 33 33 20 49 44 5F 30 30 3A 20 20 20 31 30 2E 36 20 6B 67 20 0D 0A 0D 0A. Я хочу получить их и изменить его на значение ascii и отобразить его на ярлыке. –
Извините, так вышло. когда я получаю вывод из устройства в многострочное текстовое поле. моя программа замерзает через 1 секунду. вот результат; 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 и изображение: http: //s32.postimg.org/mke80rwtx/IMG_20160515_143350.jpg –
Я didn Так интегрируйте масштаб таким образом, но я могу предложить вам проверить параметры RS232. Скорость передачи, частичная, контроль потока и т. Д. Является ли это кассовой шкалой? –