找回密码
 立即注册
查看: 1472|回复: 12

简单的上位机串口控制单片机

[复制链接]
  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:23:18 | 显示全部楼层 |阅读模式
测试使用上位机提供web服务,通过串口来控制单片机
回复

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:25:07 | 显示全部楼层
单片机使用STC8H8K64U,上位机使用的是golang语言,最终效果如下
1.png
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:26:36 | 显示全部楼层
单片机的程序可以参照,实现了简单的串口通信以及串口数据处理
STC8H8K64U-软件实现串口不断电烧录程序
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:28:17 | 显示全部楼层
串口数据处理函数中,如果收到hello 则返回world!,如果收到0x0f,则将P1端口设置为接收到的第二位数据
  1. void DealString(char *p)
  2. {
  3.     if(p[0] == 0x0f)
  4.     {
  5.         P1 = p[1];
  6.         UartSendStr("p1");
  7.     }else if(strcmp(p,"p1")==0)
  8.     {
  9.         UartSend(P1);
  10.     }
  11.     else if(strcmp(p,"hello") == 0)
  12.     {
  13.         UartSendStr("world!");
  14.     }
  15.     else if(strcmp(p,"reboot") == 0)
  16.     {
  17.         Delay_ms(1000);
  18.         UartSendStr("bye!");
  19.         IAP_CONTR = 0x60;
  20.     }
  21.     else if(strncmp(p,"seg",3) == 0)
  22.     {
  23.         STC_SEG7_ShowString(p+3);
  24.     }
  25.     else if (strcmp(p,"led40") == 0)
  26.     {
  27.         Test_STC_LED40();
  28.     }
  29.     else
  30.     {
  31.         UartSendStr(p);
  32.     }
  33. }
复制代码
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:29:27 | 显示全部楼层
完整的单片机代码和golang源码:

GO_Uart.zip

7.84 KB, 下载次数: 71

UART.zip

104.31 KB, 下载次数: 65

回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:37:54 | 显示全部楼层
golang上封装下串口操作,使用github.com/jacobsa/go-serial/serial包,实现了打开串口,收发数据,以及关闭串口
  1. package myfun
  2. import (
  3.         "errors"
  4.         "io"
  5.         "github.com/jacobsa/go-serial/serial"
  6. )
  7. var _port io.ReadWriteCloser = nil
  8. var _options = serial.OpenOptions{
  9.         PortName:              "",
  10.         BaudRate:              19200,
  11.         DataBits:              8,
  12.         StopBits:              1,
  13.         MinimumReadSize:       4,
  14.         InterCharacterTimeout: 500,
  15. }
  16. func OpenPort(port_name string) error {
  17.         if _port != nil {
  18.                 _port.Close()
  19.         }
  20.         var err error
  21.         _options.PortName = port_name
  22.         _port, err = serial.Open(_options)
  23.         if err != nil {
  24.                 _port = nil
  25.         }
  26.         return err
  27. }
  28. func SendAndRecv(msg string) (string, error) {
  29.         if _port == nil {
  30.                 return "", errors.New("请先打开串口")
  31.         }
  32.         bts := []byte(msg)
  33.         _, err := _port.Write(bts)
  34.         if err != nil {
  35.                 return "", err
  36.         }
  37.         buff := make([]byte, 128)
  38.         n, err := _port.Read(buff)
  39.         if err != nil {
  40.                 return "", err
  41.         }
  42.         return string(buff[:n]), nil
  43. }
  44. func SendAndRecv_bts(msg []byte) ([]byte, error) {
  45.         if _port == nil {
  46.                 return nil, errors.New("请先打开串口")
  47.         }
  48.         _, err := _port.Write(msg)
  49.         if err != nil {
  50.                 return nil, err
  51.         }
  52.         buff := make([]byte, 128)
  53.         _, err = _port.Read(buff)
  54.         if err != nil {
  55.                 return nil, err
  56.         }
  57.         return buff, nil
  58. }
  59. func ClosePort() error {
  60.         if _port != nil {
  61.                 return _port.Close()
  62.         }
  63.         return nil
  64. }
复制代码

