前陣子都是寫單純的rs232或rs485的資料讀取(丟個指令,然後收資料)
,或I/O控制,
modbus的通訊,一年多前寫過,然後最近測的時候,完全忘光。
最後終於弄出來,紀錄一下…方便抄寫 記憶
基本的序列埠開啟關閉就不說了,
如果有不清楚的請自行google。
,或I/O控制,
modbus的通訊,一年多前寫過,然後最近測的時候,完全忘光。
最後終於弄出來,紀錄一下…方便
基本的序列埠開啟關閉就不說了,
如果有不清楚的請自行google。
vb.net
Function Dec3(n As String)
Dim num As UInt32 = UInt32.Parse(n, System.Globalization.NumberStyles.AllowHexSpecifier)
Dim floatVals As Byte() = BitConverter.GetBytes(num)
Dim f As Single = BitConverter.ToSingle(floatVals, 0)
Return f
End Function
String hexString = "43480170";
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
Byte[] floatVals = BitConverter.GetBytes(num);
float f = BitConverter.ToSingle(floatVals, 0);
Console.WriteLine("float convert = {0}", f);
e.Graphics.MeasureString(title,New Font("Arial", 24.0F, FontStyle.Bold), e.MarginBounds.Width).Width dim ypos as integer = New Font("Arial", 16.0F, FontStyle.Underline).GetHeight Dim LinePen As New Pen(Color.Black, 1)
Dim lineDash As Single() = {10, 2} '實線長度、虛線長度(loop)
LinePen.DashStyle = Drawing2D.DashStyle.Custom
LinePen.DashPattern = lineDash
e.Graphics.DrawLine(LinePen, New Point(e.MarginBounds.Left, yPos), New PointF(e.MarginBounds.Right, yPos)) e.Graphics.DrawString(String.Format("Cpk製程能力總和指數:{0}\t{1}", txtCpk.Text, txtCpkLv.Text).Replace("\t", vbTab), New Font(dg.Font, FontStyle.Bold), Brushes.Black,e.MarginBounds.Left + 261, yPos) string.format("{1}{0}{2}{0}",a,vbTab,2,vbTab)For Each GridCol As DataGridViewColumn In dg.Columns
e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), New Rectangle(CInt(arrColumnLeft(iCount)),
iTopMargin, CInt(arrColumnWidth(iCount)), iHeaderHeight)) e.Graphics.DrawRectangle(Pens.Black, New Rectangle(CInt(arrColumnLeft(iCount)), iTopMargin,
CInt(arrColumnWidth(iCount)), iHeaderHeight)) e.Graphics.DrawString(GridCol.HeaderText, GridCol.InheritedStyle.Font,
New SolidBrush(GridCol.InheritedStyle.ForeColor),
New RectangleF(CInt(arrColumnLeft(iCount)), iTopMargin,
CInt(arrColumnWidth(iCount)), iHeaderHeight), strFormat)
iCount += 1
Next'將chart轉成圖檔
dataChart.DrawToBitmap(bmp, New Rectangle(0, 0, dataChart.Width, dataChart.Height))
e.Graphics.DrawImage(DirectCast(dest_bmp, Image), e.PageBounds.Left + 20, yPos)
dataChart.Series("Series1").Points.Clear()
dataChart.ChartAreas.Clear()
dataChart.ChartAreas.Add("ChartArea1")
Dim arrWT As Decimal() = CommonHelp.GetTypeStd(txtStd.Text)
Dim intWT_std = arrWT(0), intWT_T = arrWT(1)
Dim ChtLSL = intWT_std - intWT_T, ChtTGT = intWT_std, ChtUSL = intWT_std + intWT_T
Dim chartMin = chartRange(intWT_T)(0), chartMax = chartRange(intWT_T)(1)
'設定MainCht
With dataChart
.Series("Series1").MarkerStyle = MarkerStyle.Circle
.ChartAreas(0).AxisX.MajorGrid.Enabled = False
.ChartAreas(0).AxisY.MajorGrid.Enabled = False
.ChartAreas(0).AxisX.MajorTickMark.Enabled = False
.ChartAreas(0).AxisY.MajorTickMark.Enabled = False
.ChartAreas(0).AxisX.LabelStyle.ForeColor = Color.Black
.ChartAreas(0).InnerPlotPosition.Auto = True
.Legends.Clear()
'最大、最小
.ChartAreas(0).AxisY.Minimum = ChtLSL - 20
.ChartAreas(0).AxisY.Maximum = ChtUSL + 20
'把圖表駔標的左右兩邊空白去除
.ChartAreas(0).AxisX.IsMarginVisible = True
.ChartAreas(0).AxisY.IsMarginVisible = True
'X軸最大的資料點總數
.ChartAreas(0).AxisX.Maximum = 100
'X軸從0開始
.ChartAreas(0).AxisX.Minimum = 0
.ChartAreas(0).AxisX.Interval = 10
'塞滿mschart
.ChartAreas(0).Position.Width = 95
.ChartAreas(0).Position.Height = 90
''Y軸標籤
'first row are dummy labels to suppress axis numberics
'.ChartAreas(0).AxisY.CustomLabels.Add(30, 40, "", 0, LabelMarkStyle.LineSideMark)
.ChartAreas(0).AxisY.CustomLabels.Add(ChtLSL, ChtLSL + 1, "LSL(" & ChtLSL.ToString & ")") 'LSL
.ChartAreas(0).AxisY.CustomLabels.Add(ChtTGT, ChtTGT + 1, "TGT(" & ChtTGT.ToString & ")") 'TGT
.ChartAreas(0).AxisY.CustomLabels.Add(ChtUSL, ChtUSL + 1, "USL(" & ChtUSL.ToString & ")") 'USL
.ChartAreas(0).AxisY.LabelAutoFitStyle = LabelAutoFitStyles.None
'Set series chart type
.Series("Series1").ChartType = SeriesChartType.Point
''加橫線
For LineRow As Integer = 1 To 3
Dim sl1 As StripLine = New StripLine
sl1.BackColor = Color.Black
sl1.StripWidth = 0.25
Select Case LineRow
Case 1
sl1.IntervalOffset = ChtLSL
Case 2
sl1.IntervalOffset = ChtTGT
Case 3
sl1.IntervalOffset = ChtUSL
End Select
.ChartAreas(0).AxisY.StripLines.Add(sl1)
Next
End With