In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to realize automatic weighing by connecting the serial port of electronic scale in C#". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to connect the electronic scale serial port automatic weighing" bar!
I. Interface design
2. The core code private SerialPort com; private void FormCheck_Load (object sender, EventArgs e) {/ / initializes COMList initCOMList ();} # region private void initCOMList () {ManageCOM com = new ManageCOM (); List s = com.AutoGetCOM (); string [] comNames = s.ToArray () ComboxCOM.Items.Clear (); comboxCOM.Items.Add ("Please select COM"); comboxCOM.SelectedIndex = 0; foreach (string portName in comNames) {/ / add COM port to the interface drop-down box comboxCOM.Items.Add (portName) } # endregion private void btnStart_Click (object sender, EventArgs e) {getWeightfromCOM ();} # region private void getWeightfromCOM () {int selectIndex= comboxCOM.SelectedIndex; if (selectIndex items 0) {string COM = comboxCOM.Items [selectIndex] .ToString () Com = new SerialPort (COM); / / instantiate SerialPort and set COM port com.BaudRate = 9600; baud rate com.Parity = Parity.None;// No parity bit com.StopBits = StopBits.Two;// two stop bits com.Handshake = Handshake.RequestToSend / / Control protocol com.ReceivedBytesThreshold = 13bot / set the number of bytes in the internal input buffer before the DataReceived event occurs. Here I have 13 bytes as a set of com.Open (); / / Open serial port com.DataReceived + = new SerialDataReceivedEventHandler (Com_DataReceived) / / accept data thread} else {MessageBox.Show ("Please select COM") } / listen on serial data thread / private void Com_DataReceived (object sender, SerialDataReceivedEventArgs e) {Thread.Sleep (500) / / Thread hibernates for 500ms to receive all data from the serial port try {if (com.IsOpen) {byte [] readBuffer = new byte [com.ReadBufferSize + 1] Try {int count = com.Read (readBuffer, 0, com.ReadBufferSize); / / read serial data (listening) String SerialIn = System.Text.Encoding.ASCII.GetString (readBuffer, 0, count) / / Decode the byte array into the string if (count! = 0) {/ / emphasize here that UI cannot be assigned directly in the thread You can only use the delegated operation control this.BeginInvoke (new System.Threading.ThreadStart (delegate () {rbResult.Text = SerialIn) });} catch (TimeoutException) {}} else {TimeSpan waitTime = new TimeSpan (0,0,0,0,50); Thread.Sleep (waitTime) }} catch (Exception ex) {MessageBox.Show (ex.ToString ());}} # endregion here, I believe you have a deeper understanding of "how to connect the serial port of an electronic scale to weigh automatically". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.