回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:38:50 | 显示全部楼层
使用gin搭建web服务,实现web调用打开串口,开灯,关灯和hello的响应
  1. package myfun
  2. import (
  3.         "fmt"
  4.         "github.com/gin-gonic/gin"
  5. )
  6. func _send(c *gin.Context, msg string) {
  7.         c.JSON(200, gin.H{
  8.                 "msg": msg,
  9.         })
  10. }
  11. func Gin_OpenCom(c *gin.Context) {
  12.         com_name := c.Query("c")
  13.         if len(com_name) <= 0 {
  14.                 _send(c, "COM口异常")
  15.                 return
  16.         }
  17.         err := OpenPort(com_name)
  18.         if err == nil {
  19.                 _send(c, "COM口打开成功!")
  20.         } else {
  21.                 _send(c, err.Error())
  22.         }
  23. }
  24. func Gin_LedOn(c *gin.Context) {
  25.         _, err := SendAndRecv_bts([]byte{0x0f, 0x00})
  26.         if err != nil {
  27.                 _send(c, err.Error())
  28.         } else {
  29.                 _send(c, "开灯 - 操作成功")
  30.         }
  31. }
  32. func Gin_LedOff(c *gin.Context) {
  33.         _, err := SendAndRecv_bts([]byte{0x0f, 0xff})
  34.         if err != nil {
  35.                 _send(c, err.Error())
  36.         } else {
  37.                 _send(c, "关灯 - 操作成功")
  38.         }
  39. }
  40. func Gin_Hello(c *gin.Context) {
  41.         msg, err := SendAndRecv("hello")
  42.         fmt.Println(msg)
  43.         if err != nil {
  44.                 _send(c, err.Error())
  45.         } else {
  46.                 _send(c, msg)
  47.         }
  48. }
复制代码
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:39:45 | 显示全部楼层
简单写一个控制网页,使用XMLHttpRequest来后台发送获取数据
  1. <!DOCTYPE html>
  2. <html>
  3.     <head></head>
  4.     <body>
  5.         <div><p id="msg"></p></div>
  6.         <div>
  7.             串口:
  8.             <select id="com_select">
  9.                 <option value="COM1">COM1</option>
  10.                 <option value="COM2">COM2</option>
  11.                 <option value="COM3">COM3</option>
  12.                 <option value="COM4">COM4</option>
  13.                 <option value="COM5">COM5</option>
  14.                 <option value="COM6">COM6</option>
  15.                 <option value="COM7">COM7</option>
  16.                 <option value="COM8">COM8</option>
  17.                 <option value="COM9">COM9</option>
  18.             </select>
  19.             <button id="com_btn" onclick="open_com()">打开串口</button>
  20.         </div>
  21.         <br />
  22.         <div>
  23.             <button id="hello_btn" onclick="hello()">hello</button>
  24.             <button id="led_open_btn" onclick="led_on()">开灯</button>
  25.             <button id="led_close_btn" onclick="led_off()">关灯</button>
  26.         </div>
  27.         <script type="text/javascript">
  28.             xmlhttp = new XMLHttpRequest()
  29.             xmlhttp.onreadystatechange = function()
  30.             {
  31.                 if(xmlhttp.readyState==4 && xmlhttp.status==200)
  32.                 {
  33.                     var res = JSON.parse(xmlhttp.responseText)
  34.                     document.getElementById("msg").innerText = res.msg;
  35.                 }
  36.             }
  37.             function open_com(){
  38.                 var v = document.getElementById("com_select").value
  39.                 xmlhttp.open("GET","/open_com?c="+v,true)
  40.                 xmlhttp.send()
  41.             }
  42.             function led_on(){
  43.                 xmlhttp.open("GET","/led_on",true)
  44.                 xmlhttp.send()
  45.             }
  46.             function led_off(){
  47.                 xmlhttp.open("GET","/led_off",true)
  48.                 xmlhttp.send()
  49.             }
  50.             function hello(){
  51.                 xmlhttp.open("GET","/hello",true)
  52.                 xmlhttp.send()
  53.             }
  54.         </script>
  55.     </body>
  56. </html>
复制代码
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:41:01 | 显示全部楼层
如果需要手机访问,可以修改主函数中gin监听的ip如下即可
  1. func main() {
  2.         r := gin.Default()
  3.         r.GET("ping", pong)
  4.         r.GET("open_com", myfun.Gin_OpenCom)
  5.         r.GET("led_on", myfun.Gin_LedOn)
  6.         r.GET("led_off", myfun.Gin_LedOff)
  7.         r.GET("hello", myfun.Gin_Hello)
  8.         r.StaticFile("uart.html", "html/index.html")
  9.         r.Run(":12345")
  10.         fmt.Println("Hello")
  11. }
复制代码
回复 支持 反对

使用道具 举报 送花

  • 打卡等级:以坛为家I
  • 打卡总天数:247
  • 最近打卡:2025-02-25 19:02:50

13

主题

198

回帖

1175

积分

金牌会员

积分
1175
发表于 2024-2-3 12:46:04 | 显示全部楼层
手机上访问http://上位机ip:12345/uart.html则可进行控制了
Screenshot_2024-02-03-12-42-13-112_Alook~1.png
回复 支持 反对

使用道具 举报 送花

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|深圳国芯人工智能有限公司 ( 粤ICP备2022108929号-2 )

GMT+8, 2025-7-1 02:45 , Processed in 0.125778 second(s), 91 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表